#! /bin/sh

APP_PID=/var/run/audioplex-app.pid

start() {

    # try to get a valid date to avoid certificate validation
    # issues...
    /usr/local/sbin/get-portal-date.sh &
    
    amixer -c 2 sset "Line Out" 100%
    amixer -c 2 sset "DAC" 100%
    
#    echo "[hack alert!] forcing plug:analog_out_master to pop up..."
#    touch /tmp/dummy;
#    aplay -D plug:analog_out_master dummy >/dev/null 2>&1

    test -d /tmp/retailplayer || mkdir /tmp/retailplayer
       
    UI_DISABLED=$(/sbin/uci -q get httpd.webserver.disabled)
    if [ "$UI_DISABLED" != "true" ]; then
        /etc/init.d/lighttpd start
    fi
    
    HW_TYPE=$(qiba-spi-get-production-info.sh -w)
    
    case "$HW_TYPE" in
        54)
            BASE_BOARD="m400"
            echo 0 > /sys/class/leds/barix:led1:red/brightness
            echo 255 > /sys/class/leds/barix:led1:green/brightness
            ;;
	66) 
            BASE_BOARD="tpa400"
            echo 0 > /sys/class/leds/barix:led1:red/brightness
            echo 255 > /sys/class/leds/barix:led1:green/brightness
            ;;
        59|*)
            BASE_BOARD="s400"
            ;;        
    esac
    
    # disable IPV6
    sysctl -w net.ipv6.conf.all.disable_ipv6=1
    sysctl -w net.ipv6.conf.default.disable_ipv6=1

    echo "Launching audioplex with BASE_BOARD='$BASE_BOARD'"
    barix-wd --pid-file=$APP_PID --timeout=5 --background --start -- \
        audioplex --machine=$BASE_BOARD
}


stop() {
    barix-wd --pid-file=$APP_PID --stop     
}


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

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

exit $?
