#!/bin/sh

HWADD_CFG=/etc/rc.d/rc.network.hardware.conf
if [ -f "$HWADD_CFG" ]; then
    source $HWADD_CFG
fi

if [ -d /sys/class/net/eth1 ]; then       
    export INTERFACE0="eth1"       
    /sbin/ifconfig eth0 down &>/dev/null
fi                                    

process_udhcpc () {
    udhcpc_command_string="$SYSCFG_DHCPC_CMD$1"
    udhcpc_pids=$(pgrep -f "$udhcpc_command_string")

    udhcpc_pids_count=0;
    # restart every udhcpc instance
    for i in $udhcpc_pids
    do
        #send SIGUSR1 to udhcpc to force a renew state
        kill -SIGUSR1 $i
        udhcpc_pids_count=$((udhcpc_pids_count+1))
    done

    if [ $udhcpc_pids_count -gt 0 ]
    then
        echo $SYSCFG_DHCPC_CMD $1 is already running
    else
        $SYSCFG_DHCPC_CMD $1
    fi
}

get_interface_priority()
{
  awk -v IFACE=$1 -v TARGET=metrics -F ' *= *' '
  {
    if ($0 ~ /^\[.*\]$/) {
      gsub(/^\[|\]$/, "", $0)
      SECTION=$0
    } else if (($1 == IFACE) && (SECTION==TARGET)) {
      print $2
    }
  }
  ' /etc/network-manager.ini
}


if [ ! -x /sbin/ifconfig ]
then
    echo "/sbin/ifconfig missing, unable to configure the network"
    exit 0
fi

if [ "$1" = "start" -o "$1" = "restart" ]
then
    if [ -f /etc/sysctl.conf -a -x /sbin/sysctl ]
    then
        echo "Running sysctl"
        sysctl -p /etc/sysctl.conf >/dev/null 2>&1
    fi

    echo "Setting up networking on loopback device: "
    ifconfig lo 127.0.0.1
    route add -net 127.0.0.0 netmask 255.0.0.0 lo

    lch=$( grep "^127.0.0.1     localhost$" /etc/hosts )
    if [ -z "$lch" ]
    then
        echo "127.0.0.1       localhost" > /etc/hosts
        sync
    fi

    # remove configuration 
    rm -f /etc/resolv.conf
    rm -rf /run/resolvconf
    #
    # set up the network interfaces
    #
    if [ "$IPADDR0" = "" ]
    then
        echo ""
        echo "Warning: no IPADDR is set, please set this from the ltib"
        echo "config screen, or directly in /etc/rc.d/rc.conf."
        echo "IP address setup bypassed"
        echo ""
        sleep 2
    fi

    # if rootfs is mounted via NFS, DEPLOYMENT_STYLE is NFS regardless
    # whether LTIB built it for JFFS2.
    if grep -q 'root=/dev/nfs' /proc/cmdline
    then
        DEPLOYMENT_STYLE=NFS;
    fi


    if [ "$SYSCFG_IFACE0" = "y" ] && [ -d /sys/class/net/$INTERFACE0 ]
    then
        echo -n "Setting MAC Address $HWADDR0 for $INTERFACE0 : "
        if [ ! -z "$HWADDR0" ]
        then
            ifconfig $INTERFACE0 hw ether $HWADDR0
            echo " Done"
        else
            HW=$(ifconfig -a $INTERFACE0 | head -n 1 | awk -F "HWaddr" '{ print $2 }' | cut -d' ' -f 2)
            if [ ! "$HW" = "" ]; then
                echo "export HWADDR0=$HW" >> $HWADD_CFG
            fi
            echo " Empty. Use default."
        fi

        echo "Setting up networking on $INTERFACE0: "
        if [ "$IPADDR0" = "dhcp" ]
        then
            if [ "$DEPLOYMENT_STYLE" = "NFS" ]
            then
                if [ -f /proc/net/pnp ]
                then
                    if [ -x /sbin/resolvconf ]; then
                         grep nameserver /proc/net/pnp | resolvconf -a "${INTERFACE0}.udhcpc"
                    else
                         grep nameserver /proc/net/pnp > /etc/resolv.conf
                    fi
                else
                    echo "You need to manually set your nameserver in /etc/resolv.conf"
                fi
            else
                process_udhcpc $INTERFACE0
            fi
        else
            # non-dhcp network startup
            ifconfig $INTERFACE0 $IPADDR0 netmask $NETMASK0
            if [ -n "$GATEWAY0" ]
            then
                echo "Adding static route for default gateway to $GATEWAY0: "
                metric=$(get_interface_priority $INTERFACE0)
                route add default gateway $GATEWAY0 dev $INTERFACE0 metric $metric
            fi

            R=""
            for i in "$NAMESERVER00" "$NAMESERVER10"; do
                if [ -n $i ]; then
                    R="${R}nameserver $i\n";
                fi
            done
            if [ -x /sbin/resolvconf ]; then
                echo -n -e "$R" | resolvconf -a "${INTERFACE0}.udhcpc"
            else
                echo -n -e "$R" >> /etc/resolv.conf
            fi
        fi
    fi
    if [ "$SYSCFG_IFACE1" = "y" ] && [ -d /sys/class/net/$INTERFACE1 ]
    then
        echo -n "Setting MAC Address $HWADDR1 for $INTERFACE1 : "
        if [ ! -z "$HWADDR1" ]
        then
            WLAN_DG=/etc/platform/99-wlan-downgrade
            sed -i '/HWADDR1/d' $HWADD_CFG
            if [ ! -f "$WLAN_DG" ]
            then
               echo "#!/bin/sh" >> $WLAN_DG
               echo >> $WLAN_DG
               echo "# This is an auto-generated file for solving of the release upgrading/downgrading problems" >> $WLAN_DG 
               echo "# caused by the change of the Espressif firmware version ( wlan ) and therefore a change in" >> $WLAN_DG 
               echo "# the OUI part of the MAC address." >> $WLAN_DG
               echo >> $WLAN_DG

               echo "sed -i '/HWADDR1/d' $HWADD_CFG" >> $WLAN_DG
               chmod a+x $WLAN_DG
            fi
            echo " Done"
        fi
    fi
    if [ "$SYSCFG_IFACE2" = "y" ] && [ -d /sys/class/net/$INTERFACE2 ]
    then
        echo "Setting up networking on $INTERFACE2: "
        state=`cat /sys/class/net/$INTERFACE2/operstate`
        if [ "$state" = "up" ]; then
            ifconfig "$INTERFACE2" "$IPADDR2" netmask "$NETMASK2" up
            if [ -n "$GATEWAY2" ]; then
                echo "Adding static route for default gateway to $GATEWAY2: "
                metric=$(get_interface_priority "$INTERFACE2")
                ip route add default via "$GATEWAY2" dev "$INTERFACE2" proto static metric $metric >& /dev/null
            fi
        fi
        if [ -n "$NAMESERVER02" ]; then
            if [ -x /sbin/resolvconf ]; then
                echo -n -e "nameserver $NAMESERVER02" | resolvconf -a "${INTERFACE2}.udhcpc"
            else
                echo "nameserver $NAMESERVER02" >> /etc/resolv.conf
            fi
        fi
    fi
    if [ "$SYSCFG_IFACE3" = "y" ] && [ -d /sys/class/net/$INTERFACE3 ]
    then
        echo "Setting up networking on $INTERFACE3: "
        if [ "$IPADDR3" = "dhcp" ]
        then
            process_udhcpc $INTERFACE3
        else
            # non-dhcp network startup
            ifconfig $INTERFACE3 $IPADDR3 netmask $NETMASK3

            if [ -n "$GATEWAY3" ]
            then
                echo "Adding static route for default gateway to $GATEWAY3: "
                metric=$(get_interface_priority "$INTERFACE3")
                route add default gateway $GATEWAY3 dev $INTERFACE3 metric $metric
            fi
        fi
    fi
    if [ "$SYSCFG_IFACE4" = "y" ] && [ -d /sys/class/net/$INTERFACE4 ]
    then
        echo "Setting up networking on $INTERFACE4: "
        if [ "$IPADDR4" = "dhcp" ]
        then
            process_udhcpc $INTERFACE4
        else
            # non-dhcp network startup
            ifconfig $INTERFACE4 $IPADDR4 netmask $NETMASK4

            if [ -n "$GATEWAY4" ]
            then
                echo "Adding static route for default gateway to $GATEWAY4: "
                metric=$(get_interface_priority "$INTERFACE4")
                route add default gateway $GATEWAY4 dev $INTERFACE4 metric $metric
            fi
        fi
    fi
    # enforce /etc/resolv.conf update
    if [ -x /sbin/resolvconf ]; then
         resolvconf -u
    fi
fi

