#!/bin/sh

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

    # remove the nameserver file
    rm -f /etc/resolv.conf

    #
    # set up the network interfaces
    #
    if [ "$SYSCFG_IFACE0" = "y" ]
    then
        echo "Setting up networking on $INTERFACE0: "

        ifconfig $INTERFACE0 up

        if [ "$IPADDR0" = "" ]
        then
            echo ""
            echo "Warning: no IPADDR is set it in /etc/rc.d/rc.conf."
            echo "IP address setup bypassed"
            echo ""
            return
        fi
        if [ "$IPADDR0" = "dhcp" ]
        then
            NFSBOOT="`cat /proc/cmdline | grep -q /dev/nfs ; echo $?`"
            if [ "$NFSBOOT" == "0" ]
            then
                echo "You need to manually set your nameserver in /etc/resolv.conf"
            else
                $SYSCFG_DHCPC_CMD $DHCP_ARG $INTERFACE0
            fi
        else  
            # non-dhcp network startup
            ifconfig $INTERFACE0 $IPADDR0 netmask $NETMASK0
            sed -e 's,.*hostname,'$IPADDR0'     '`hostname`',' /etc/hosts >/tmp/hosts
            mv /tmp/hosts /etc/hosts

            if [ -n "$GATEWAY0" ]
            then 
                echo "Adding static route for default gateway to $GATEWAY0: "
                route add default gateway $GATEWAY0 $INTERFACE0
                sed -e 's,.*gateway0,'$GATEWAY0'     gateway0,' /etc/hosts >/tmp/hosts
                mv /tmp/hosts /etc/hosts
            fi
            if [ -n "$NAMESERVER0" ]
            then
                echo "Setting nameserver to $NAMESERVER0 in /etc/resolv.conf: "
                echo "nameserver $NAMESERVER0" >> /etc/resolv.conf
            fi
        fi
    fi
fi
