#!/bin/sh /etc/rc.common
# Copyright (C) 2009 Draytek.com

START=99
YES=0	# TRUE
NO=1	# FALSE

check_rt2880_cfg() {
	local devid
	local chipid=18140801
	devid=$(cat /proc/bus/pci/devices | cut -f2)
	if [ ! -f /etc/config/wireless ]; then
		return $NO
	fi
	if echo "$devid" | grep -q $chipid; then
	    cat /etc/config/wireless | grep -qE 'rt2880\>' && return $YES
	else
	    return $NO
	fi
	return $NO
}

check_rt3062_cfg() {
	local devid
	local chipid=18143062
	devid=$(cat /proc/bus/pci/devices | cut -f2)
	if [ ! -f /etc/config/wireless ]; then
		return $NO
	fi
	if echo "$devid" | grep -q $chipid; then
	    cat /etc/config/wireless | grep -qE 'rt3062\>' && return $YES
	else
	    return $NO
	fi
	return $NO
}

check_rt2860_cfg() {
	local devid
	local chipid=18140601
	devid=$(cat /proc/bus/pci/devices | cut -f2)
	if [ ! -f /etc/config/wireless ]; then
		return $NO
	fi
	if echo "$devid" | grep -q $chipid; then
	    cat /etc/config/wireless | grep -qE 'rt2860\>' && return $YES
	else
	    return $NO
	fi
	return $NO
}

boot() {
	start
	DEVICE_REBOOT_FLAG=$(uci -q get system.@mail-alert[0].device_reboot_flag || echo 0)
	if [ "$DEVICE_REBOOT_FLAG" == "1" ]; then
		sh /sbin/mail-alert.sh 'reboot' '2130' '1' 'Reboot Alert' 2>/dev/null
	fi	
}

start() {
        mac_address=$(ifconfig eth0 | grep "HWaddr" | awk '{print $5}')
        uci -q -P /var/state set network.lan_mac=$mac_address
        mac_address=$(ifconfig eth1 | grep "HWaddr" | awk '{print $5}')
        uci -q -P /var/state set network.wan_mac=$mac_address
		sed 's/option password.*/option password ****/' /etc/config/grocx/users > /etc/config/users 2>/dev/null
        
        /etc/gcom/3g.reboot
        dray_fwup&
        /sbin/shnat_rate_monitor&
        nice /etc/wan_detect.sh&
		
		
		#set WAN IPv6 IP for static or dhcp
		WAN_IPv6_Type=$(uci -P /var/state get ipv6.wan.mode)
		if [ $WAN_IPv6_Type == "static" ]; then
			/usr/sbin/ipv6_wan_static.sh
		elif [ $WAN_IPv6_Type == "dhcpv6" ]; then  # IANA
			/usr/sbin/dhcpv6_client.sh
		fi

		#enable radvd
		LAN_RADVD=$(uci -P /var/state get ipv6.lan.radvd_en)
		if [ $LAN_RADVD == "1" ]; then
			/usr/sbin/radvd.sh > /dev/null 2>&1
		fi

		#enable dhcpv6 server
		DHCP6_EN=$(uci -q get ipv6.lan.dhcp6_en)
		if [ "$DHCP6_EN" == "1" ]; then
            /usr/sbin/dhcpv6_server.sh
        fi
        
        # Rossi: G42609 - Recovery the value of ARP table.
		if [ "$DHCP6_EN" == "1" -o $LAN_RADVD == "1" ]; then
            /etc/init.d/dnsmasq stop   > /dev/null 2>&1
            /etc/init.d/dnsmasq start  > /dev/null 2>&1
        fi

		/etc/tsp/tspinit start &
		# detect wlan card for rt3062, rt2860 and rt2880
		if ( check_rt2880_cfg || check_rt3062_cfg || check_rt2860_cfg ); then
			uci -q -P /var/state set network.wifi_device=1
			enc=$(uci get wireless.@wifi-iface[0].encryption)
			case "$enc" in
				WPA|wpa|WPA2|wpa2|WPA1WPA2|wpa1wpa2)
					# stop vitesse hardware DoS to pass radius authentication
					/etc/init.d/dosprotech stop
			;;
			esac
		fi

		#remove obsolete ipk in previous firmware
		obsolete="LPRng dosfstools dhcp6-client libvtss_api"
		for ipk in $obsolete; do
		    grep -q "Package: $ipk" /usr/lib/opkg/status || continue
		    [ -f /usr/lib/opkg/info/$ipk.list ] || opkg remove --force-remove $ipk
		done
		
		#set LAN IPv6 IP
		LAN_IPv6=$(uci get ipv6.lan.global_addr)
		LAN_INF=$(uci -q -P /var/state get network.lan.ifname)
		if [ $LAN_IPv6 ]; then
			ifconfig $LAN_INF $LAN_IPv6/64 
		fi
		if [ "$WAN_IPv6_Type" == "static_6in4" ]; then
			v6in4_PREFIX=$(uci get -q ipv6.wan_static_6in4.v6in4_lan_mask)
			v6in4_PREFIX_LEN=$(uci get -q ipv6.wan_static_6in4.v6in4_lan_mask_len)
			if [ "$v6in4_PREFIX" -a "$v6in4_PREFIX_LEN" ]; then
				ip addr add $v6in4_PREFIX:10/$v6in4_PREFIX_LEN dev br-lan
			fi
		fi		

		if [ $WAN_IPv6_Type == "dhcpv6-3" ]; then  # IAPD
			/sbin/dhcp6.sh client start iapd > /dev/null 2>&1
		elif [ $WAN_IPv6_Type == "dhcpv6-4" ]; then  # IAPD+IANA
			/sbin/dhcp6.sh client start both > /dev/null 2>&1
		fi
}
