#!/bin/bash
PATH=/sbin:/usr/sbin:/bin:/usr/bin
# 06-feb-2004, Berend-Jan Wever: Changed dhcp to use dhclient instead of pump

cd /etc/sysconfig/network-scripts
. network-functions
. /etc/sysconfig/network

need_hostname

CONFIG=$1

[ -z "$CONFIG" ] && {
    echo "usage: ifup <device name>" >&2
    exit 1
}

[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
[ -f "$CONFIG" ] || {
    echo "usage: ifup <device name>" >&2
    exit 1
}

if [ $UID != 0 ]; then
    if [ -x /usr/sbin/usernetctl ]; then
	if /usr/sbin/usernetctl $CONFIG report ; then
	    exec /usr/sbin/usernetctl $CONFIG up
	fi
    fi
    echo "Users cannot control this device." >&2
    exit 1
fi

# preserve GATEWAY from /etc/sysconfig/network 
# GATEWAY  may be overrided by fcfg-* and is neccesary later 
MAIN_GATEWAY=${GATEWAY}
GATEWAY=

source_config

if [ "foo$2" = "fooboot" -a "${ONBOOT}" = "no" -o "${ONBOOT}" = "NO" ]
then
    exit
fi

IPSETUP=no

DEVICETYPE=`echo $DEVICE | sed "s/[0-9]*$//"`
REALDEVICE=`echo $DEVICE | sed 's/:.*//g'`
if echo $DEVICE | grep -q ':' ; then
    ISALIAS=yes
else
    ISALIAS=no
fi

# Old BOOTP variable
if [ "$BOOTP" = "yes" ]; then
    BOOTPROTO=bootp
fi

OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${DEVICETYPE}"

if [ -x $OTHERSCRIPT ]; then
    exec $OTHERSCRIPT $CONFIG $2
fi

# 802.1q part
if [ -x /usr/sbin/vconfig -a "$VLAN" = "yes" -a "$ISALIAS" = "no" ]; then

    # DEV_PLUS_VID* type
    if echo ${DEVICE} | LANG=C egrep -q '(eth|bond)[0-9]+\.[0-9]{1,4}($|[^:])'; then
        VID="`echo ${DEVICE} | \
            LANG=C egrep '(eth|bond)[0-9]+\.[0-9]{1,4}($|[^:])' | \
            LANG=C sed 's/^[a-z0-9]*\.//g;s/^0*//'`"
        PHYSDEV="`echo ${DEVICE} | \
            LANG=C egrep '(eth|bond)[0-9]+\.[0-9]{1,4}($|[^:])' | \
            LANG=C sed 's/\.[a-z0-9]*$//g'`"
    fi

    # VLAN_PLUS_VID* type
    if echo ${DEVICE} | LANG=C egrep -q 'vlan[0-9]{1,4}'; then
        VID="`echo ${DEVICE} | LANG=C sed 's/^vlan//;s/^0*//'`"
        VLAN_NAME_TYPE=VLAN_PLUS_VID_NO_PAD
        # PHYSDEV should be set in ifcfg-vlan* file
        if [ -z "$PHYSDEV" ]; then
            echo "PHYSDEV should be set for device ${DEVICE}"
            exit 1
        fi
    fi

    if [ -n "$VID" ]; then
        # standard Owl kernel has vlan module compiled-in, this is redundant
        if [ ! -d /proc/net/vlan ]; then
            if ! modprobe 8021q >/dev/null 2>&1 ; then
                echo "No 802.1Q VLAN support available in kernel for device ${DEVICE}"
                exit 1
            fi
        fi

        # default mode for vlan devices in rh-compatible scripts
        test -z "$VLAN_NAME_TYPE" && VLAN_NAME_TYPE=DEV_PLUS_VID_NO_PAD
        /usr/sbin/vconfig set_name_type "$VLAN_NAME_TYPE" >/dev/null
        [ $? -gt 0 ] && echo "Could not set 802.1Q VLAN parameters."

        ifconfig ${PHYSDEV} 2>/dev/null | grep -q ${PHYSDEV}
        if [ $? -gt 0 ]; then
            echo "Physical device ${PHYSDEV} for ${DEVICE} does not seem to be present."
            exit 1
        fi

        # Link on Physical device needs to be up but no ip required
        /sbin/ifconfig ${PHYSDEV} 2>/dev/null | grep -q UP 
        [ $? -gt 0 ] && /sbin/ifconfig ${PHYSDEV} up

        if [ ! -f /proc/net/vlan/${DEVICE} ]; then
            /usr/sbin/vconfig add ${PHYSDEV} ${VID} 
            if [ $? -gt 0 ]; then
                echo "ERROR: could not add vlan ${VID} as ${DEVICE} on dev ${PHYSDEV}"
                exit 1
            fi
        else
            case "$REORDER_HDR" in
                yes|1)
                    /usr/sbin/vconfig set_flag ${DEVICE} 1 1 
                    ;;
                no|0)
                    /usr/sbin/vconfig set_flag ${DEVICE} 1 0 
                    ;;
            esac
        fi
    fi
fi
# end of 802.1q part

# is this device available? (this catches PCMCIA devices for us)
/sbin/ifconfig ${REALDEVICE} 2>&1 | grep -s "not found" > /dev/null
if [ "$?" = "0" ]; then
    echo "Delaying ${DEVICE} initialization."
    exit 1
fi

if [ "$SLAVE" = yes -a "$ISALIAS" = no -a "$MASTER" != "" -a \
     -x /sbin/ifenslave ]; then
    RFLAG="" ; [ "$RECEIVE-ONLY" = yes ] && RFLAG="-r"

    ifconfig ${DEVICE} down
    echo "Enslaving $DEVICE to $MASTER"
    ifenslave $RFLAG "$MASTER" "$DEVICE"

    exit 0
fi

if [ -n "$MACADDR" ]; then
   ifconfig ${DEVICE} hw ether ${MACADDR}
fi

if [ "$BOOTPROTO" = "dhcp" ]; then
    DHCLIENTARGS=
    if [ -n "$DHCP_PORT" ]; then
        DHCLIENTARGS=="-p $DHCP_PORT"
    fi
    if [ -n "$DHCP_DESTINATION" ]; then
        DHCLIENTARGS=="-s $DHCP_DESTINATION $DHCLIENTARGS"
    fi
    if [ -n "$DHCP_RELEASE" ]; then
        DHCLIENTARGS=="-r $DHCP_RELEASE $DHCLIENTARGS"
    fi
    echo -n "Requesting DHCP information for $DEVICE..."
    if ifconfig $DEVICE up && /sbin/dhclient -q $DEVICE $DHCLIENTARGS ; then
        echo " done."
    else
        ifconfig $DEVICE down
        echo " failed."
        exit 1
    fi
elif [ "$BOOTPROTO" = "bootp" ]; then
    PUMPARGS=
    if [ -n "$DHCP_HOSTNAME" ]; then
       PUMPARGS="-h $DHCP_HOSTNAME"
    fi
    echo -n "Determining IP information for $DEVICE..."
    if /sbin/pump $PUMPARGS -i $DEVICE ; then
	echo " done."
    else
	echo " failed."
	exit 1
    fi
else 
    if [ -z "$NETMASK" ]; then
	eval `/bin/ipcalc --netmask ${IPADDR}`
    fi

    if [ -z "$BROADCAST" ]; then
	eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
    fi

    if [ -z "$NETWORK" ]; then
	eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
    fi

    ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST} ${ARP:+arp}
    # don't re-add subnet route on 2.2 kernels, but add a route
    # to a non-local subnet.
    # stupid hack, but it should work
    if [ "$ISALIAS" = no ] && [ -z "`route -n | sed "s/ .*//" | grep ${NETWORK}`" ]; then
	route add -net ${NETWORK} netmask ${NETMASK} dev ${DEVICE}
    else
	route add -host ${IPADDR} ${DEVICE}
    fi

    # this is broken! it's only here for compatibility with old RH systems
    if [ "${GATEWAY}" != "" -a "${GATEWAY}" != "none" ]; then
	route add default gw ${GATEWAY} metric 1 ${DEVICE}
    fi

    GATEWAY=${MAIN_GATEWAY}
    if [ "${GATEWAYDEV}" = "" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
	# set up default gateway
	if [ "${GATEWAY}" != "" ]; then
	    route add default gw ${GATEWAY} ${DEVICE}
	    DEFGW=${GATEWAY}
	elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
	    route add default ${DEVICE}
	fi
    fi
fi

if [ "$IPX" = yes ]; then
	/etc/sysconfig/network-scripts/ifup-ipx $DEVICE
fi

exec /etc/sysconfig/network-scripts/ifup-post $CONFIG $2
