#!/bin/sh

if [ "$#" -lt 1 ] ; then
        echo "Invalid arguments."
        exit 1
fi

IFCNAME=$1

IFCBASE=/etc/sysconfig/network-scripts
IFCFILE=$IFCBASE/ifcfg-$IFCNAME
IFCETHTOOLFILE=$IFCBASE/ethtool-$IFCNAME

if [ ! -f $IFCFILE ]; then
        echo "Interface configuration file for $IFCNAME does not exist."
        exit 2
fi

. $IFCFILE

if [ "$ONBOOT" != "yes" ] ; then
        echo "Interface $IFCNAME is disabled."
        exit 0
fi

if [ "$TYPE" = "Ethernet" ] ; then
    if [ ! -f $IFCETHTOOLFILE ]; then
        echo "Interface ethtool option file for $IFCNAME does not exist."
        exit 2
    fi

    . $IFCETHTOOLFILE

    if [ "$ETHTOOL_OPTS" != "" ] ; then
        ethtool -s $IFCNAME $ETHTOOL_OPTS
    fi
fi

. $IFCFILE
if [ "$BOOTPROTO" = "static" ] ; then
	if [ -n "$IPADDR" ] && [ "$IPADDR" != "0.0.0.0" ] ; then
        	ifconfig $IFCNAME $IPADDR netmask $NETMASK up
		arping -I $IFCNAME -U $IPADDR -c1
	else
	        /sbin/start_autoipd $IFCNAME 0 &
	fi
elif [ "$BOOTPROTO" = "dhcp" ] || [ "$BOOTPROTO" = "bootp" ] ; then
        if [ ! -x /sbin/udhcpc ] ; then
                echo "DHCP client udhcpc not found."
                exit 3
        fi
        if [ -n "$DHCP_CLIENTID" ] ; then
        	if [ -n "$HOSTNAME" ] ; then
	        	udhcpc -c "'$DHCP_CLIENTID'" -h "$HOSTNAME" -R -t 5 -A 10 -S -s /usr/share/udhcpc/ltrx-dhcp.script -p /var/run/udhcpc.$IFCNAME.pid -i $IFCNAME &
	        else
        		udhcpc -c "'$DHCP_CLIENTID'" -R -t 5 -A 10 -S -s /usr/share/udhcpc/ltrx-dhcp.script -p /var/run/udhcpc.$IFCNAME.pid -i $IFCNAME &
		fi
	else
                if [ -n "$HOSTNAME" ] ; then
        		udhcpc -h "$HOSTNAME" -R -t 5 -A 10 -S -s /usr/share/udhcpc/ltrx-dhcp.script -p /var/run/udhcpc.$IFCNAME.pid -i $IFCNAME &
                else
        		udhcpc -R -t 5 -A 10 -S -s /usr/share/udhcpc/ltrx-dhcp.script -p /var/run/udhcpc.$IFCNAME.pid -i $IFCNAME &
                fi
        fi
        /sbin/start_autoipd $IFCNAME 4 &
else
        echo "Invalid boot protocol $BOOTPROTO"
        exit 4
fi

exit 0
