#!/bin/sh

dhcpscript=/etc/init.d/nas/udhcps
dhcpclient=`which udhcpc`
inipath=/etc/network
dnsfile=/etc/resolv.conf
wifiexec=/etc/init.d/nas/wifi
defaultsys=/etc/.default.sys
masksum=/usr/sbin/masksum

wdev=$1

if [ -z "${wdev}" ]; then
    echo "eg: ${0} [wlan[0-32]]"
    exit 1
fi

dhcpip(){
    for dev in $@
    do
        $dhcpclient -i $dev -S -s $dhcpscript -b > /dev/null &
    done
}

numberkey=0
member=""
getmetric(){
    local currentdev=$1
    local defaultgw=`iniparse -c -f ${defaultsys} -s system -k defaultgwdev`
    [ "X${defaultgw}" = "X" ] && defaultgw="eth0"
    if [ "${currentdev}" != "${defaultgw}"  -a "${currentdev}" != "bond0" ];then
        if [ $numberkey -eq 0 ];then
            numberkey=2
        else
            let numberkey=numberkey+1
        fi
    else
        numberkey=1
    fi
    return 0
}

deleteRoute(){
    local interface=$1
    local route_table="tbl_${interface}"
    ip rule del table ${route_table}
    route -n | grep ${interface} > /tmp/table_${interface}
    while read line
    do
        local flag=`echo $line | awk '{print $4}'`
        if [ "X${flag}" = "XUG" ]; then
            route del default gw 0.0.0.0 dev ${interface}
        else
            local Destination=`echo $line | awk '{print $1}'`
            local Genmask=`echo $line | awk '{print $3}'`
            route del -net ${Destination} netmask ${Genmask} dev ${interface}
        fi                                                                                           
    done < /tmp/table_${interface}
    rm -fr /tmp/table_${interface}
}

Resolv_make(){
    local DNS=$1
    local level=$2
    [ -z "${DNS}" ] && return 1
    echo "Setting nameserver ${DNS}"
    sed -i "/${DNS}/d" ${dnsfile}
    if [ ${numberkey} -eq 1 ];then
        InsertFile "nameserver ${DNS}" $level
    else
        InsertFile "nameserver ${DNS}" 3
    fi
    sed -i '/^$/d' ${dnsfile}
    return 0
}

InsertFile(){
    local content=$1
    local pos=$2
    local rows=`wc -l ${dnsfile} | awk '{print $1}'`
    if [ $rows -eq 0 ]; then
        echo "${content}" > ${dnsfile}
    elif [ $pos -eq 1 ]; then
        sed -i "1i ${content}" ${dnsfile}
    elif [ $pos -eq 2 ]; then
        sed -i "1a ${content}" ${dnsfile}
    else
        sed -i "/$/a ${content}" ${dnsfile}
    fi
}

setnetworkdeault(){
    local ifcs=$1
    local route_table="tbl_${ifcs}" 
    local options_dhcp=`iniparse -c -f ${inipath} -s ${ifcs} -k DHCP | tr a-z A-Z`
    echo "${ifcs}" |egrep -q -E "^wlan[0-9]+"
    if [ $? -eq 0 ];then
        local WLANLINKMODE=`iniparse -c -f ${inipath} -s ${ifcs} -k WLANLINKMODE`
        local WLANSSID=`iniparse -c -f ${inipath} -s ${ifcs} -k WLANSSID`
        local WLANENC=`iniparse -c -f ${inipath} -s ${ifcs} -k WLANENC`
        local WLANPASSWD=`iniparse -c -f ${inipath} -s ${ifcs} -k WLANPASSWD`
        if [ -z "$WLANLINKMODE" -o -z "$WLANSSID" -o -z "$WLANENC" ]; then
            echo "[${ifcs}] try connect wifi ..."
        else
            ${wifiexec} "${ifcs}" "$WLANLINKMODE" "$WLANSSID" "$WLANENC" "$WLANPASSWD" > /dev/null
            if [ $? -ne 0 ];then
                echo "wifi init failed"
                continue
            else
                echo "wifi init success"
            fi
        fi
    fi

    getmetric ${ifcs}
    if [ "${options_dhcp}X" = "YESX" ] || [ "xxx${options_dhcp}" = "xxx" ]; then
        echo "Setting network by dhcp server"
        dhcpip ${ifcs}
    else
        local options_ip=`iniparse -c -f ${inipath} -s ${ifcs} -k IPADDRESS`
        [ "${options_ip}X" = "X" ] && IP="192.168.0.200"

        local options_netmask=`iniparse -c -f ${inipath} -s ${ifcs} -k NETMASK`
        [ "${options_netmask}X" = "X" ] && NETMASK="255.255.255.0"

        local options_mtu=`iniparse -c -f ${inipath} -s ${ifcs} -k MTU`
        [ "${options_mtu}X" = "X" ] && MTU="1500"

        # set ip netmask 
        echo "Setting IP address ${options_ip} ${options_netmask} ${options_mtu}"
        ifconfig ${ifcs} ${options_ip} netmask ${options_netmask} mtu ${options_mtu}
        local MASK=`${masksum} ${options_ip} ${options_netmask}`

        # set router here
        local options_gateway=`iniparse -c -f ${inipath} -s ${ifcs} -k GATEWAY`
        if [ "${options_gateway}X" != "X" ]; then
            deleteRoute ${ifcs}
            echo "WIFI: Adding router ${options_gateway}================${ifcs}================${numberkey}"
            route add -net ${MASK} dev ${ifcs}
            [ ${numberkey} -eq 1 ] && route add default gw ${options_gateway} dev ${ifcs}
            # config eht iproute2                                                                
            ip route flush table ${route_table}                                                  
            ip route add ${MASK} src ${options_ip} dev ${ifcs} table ${route_table}         
            ip route add default via ${options_gateway} src ${options_ip} dev ${ifcs} table ${route_table}
            ip rule add from ${options_ip} table ${route_table}                                                
            Resolv_make "${options_gateway}" "1" 
        fi

        # set mac address here
        local options_mac=`iniparse -c -f ${inipath} -s ${ifcs} -k MAC`
        if [ "${options_mac}X" != "X" ]; then
            echo "Setting mac address ${options_mac}"
            ifconfig ${ifcs} hw ether ${options_mac}
        fi

        local options_dns1=`iniparse -c -f ${inipath} -s ${ifcs} -k DNS1`
        Resolv_make "${options_dns1}" "1"
        local options_dns2=`iniparse -c -f ${inipath} -s ${ifcs} -k DNS2`
        Resolv_make "${options_dns2}" "2"
    fi
}

if [ ! -f "$defaultsys" ];then                                         
    cp /etc.default/.default.sys /etc/.default.sys -a
fi
sleep 1
setnetworkdeault ${wdev}
