#!/bin/sh
##############
# LED ON/OFF
##############

led_on=255
led_off=0
model=$(head -n 1 /etc/version)
if [ "$model" = "Vigor2960" -o "$model" = "Vigor2960F"  ];then
	#csm qos wlan dos
	type=$1

	csm_led_control='/sys/class/leds/draytek-x2-led-csm/brightness'
	qos_led_control='/sys/class/leds/draytek-x2-led-qos/brightness'
	wlan_led_control='/sys/class/leds/draytek-x2-led-wlan/brightness'
	dos_led_control='/sys/class/leds/draytek-x2-led-wlan/brightness'

	if [ "$type" = "wlan" ];then
		check_wifi=$(ls /sys/class/ieee80211)
		if [ "$check_wifi" = "" ];then
			return;
		fi
	fi

	if [ "$type" = "csm" ];then
		#1.csm  config : app_filter and url_filter
		csm_led_check_app=$(uci show app_filter | grep status=enable | head -n 1)
		csm_led_check_url=$(uci show url_filter | grep status=enable | head -n 1)
		
		if [ -n "$csm_led_check_app" ] || [ -n "$csm_led_check_url" ];then
			echo $led_on > $csm_led_control
			json -f /var/status_system_interface set "led.csm"  \
				  link="up"
		elif [ -z "$csm_led_check_app" ] && [ -z "$csm_led_check_url" ];then
			echo $led_off > $csm_led_control
			json -f /var/status_system_interface set "led.csm"  \
				  link="down"
		fi
	elif [ "$type" = "qos" ];then
		#2. QoS config :qos_lanv4, packet_mark_lan, qos_lan_sch_shpaer_ttl

		local isQoS_on=0
		local isSW_QoS=1
		
		if [ "$isSW_QoS" = "1" ]; then	# software QoS here.....
			cat /etc/config/sw_qos_out /etc/config/sw_qos_in | grep -q "enable" && {
				echo $led_on > $qos_led_control
				json -f /var/status_system_interface set "led.qos"  \
				  link="up"
				isQoS_on=1
			} || {	# check for HW QoS

				#2.1 check incomping filter
				imftr_drop=$(uci show qos_lanv4 | grep drop=enable | head -n 1)
				imftr_reserved=$(uci show qos_lanv4 | grep reserved=enable | head -n 1)
				imftr_policer0=$(uci show qos_lanv4 | grep policer=incoming_class0 | head -n 1)
				imftr_policer1=$(uci show qos_lanv4 | grep policer=incoming_class1 | head -n 1)

				if [ -n "$imftr_drop" ] || [ -n "$imftr_reserved" ] || [ -n "$imftr_policer0" ] || [ -n "$imftr_policer1" ];then
					echo $led_on > $qos_led_control
					json -f /var/status_system_interface set "led.qos"  \
						link="up"
					isQoS_on=1
				fi

				#2.2 check incomping class

				imcls=$(uci show qos_lanv4.ADM1_SHAPER_CONF | grep enable=enable )
				if [ -n "$imcls" ] ;then
					echo $led_on > $qos_led_control
					json -f /var/status_system_interface set "led.qos"  \
						link="up"
					isQoS_on=1
				fi

				#2.3 check outgoing filter
				omftr=$(uci show packet_mark_lan | grep status=disable )
				if [ -n "$omftr" ] ;then
					echo $led_on > $qos_led_control
					json -f /var/status_system_interface set "led.qos"  \
						link="up"
					isQoS_on=1
				fi

				#2.4 check outgoing class
				omcls_ttl=$(uci show qos_lan_sch_shpaer_ttl | grep enable=enable )
				omcls_class0=$(uci show qos_lanv4.outgoing_class0 | grep enable=enable )
				omcls_class1=$(uci show qos_lanv4.outgoing_class1 | grep enable=enable )
				omcls_class2=$(uci show qos_lanv4.outgoing_class2 | grep enable=enable )
				omcls_class3=$(uci show qos_wanv4.outgoing_class2 | grep enable=enable )	# check LAN tx as inclomig 

				if [ -n "$omcls_ttl" ] || [ -n "$omcls_class0" ] || [ -n "$omcls_class1" ]  || [ -n "$omcls_class2" ] || [ -n "$omcls_class3" ];then
					echo $led_on > $qos_led_control
					json -f /var/status_system_interface set "led.qos"  \
						link="up"
					isQoS_on=1
				fi

				if [ "$isQoS_on" = "0" ];then
					echo $led_off > $qos_led_control
					json -f /var/status_system_interface set "led.qos"  \
						link="down"
				fi
			}
		fi

	elif [ "$type" = "wlan" ];then
		#4. wlan :wireless

		wlan_status=$(uci show wireless | grep disabled=0)
		if [ "$wlan_status" != "" ];then
			echo $led_on > $wlan_led_control
			json -f /var/status_system_interface set "led.wlan"  \
				  link="up"
		else
			echo $led_off > $wlan_led_control
			json -f /var/status_system_interface set "led.wlan"  \
				  link="down"
		fi

	elif [ "$type" = "dos" ];then
		#5. dos :ddos

		dos_status=$(uci get ddos.ddos_config.status)
		if [ "$dos_status" = "enable" ];then
			echo $led_on > $dos_led_control
			json -f /var/status_system_interface set "led.dos"  \
				  link="up"
		else
			echo $led_off > $dos_led_control
			json -f /var/status_system_interface set "led.dos"  \
				  link="down"
		fi
	fi
elif [ "$model" = "Vigor300B" ];then
	type=$1
	wlan_led_control='/sys/class/leds/draytek-x2-led-wlan/brightness'
	dos_led_control='/sys/class/leds/draytek-x2-led-wlan/brightness'

	if [ "$type" = "wlan" ];then
		check_wifi=$(ls /sys/class/ieee80211)
		if [ "$check_wifi" = "" ];then
			return;
		fi
	fi

	if [ "$type" = "wlan" ];then
		#1. wlan :wireless

		wlan_status=$(uci show wireless | grep disabled=0)
		if [ "$wlan_status" != "" ];then
			echo $led_on > $wlan_led_control
			json -f /var/status_system_interface set "led.wlan"  \
				  link="up"
		else
			echo $led_off > $wlan_led_control
			json -f /var/status_system_interface set "led.wlan"  \
				  link="down"
		fi

	elif [ "$type" = "dos" ];then
		#2. dos :ddos
		
		dos_status=$(uci get ddos.ddos_config.status)
		if [ "$dos_status" = "enable" ];then
			echo $led_on > $dos_led_control
			json -f /var/status_system_interface set "led.dos"  \
				  link="up"
		else
			echo $led_off > $dos_led_control
			json -f /var/status_system_interface set "led.dos"  \
				  link="down"
		fi
	fi
else
	return;
fi