#!/bin/sh
. /barix/apps/epic-sip/common.sh

APP_PID=/var/run/msx00-app.pid
UI_PID=/var/run/msx00-ui.pid

# Detect device model
detect_model() {
    VER=$(cat /barix/info/VERSION)
    # echo "VER: $VER"

    # Set the volume for the XP and RP1 inputs to -79db, if the board is ms720 or ms775
    if [ -f /barix/apps/epic-sip/run-once.sh ]; then
        /barix/apps/epic-sip/run-once.sh
        rm /barix/apps/epic-sip/run-once.sh
    fi
    
    detect_board
    # echo "  Model: $MODEL"


    # Set device name for lldp
    sed -i "s/.*hostname.*/configure system hostname $MODEL/g" /etc/lldpd.conf
    sed -i "s/.*DAEMON_ARGS.*/DAEMON_ARGS=\"-M 2 -S AE-$MODEL\"/g" /etc/default/lldpd

    # Set device name for snmp
    DESCR="${MODEL} ${VER}"
    sed -i '/^sysDescr/d' /etc/snmp/snmpd.conf
    sed -i "1s/^/sysDescr $DESCR\n/" /etc/snmp/snmpd.conf
    sed -i '/^sysName/d' /etc/snmp/snmpd.conf
    sed -i "1s/^/sysName $MODEL\n/" /etc/snmp/snmpd.conf

    # Set device name for the application
    echo "$MODEL" > /barix/apps/epic-sip/MODEL
    uci set epic.main.model="$MODEL"
    if [ -z "$(uci -q get epic.main.device_alias)" ]; then
        uci set epic.main.device_alias="$MODEL"
    fi
    uci set epic.main.description="System Description: $MODEL $VER"
    uci commit

    # Create the ae_ms.volume.out0 and ae_ms.volume.enable0
    if [ "$MODEL" = "MS-775" ] || [ "$MODEL" = "MS-720" ]; then
        if [ -z "$(uci -q get ae_ms.volume.out0)" ]; then
            uci set ae_ms.volume.out0='0'
            uci commit
        fi
        if [ -z "$(uci -q get ae_ms.volume.enable0)" ]; then
            uci set ae_ms.volume.enable0='true'
            uci commit
        fi
    fi

    echo "$MODEL"
}

init_gpio() {
    # Create the gpio devices for each pin for the 1st Padauke, if they are not already there
    if [ ! -d /sys/class/gpio/gpio400 ]; then echo 400 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio401 ]; then echo 401 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio402 ]; then echo 402 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio403 ]; then echo 403 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio404 ]; then echo 404 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio405 ]; then echo 405 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio406 ]; then echo 406 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio407 ]; then echo 407 > /sys/class/gpio/export; fi

    # Set pin direction
    echo out > /sys/class/gpio/gpio400/direction        # AMP_STDBY
    echo in > /sys/class/gpio/gpio401/direction
    echo in > /sys/class/gpio/gpio402/direction
    echo in > /sys/class/gpio/gpio403/direction         # AMP_STATUS
    echo in > /sys/class/gpio/gpio404/direction         # Device type (1=CZ300, 0=MS-375)
    echo in > /sys/class/gpio/gpio405/direction         # Model
    echo in > /sys/class/gpio/gpio406/direction
    echo out > /sys/class/gpio/gpio407/direction        # CPU LEDs control. Set to 0 to have app to control them.

    # Disable AMP
    echo 1 > /sys/class/gpio/gpio400/value

    # LEDs controlled by the CPU
    echo 1 > /sys/class/gpio/gpio407/value


    # Create the gpio devices for each pin for the 2nd Padauke, if they are not already there
    if [ ! -d /sys/class/gpio/gpio408 ]; then echo 408 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio409 ]; then echo 409 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio410 ]; then echo 410 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio411 ]; then echo 411 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio412 ]; then echo 412 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio413 ]; then echo 413 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio414 ]; then echo 414 > /sys/class/gpio/export; fi
    if [ ! -d /sys/class/gpio/gpio415 ]; then echo 415 > /sys/class/gpio/export; fi

    # Set pin direction
    echo out > /sys/class/gpio/gpio408/direction     # OUT1
    echo out > /sys/class/gpio/gpio409/direction     # OUT2
    echo out > /sys/class/gpio/gpio410/direction     # WPA OUT1
    echo out > /sys/class/gpio/gpio411/direction     # EPA OUT2
    echo in > /sys/class/gpio/gpio412/direction      # IN1_WPA_EM
    echo in > /sys/class/gpio/gpio413/direction      # IN2_WPA_CALL
    echo in > /sys/class/gpio/gpio414/direction      # IN1_USER1
    echo in > /sys/class/gpio/gpio415/direction      # IN1_USER2

    #Turn LEDs off
    i2cset -y 0 0x43 0x08 0x00
    i2cset -y 0 0x43 0x09 0x00
    i2cset -y 0 0x43 0x0a 0x00
    i2cset -y 0 0x43 0x0b 0x00

    # Enable USB
    i2cset -y 0 0x42 0x01 0x01

    # Mute the mixer IC - for normal operation
    i2cset -y 0 0x44 0xf9

    # Unmute the mixer IC - for testing
#    i2cset -y 0 0x44 IN1_WPA_EM
#    i2cset -y 0 0x44 0xe0
#    i2cset -y 0 0x44 0xd0
}


start() {
    init_gpio
    sleep 1
    detect_model

    sleep 5 && /barix/apps/epic-sip/ssh_key_mon.sh &

#     lldpd -p /var/run/lldpd.pid -S "Audio Enhancement MS-500v2" \
#         -M 2 \
#         -u /var/run/lldpd.socket
#
#     #lldpcli configure lldp tx-interval 60
#     #lldpcli configure lldp status tx-only
#     #lldpcli configure med policy application voice-signaling
#     lldpcli configure me power pd source pse priority critical value 25500
#     lldpcli configure dot3 power pd supported enabled paircontrol powerpairs signal class class-4 type 2 source pse priority critical requested 25500 allocated 25500
    /etc/init.d/lldpd start

    snmp=$(uci get epic.snmp.enable)
    if [ "$snmp" == "true" ]; then
        /etc/init.d/snmpd start
    else
        /etc/init.d/snmpd stop
    fi
    sleep 2

    amixer -c 2 sset "Line Out" 100%
    amixer -c 2 sset "DAC" 100%
    # amixer -c 2 sset "Line In" "off"

    # prevent the codec from sleeping....
    qiba-aplay-loop.sh &

    mkdir -p /mnt/data/state/epic-sip

    cd /barix/apps/epic-sip
    barix-wd --pid-file=$APP_PID --timeout=5 --background --start -- \
        python3 main.py

    cd /barix/apps/epic-sip/ui
    barix-wd --pid-file=$UI_PID --timeout=5 --background --start -- \
        python3 main.py

    if [ -f /barix/apps/epic-sip/updated_flag ]; then
        # This is necessary for alsa to create the WAP and RP1 controls
        # Otherwise the following commands won't have any effect
        /usr/bin/arecord -d 3 -f S16_LE -r 21050 /barix/apps/epic-sip/tmp.wav
        rm /barix/apps/epic-sip/tmp.wav
        rm /barix/apps/epic-sip/updated_flag
    fi
}

stop() {
    killall ssh_key_mon.sh
    barix-wd --pid-file=$APP_PID --stop --wait
    barix-wd --pid-file=$UI_PID --stop --wait

    killall qiba-aplay-loop
}

# See how we were called.
case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit $?


