#!/bin/sh

. /etc/wlan/wifi_conf

prog_name=`basename $0`
ledcase=$1
action=$2

[ -z $action ] && {
    echo "Usage:"
    echo " wpsled wps_lock_down <on/off>"
    exit
}

if eval "type ledcontrol" 2>/dev/null >/dev/null; then
    if [ -f /tmp/orbi_project ]; then
        led_off=''
        led_on=''
        wifiled_off=''
        wifiled_on=''
    else
        led_off='/sbin/ledcontrol -n wps -c green -s off'
        led_on='/sbin/ledcontrol -n wps -c green -s on'
        wifiled_off='/sbin/ledcontrol -n wifi -c green -s off'
        wifiled_on='/sbin/ledcontrol -n wifi -c green -s on'
    fi
elif [ -e /proc/simple_config/tricolor_led ]; then
    led_off='echo 0 > /proc/simple_config/tricolor_led'
    led_on='echo 1 > /proc/simple_config/tricolor_led'
    wifiled_off=''
    wifiled_on=''
elif [ -e /proc/simple_config/simple_config_led ]; then
    led_off='echo 1 > /proc/simple_config/simple_config_led'
    led_on='echo 2 > /proc/simple_config/simple_config_led'
    wifiled_off=''
    wifiled_on=''
else
    echo "WARNING: No method to control WPS LED"
    led_off=''
    led_on=''
    wifiled_off=''
    wifiled_on=''
fi

set_wifiled(){
	g_enabled=`/bin/config get endis_wl_radio`
	a_enabled=`/bin/config get endis_wla_radio`
	a2_enabled=`/bin/config get endis_wla_2nd_radio`
	if [ "$g_enabled" = "1" ] || [ "$a_enabled" = "1" ] || [ "$a2_enabled" = "1" -a "x$is_dual_band" = "x0" ]; then
		if [ "$(${CONFIG} get wireless1_vap)" = "1" -a "x${is_single_ssid}" = "x1" ] ||
		[ "$(${CONFIG} get wireless2_vap)" = "1" -a "x${is_single_ssid}" = "x1" ] ||
		[ "$(${CONFIG} get wireless3_vap)" = "1" -a "x${is_single_ssid}" = "x1" ] ||
		[ "$(${CONFIG} get wireless4_vap)" = "1" -a "x${is_single_ssid}" = "x1" -a "x$is_dual_band" = "x0" ] ||
		[ "x${is_single_ssid}" != "x1" ]; then
			eval $wifiled_on
		else
			eval $wifiled_off
		fi

	else
		eval $wifiled_off
	fi

}

set_finialwpsled(){
	CONFIG=/bin/config
	if [ "x$($CONFIG get bridge_mode)" = "x1" ]; then
		if [ "$(${CONFIG} get bridge_band_choose)" = "2.4g" -a "$(${CONFIG} get wl_bridge_sectype)" = "1" ] ||
		[ "$(${CONFIG} get bridge_band_choose)" = "5g" -a "$(${CONFIG} get wla_bridge_sectype)" = "1" ]; then
			eval $led_off
		else
			eval $led_on
		fi
	else
		if [ "$(${CONFIG} get endis_wl_radio)" = "1" -a "$(${CONFIG} get wl_sectype)" != "1" ] ||
		[ "$(${CONFIG} get endis_wla_radio)" = "1" -a "$(${CONFIG} get wla_sectype)" != "1" ] ||
		[ "$(${CONFIG} get endis_wla_2nd_radio)" = "1" -a "$(${CONFIG} get wla_2nd_sectype)" != "1" -a "x$is_dual_band" = "x0" ]; then
			if [ "$(${CONFIG} get wireless1_vap)" != "1" -a "x${is_single_ssid}" = "x1" ]; then
				eval ${led_off}
			else
				eval ${led_on}
			fi
		else
			eval $led_off
		fi
	fi
}

kill_wpsled(){
    name=$1
    ledpid=`ps | grep $prog_name | grep $name | grep on | awk '{print $1}'`
    [ -n "$ledpid" ] && kill -9 $ledpid
}

wpsled_run_file=/tmp/.wpsled_is_running

case $ledcase in
    wps_lock_down)
        if [ "$action" = "on" ]; then
            if [ -e $wpsled_run_file ]; then
                echo "Already on wpsled process running, exit..."
                exit;
            fi
            touch $wpsled_run_file
            led_next=off
            while [ 1 ]; do
	        if [ $led_next = "on" ]; then
		    eval $led_on
		    led_next=off
                    usleep 100000 # 0.1 second
	        else
		    eval $led_off
		    led_next=on
                    is_lockdown=`config get wps_lock_down`
                    if [ "x$is_lockdown" != "x1" ]; then
                        rm -f $wpsled_run_file
                        set_finialwpsled
                        set_wifiled
                        exit
                    fi
                    usleep 900000 # 0.9 second
	        fi
            done
        elif [ "$action" = "stop" ]; then
            kill_wpsled $ledcase
            rm -f $wpsled_run_file
        else
            kill_wpsled $ledcase
            set_finialwpsled
            set_wifiled
            rm -f $wpsled_run_file
        fi
        ;;
    *)
        ;;
esac
