#!/bin/sh

. /lib/functions.sh

classify_no=0
generate_dns_rule(){
	classify_no=$((classify_no + 1))
	uci add qos classify
	uci rename qos.@classify[-1]="c${classify_no}"
	uci set qos.@classify[-1].ifname="lo"
	uci set qos.@classify[-1].all_interfaces="0"
	uci set qos.@classify[-1].proto="udp"
	uci set qos.@classify[-1].ethertype="IPv4"
	uci set qos.@classify[-1].dest_port="53"
	uci set qos.@classify[-1].traffic_class="$1"
	uci set qos.@classify[-1].order="$classify_no"
}

generate_dhcp_rule(){
	classify_no=$((classify_no + 1))
	uci add qos classify
	uci rename qos.@classify[-1]="c${classify_no}"
	uci set qos.@classify[-1].ifname="lo"
	uci set qos.@classify[-1].all_interfaces="0"
	uci set qos.@classify[-1].proto="udp"
	uci set qos.@classify[-1].ethertype="IPv4"
	uci set qos.@classify[-1].dest_port="67"
	uci set qos.@classify[-1].dest_port_range="68"
	uci set qos.@classify[-1].traffic_class="$1"
	uci set qos.@classify[-1].order="$classify_no"
}

generate_igmp_rule(){
	classify_no=$((classify_no + 1))
	uci add qos classify
	uci rename qos.@classify[-1]="c${classify_no}"
	uci set qos.@classify[-1].ifname="lo"
	uci set qos.@classify[-1].all_interfaces="0"
	uci set qos.@classify[-1].proto="IGMP"
	uci set qos.@classify[-1].ethertype="IPv4"
	uci set qos.@classify[-1].traffic_class="$1"
	uci set qos.@classify[-1].order="$classify_no"
}

generate_icmp_rule(){
	classify_no=$((classify_no + 1))
	uci add qos classify
	uci rename qos.@classify[-1]="c${classify_no}"
	uci set qos.@classify[-1].ifname="lo"
	uci set qos.@classify[-1].all_interfaces="0"
	uci set qos.@classify[-1].proto="icmp"
	uci set qos.@classify[-1].ethertype="IPv4"
	uci set qos.@classify[-1].traffic_class="$1"
	uci set qos.@classify[-1].order="$classify_no"

	classify_no=$((classify_no + 1))
	uci add qos classify
	uci rename qos.@classify[-1]="c${classify_no}"
	uci set qos.@classify[-1].proto="icmp"
	uci set qos.@classify[-1].ethertype="IPv4"
	uci set qos.@classify[-1].traffic_class="$2"
	uci set qos.@classify[-1].order="$classify_no"
}

generate_classify(){

	no_queue_file="/tmp/qos/no_queue_per_port"
	queue_num=$(cat "$no_queue_file")
	rm -f "$no_queue_file"

	# assign queue type
	if [ $queue_num -eq 8 ]; then
		q_def_queue="0"
		q_low="1"
		q_besteffort="2"
		q_normal="3"
		q_video="4"
		q_medium="5"
		q_high="6"
		q_highest="7"
	elif [ $queue_num -eq 4 ]; then
		q_def_queue="0"
		q_normal="1"
		q_medium="2"
		q_highest="3"
	fi

	# Local generated DNS traffic goes to q_highest
	generate_dns_rule $q_highest
	# Local generated IGMP traffic goes to q_highest
	generate_igmp_rule $q_highest
	# Local generated DHCP traffic goes to q_highest
	generate_dhcp_rule $q_highest
	# Local generated ICMP traffic goes to q_highest rotue as q_normal
	generate_icmp_rule $q_highest $q_normal

	# VLAN priority tag -> Queue priority
	for i in `seq 1 7`; do
		classify_no=$((classify_no + 1))
		uci add qos classify
		uci rename qos.@classify[-1]="c${classify_no}"
		uci set qos.@classify[-1].pcp_check="$i"
		uci set qos.@classify[-1].all_interfaces="0"
		uci set qos.@classify[-1].traffic_class="$i"
		uci set qos.@classify[-1].order="$classify_no"
	done
}

# cleaning up for upgrade same version that contain firewall.qos
if [ -f "/etc/firewall.qos" ]; then
	rm -f "/etc/firewall.qos"
	if uci -q get firewall.qos >/dev/null; then
		uci delete firewall.qos
	fi
fi

if uci -q get qos.@classify[0] >/dev/null; then
	exit
else
	generate_classify
fi
