#!/bin/sh /etc/rc.common

START=99
UCI_CONFIG="vlan_bridge"

add_vlan_bridge()
{
	config_get vid_wan $1 vid_wan
	config_get vid_lan $1 vid_lan
	wan_intf=`uci get vlan_bridge.$1.vid_wan`
	wan_vid=`uci get network.$vid_wan.vlan_id`
	wan_port_temp=`uci get network.$vid_wan.port`
	wan_port=$(echo $wan_port_temp | cut -c 4)
	cmm -c vlan bridge add wan $wan_vid lan $vid_lan $wan_port
	find_wan_interface $wan_intf 
	find_lan_interface $vid_lan
	#wan_vlan=`uci get vlan_bridge.$1.vid_wan`
	  
}

remove_vlan_bridge()
{
	config_get vid_wan $1 vid_wan
	config_get vid_lan $1 vid_lan
	wan_vid=`uci get network.$vid_wan.vlan_id`
	wan_port_temp=`uci get network.$vid_wan.port`
	wan_port=$(echo $wan_port_temp | cut -c 4)
	cmm -c vlan bridge remove wan $wan_vid lan $vid_lan	$wan_port
	refresh_lan_interface $vid_lan
}
find_wan_interface()
{
#. $IPKG_INSTROOT/lib/network/config.sh
#	scan_interfaces
	uptime=$(cat /proc/uptime | cut -d'.' -f 1)
#	for ifs in $interfaces; do
#		config_get vlan_id $ifs vlan_id
#		config_get ifname $ifs ifname
#	  [ "$1" = "$vlan_id" ] && {	 
      vlan_id_temp=`uci get network.$1.vlan_id` 
	  /usr/sbin/ip link set eth2.$vlan_id_temp down
	  /usr/sbin/ip link set eth2.$vlan_id_temp name wan-$1
	  /usr/sbin/ip link set wan-$1 up
	  json set network.$1 uptime=$uptime
	  json set network.$1 connection=bridge
	  json -f /var/status_system_interface set network.$1 connection=bridge	  
#	}
#	done 
	
}
find_lan_interface()
{
. $IPKG_INSTROOT/lib/network/config.sh
	scan_interfaces
	uptime=$(cat /proc/uptime | cut -d'.' -f 1)
	for ifs in $interfaces; do
		config_get vlan_id $ifs vlan_id
		config_get ifname $ifs ifname
	  [ "$1" = "$vlan_id" ] && {
	  json set network.$ifs uptime=$uptime
      json set network.$ifs connection=bridge
	  json -f /var/status_system_interface set network.$ifs connection=bridge	  
	}
	done 
	
}
refresh_lan_interface()
{
. $IPKG_INSTROOT/lib/network/config.sh
	scan_interfaces	
	for ifs in $interfaces; do
		config_get vlan_id $ifs vlan_id
		config_get ifname $ifs ifname
	  [ "$1" = "$vlan_id" ] && {	  
      json set network.$ifs connection=up
	  json -f /var/status_system_interface set network.$ifs connection=up 
	}
	done 
	
}

start()
{
	config_load $UCI_CONFIG
	config_foreach add_vlan_bridge
	#eth0 should enter promiscous mode if bride vlan work
	temp_config=$(cat /etc/config/$UCI_CONFIG)
	if [ -n "$temp_config" ]; then 
	  ifconfig eth0 promisc
      ifconfig eth2 promisc  
	else
	  ifconfig eth0 -promisc
	fi
}

stop()
{
	config_load $UCI_CONFIG
	config_foreach remove_vlan_bridge
	ifconfig eth0 -promisc
}

boot()
{
	start
}

apply_rule()
{
	config_get uciaction $1 uciaction
	config_get uciid $1 uciid
	case $uciaction in
		add)
			#echo "vlan_bridge:: action: add" > /dev/console
			add_vlan_bridge $1
			;;
		delete)
			#echo "vlan_bridge:: action: delete" > /dev/console
			remove_vlan_bridge $1
			;;
	esac
}

apply()
{
	tmpcfg=${UCI_CONFIG}_`cat /proc/uptime | cut -d. -f 1`
	uci fchanges export $UCI_CONFIG > /etc/config/$tmpcfg
	config_load $tmpcfg
	rm -f /etc/config/$tmpcfg
	config_foreach apply_rule
	uci commit $UCI_CONFIG
	#eth0 should enter promiscous mode if bride vlan work
	temp_config=$(cat /etc/config/$UCI_CONFIG)
	if [ -n "$temp_config" ]; then
	  ifconfig eth0 promisc
	  ifconfig eth2 promisc
	  
	else
	  ifconfig eth0 -promisc
	fi
}

