#!/bin/sh

start()
{
    /usr/bin/qiba-aplay-loop.sh &
    sleep 1

    HW_TYPE=$(/usr/bin/qiba-spi-get-production-info.sh -w)
    
    #
    # M400 & S400 until boot completes the system LED is the same
    if [ "$HW_TYPE" = "54" ]; then
        # M400
        echo 14 > /sys/class/gpio/export
        echo out > /sys/class/gpio/gpio14/direction
        echo 0 > /sys/class/gpio/gpio14/value
    fi
   
    # All others...
    echo "timer" > /sys/class/leds/barix:led1:red/trigger
    echo 0 > /sys/class/leds/barix:led1:green/brightness
    
    /usr/bin/amixer -q -M -c 2 set 'Line Out' 0%
    /usr/bin/amixer -q -M -c 2 set 'DAC' 0%
    
    echo 17 > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio17/direction
    echo 0 > /sys/class/gpio/gpio17/value

    /usr/bin/amixer -q -M -c 2 set 'Line Out' 100%
    /usr/bin/amixer -q -M -c 2 set 'DAC' 100%
    /usr/bin/amixer -q -- sset BARIX_MAIN 100%
    /usr/sbin/alsactl store
    
    # TPA400 digital amplifier
    if [ "$HW_TYPE" = "66" ]; then
        echo 1 > /sys/class/gpio/gpio17/value
        /usr/bin/digital-audio-amp tas2770 0x41 0x0c &
    fi
    
    # set DEP config
    python3 /usr/bin/setDepConfig.py &

    #need before loading external wifi driver module (rtl8812bu)
    depmod -a
}

stop()
{
    /usr/bin/amixer -q -M -c 2 set 'Line Out' 0%
    /usr/bin/amixer -q -M -c 2 set 'DAC' 0%

    HW_TYPE=$(/usr/bin/qiba-spi-get-production-info.sh -w)
    if [ "$HW_TYPE" = "54" ]; then
        echo 0 > /sys/class/gpio/gpio14/value
    fi
    
    echo 0 > /sys/class/gpio/gpio17/value
}


case "$1" in

"start")
    start
    ;;
    
"stop")
    stop
    ;;
    
esac
