#!/bin/sh
#
# Starts Sonic IP announcement

# Use Barix utilities.
source /lib/config/functions.sh
source /usr/local/sbin/barix-sound-ctrl.sh

# The old sonic_ip configuration was located inside network group.
# This function checks for this configuration and moves it to the new one.
move_config_from_network() {
    #
    # make sure the uci config and sections exist. Even if the defaults
    # are not the same as the current (older version) values, the latter will
    # replace the defaults in the next step.
    if [ ! -f /barix/config/current/sonic_ip ]; then
        cp /barix/config/defaults/sonic_ip /barix/config/current/sonic_ip
    fi
    
    uci -q get network.sonic_ip
    if [ $? -eq 0 ]; then
        uci -q set sonic_ip.general.enabled=$(uci -q get network.sonic_ip.enabled)
        VOLUME=$(uci -q get network.sonic_ip.volume)
        uci -q set sonic_ip.general.volume=$(echo $VOLUME | sed 's/%//')
        uci -q delete network.sonic_ip
    else
        # Remove percentual symbol
        VOLUME=$(uci -q get sonic_ip.general.volume)
        uci -q set sonic_ip.general.volume=$(echo $VOLUME | sed 's/%//')
    fi
    
    uci commit    
}

get_ip_address(){
    ip_addr=`ifconfig $1 2>/dev/null | grep -o "inet [^ ]*"`
    ip_addr=${ip_addr#inet }
    echo -n "$ip_addr"
}

SOUND_CARD_CONF=/etc/barix/soundcard.conf

start() {
    move_config_from_network

    if cfg_bool_is_true sonic_ip.general.enabled ; then
        SONICIP_VOLUME=$(uci -q get sonic_ip.general.volume 2>/dev/null)

        if [ -z "$SONICIP_VOLUME" ]; then
            echo "sonic_ip.volume not set or invalid. set to default value of 70" >&2
            SONICIP_VOLUME="70"
        fi

        if board-probe m400; then
            m400-amp enable
            m400-rca out
        elif board-probe s400 || board-probe sp400; then
            s400-minijack enable
        elif board-probe mpi400; then
            mpi400-audio enable
        else
            sound_set_amplifier 1
        fi
        echo -n "Starting sonic IP announcement with volume $SONICIP_VOLUME% :"
        if [ -f $SOUND_CARD_CONF ] ; then
            MASTER_CONTROL=$(cat $SOUND_CARD_CONF | grep "master.control" | sed -e 's/master.control\s*=\s*//')
	    MASTER_DEVICE=$(cat $SOUND_CARD_CONF | grep "master.device" | sed -e 's/master.device\s*=\s*//')
            /usr/bin/amixer -M -q set "$MASTER_CONTROL" $SONICIP_VOLUME%
        else
            /usr/bin/amixer -M -q -D hw:Codec sset DAC $SONICIP_VOLUME%
        fi
        IFC=""
        CNT=0
        while [ -z $IFC ]; do
            ADDR=$(get_ip_address eth0)
            if [ -n "$ADDR" ]; then
                IFC=eth0
                break
            fi
            
            ADDR=$(get_ip_address wlan0)
            if [ -n "$ADDR" ]; then
                IFC=wlan0
                break
            fi
            
            if [ $CNT -le 60 ]; then
                sleep 1
                CNT=$((CNT+1))
            else
                break
            fi

        done

        # If, at this point we still don have an IP from an interface,
        # just use eth0...
        test -z "$IFC" && IFC=eth0
        /usr/bin/sonic-ip ${IFC} ${MASTER_DEVICE}
    else
        echo "Sonic IP is disabled, skipping"
    fi
}
restart() {
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
    # no action
    ;;
    restart|reload)
        restart
    ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

