#!/bin/sh
# chkconfig: 345 40 60
#
# network
#
# bring up network driver and interface
# configure the network interface


############
# CHANGELOG
#===========
# 2013/03/22    PR442574    Qianjun Zhang
#               /config/dhcp/admin.cfg must be removed 
#
# 2013/03/05    crms00423479    Jerry ZHOU
#               [Regression] No prompt when no Ethernet link in Step2 and VHE stops at 2.5 usb load stage.
#
# 2013/01/24    PR261951        Jerry ZHOU
#               Reduce time of unavailability during boot time
#
# 2012/10/19    CRMS00391792    Ting HE
#               Clear vlan table and cfp rules in initialization 
#
# 2012/08/30    CRMS00376733    QINAJUN ZHANG	
#                after rm dhcp/enet.cfg,we still need one update
#
# 2011/01/06    CRMS00284808    Thanh lam NGUYEN
#               Fix dhcp enet.cfg file removal.
#
# 2010-05-11    crms00223288	Alex CAO
#               File from DHCP not erase after reboot
#
# 2010/05/06    DEMO_MODE       Thanh lam NGUYEN
#               Modification to take into account the demo mode
#
# 2010/04/27    CRMS00209259    Thanh lam NGUYEN
#               Add a message for an invalid netmask.
#
################

. /etc/init.d/rc.config

# determine wether we start only the loopbak or not
: "\$0: $0"
unset STEPS
case "$0" in
    *network.driver)
        push STEPS DRIVER
    ;;
    *network.hw)
        push STEPS HW
    ;;
    *network.mac)
        push STEPS MAC
    ;;
    *network.loopback)
        push STEPS LOOPBACK
    ;;
#PR261951 jerryzh+
    *network.init)
        push STEPS DRIVER HW LOOPBACK
    ;;
#PR261951 jerryzh-
    *network.802.1x)
        push STEPS HW MAC LOOPBACK CHECK_LINK WPA802.1X
    ;;
    *network.network)
        push STEPS HW MAC LOOPBACK CHECK_LINK WPA802.1X ADDR
    ;;
    *network.lldp)
        push STEPS HW MAC LOOPBACK CHECK_LINK WPA802.1X ADDR LLDP
    ;;
#PR261951 jerryzh+
    *)
        push STEPS MAC CHECK_LINK WPA802.1X ADDR LLDP
    ;;
#PR261951 jerryzh-
esac
: "STEPS=[${STEPS:-none}]"

readConfig

# process the command
case "$1" in
    start|"")
        echo $HOSTNAME > /proc/sys/kernel/hostname
        STEPSSAV=$STEPS
        retry=5
        while [[ $retry -gt 0 ]]; do
            STEPS=$STEPSSAV
            while pop STEP STEPS; do
                case "$STEP" in
                    DRIVER) : "DRIVER"
                        # add the driver
                        loadNetworkModules
                        DRIVER_RET=$?
                        [ $DRIVER_RET -eq 0 ] && STEPSAV=${STEPSAV/$STEP} STEPSAV=${STEPSAV//^^/^}
                    ;;
                    HW) : "HW"
                        # set the physical configuration for LAN & PC port
                        configEthHw
                        HW_RET=$?
                        [ $HW_RET -eq 0 ] && STEPSAV=${STEPSAV/$STEP} STEPSAV=${STEPSAV//^^/^}
                    ;;
                    MAC) : "MAC"
                        if ! definedMAC "$ENETCFG_MACADDR"; then
                            attr="$esc[30;41m" display "No mac address was defined for $ENETCFG_INTERFACE\nPlease return the set to the S.A.V."
                            while true; do
                                sleep 1
                            done
                        fi
                        CURRENTMAC=$($busybox ifconfig $ENETCFG_INTERFACE | $busybox sed '/HWaddr/{s/^.*HWaddr[[:blank:]]*//;p};d')
                        CMAC_VALUE=$((0x${CURRENTMAC//:}))
                        EMAC_VALUE=$((0x${ENETCFG_MACADDR//:}))
                        [ "$CMAC_VALUE" == "0" -a "$CMAC_VALUE" != "$EMAC_VALUE" ] && ifconfig $ENETCFG_INTERFACE hw ether $ENETCFG_MACADDR
                        MAC_RET=$?
                        [ $MAC_RET -eq 0 ] && STEPSAV=${STEPSAV/$STEP} STEPSAV=${STEPSAV//^^/^}
                    ;;
                    LOOPBACK) : "LOOPBACK"
                        # Bring the loopback up
                        /sbin/if_setup.sh lo up
                        LOOPBACK_RET=$?
                        [ $LOOPBACK_RET -eq 0 ] && STEPSAV=${STEPSAV/$STEP} STEPSAV=${STEPSAV//^^/^}
                    ;;
                    CHECK_LINK) : "Check link status"
                    #crms00423479 jerryzh+
                        # create rcS pause runnig flag
                        # rcS will pause executing following scripts if this flag exist
                        touch $RCS_PAUSE_FLAG                    
                    #crms00423479 jerryzh-
                        displaymsg "Waiting for the ethernet link..."
                        checkLinkStatus
                        CHECK_LINK_RET=$?
                        displaymsg "Ethernet link found"
                    #crms00423479 jerryzh+
                        # remove rcS pause runnig flag
                        rm -f $RCS_PAUSE_FLAG
                    #crms00423479 jerryzh-
                    ;;
                    WPA802.1X) : "WPA 802.1X"
                        /etc/init.d/wpa start
                        WPA8021X_RET=$?
                    ;;
                    ADDR) : "ADDR"
#PR442574 zqj ++
                        rm -f /config/dhcp/admin.cfg
#PR442574 zqj --
#crms00391792 tingh+
                        /bin/eth vlan en 0                        
#crms00391792 tingh-
                    	TESTINTERFACE=1
                        case "$ENETCFG_DHCP_MODE" in
                            Static) : "Static"
                                ADDR_RET=0
                                check_netmask $ENETCFG_SUBNET || ADDR_RET=$?
# CRMS00209259
                                if [[ $ADDR_RET -ne 0 ]]; then
                                #crms00423479 jerryzh+
                                    # create rcS pause runnig flag
                                    # rcS will pause executing following scripts if this flag exist
                                    touch $RCS_PAUSE_FLAG
                                #crms00423479 jerryzh-
                                    displaymsg "Invalid IP netmask, please correct..."
                                    gotoInfiniteLoop
                                fi
# CRMS00209259 END
                                check_ip $ENETCFG_IPADDR $ENETCFG_SUBNET || ADDR_RET=$?
                                check_ip $ENETCFG_ROUTER $ENETCFG_SUBNET || ADDR_RET=$?
                                check_router $ENETCFG_ROUTER $ENETCFG_IPADDR $ENETCFG_SUBNET || ADDR_RET=$?
                                if [[ $ADDR_RET -ne 0 ]]; then
                                #crms00423479 jerryzh+
                                    # create rcS pause runnig flag
                                    # rcS will pause executing following scripts if this flag exist
                                    touch $RCS_PAUSE_FLAG
                                #crms00423479 jerryzh-
                                    displaymsg "Incoherent IP parameters, please correct..."
                                    gotoInfiniteLoop
                                fi
                            ;;
                            *) : "Dynamic"
                                displaymsg "Waiting for DHCP server..."
                            ;;
                        esac
                        /sbin/if_setup.sh $ENETCFG_INTERFACE up
                        ADDR_RET=$?
                    ;;
                esac
            done
            if [[ ${TESTINTERFACE:-0} -ne 0 ]]; then
                readCurrentConfig
                # interface configured ?
                $busybox ifconfig $ENETCFG_INTERFACE | $busybox grep "inet addr" >/dev/null 2>&1 && break
# DEMO_MODE
                # no, demo mode ?
                [[ "$DEMO_MODE" == "true" ]] && break
# DEMO_MODE END
                # no
                if [ "$ENETCFG_DHCP_MODE" != "Static" -a "$ENETCFG_DHCP_MODE" != "static" ]; then
                    # infinite loop until the device is configured
                    while ! $busybox ifconfig $ENETCFG_INTERFACE | $busybox grep "inet addr" >/dev/null 2>&1; do
	                display "==== Waiting for a DHCP Server... ===="
                        sleep 10
	            done
	            break
                fi
                # no IP address found
	        display "==== Could not initiaze $ENETCFG_INTERFACE retrying ($retry)... ===="
                sleep 1
                retry=$((retry-1))
            else
                break
            fi
        done
        if [[ $retry -eq 0 ]]; then
            attr="$esc[30;41m" display "==== Could not initialize $ENETCFG_INTERFACE ===="
            displaymsg "Could not initialize network interface"
        fi
    ;;
    stop)
        # Stop only real interfaces, not the loopback
        if [ -x /sbin/if_setup.sh ]; then
            /sbin/if_setup.sh $ENETCFG_INTERFACE down
        fi
        /etc/init.d/wpa stop
	killall lldpagent 2>/dev/null
    ;;
    *)
        echo "Usage $0 {start|stop}" >&2
        exit 1
    ;;
esac
