#!/bin/sh

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

start() {
    # Set governor to user space
    echo userspace > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
    # Set clock to 648MHz
    echo 648000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed
    
#     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"
    sleep 2
    # allow some time for lldp to send power requirements to the network...
    
    # prevent the codec from sleeping....
    qiba-aplay-loop.sh &

    mkdir -p /mnt/data/state/epic-sip
    
    cd /barix/apps/epic-sip
    ./auto-reset.sh &
    
    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
}

stop() {
    barix-wd --pid-file=$APP_PID --stop --wait
    barix-wd --pid-file=$UI_PID --stop --wait
    
#    /etc/init.d/dropbear stop
    
    kill $(cat /var/run/lldpd.pid)
    killall auto-reset.sh
}


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

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

exit $?

