#!/bin/sh
#
#	$Id: ifup,v 1.66 2001/10/11 18:49:00 baggins Exp $
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/.functions

# Will be removed in the future
if is_yes "$NETWORKING" ; then
	if [ -z "$IPV4_NETWORKING" ] ; then
		echo "NETWORKING is set to YES, but IPV4_NETWORKING is empty!"
		echo "Please upgrade your config"
		echo "Assuming you want IPv4 networking"
		IPV4_NETWORKING=yes
		export IPV4_NETWORKING
	fi
fi

need_hostname

# device name must be ifcfg-somename
DEV=$1

[ -z "$DEV" ] && {
    nls "Usage: %s <device name>" "ifup" >&2
    exit 1
}

if [ `id -u` != 0 ]; then
    if [ -x /sbin/usernetctl ]; then
	exec /sbin/usernetctl $DEV up
    fi
    nls "Users cannot control this device." >&2
    exit 1
fi

if [ -f "/etc/sysconfig/interfaces/ifcfg-$DEV" ] ; then
	CONFIG="/etc/sysconfig/interfaces/ifcfg-$DEV"
else
	CONFIG="$DEV"
fi

source_config

if [ "foo$2" = "fooboot" ] && is_no "${ONBOOT}"; then
    exit
fi

IPSETUP=no

# set all major variables
setup_ip_param

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

if [ -x "$OTHERSCRIPT" ]; then
    if is_yes "$DEFAULTHANDLING" ; then HANDLING=1 ; fi
    if [ "$HANDLING" = "0" ]; then
    	exec $OTHERSCRIPT $CONFIG $2
    elif [ "$HANDLING" = "1" ]; then
        $OTHERSCRIPT $CONFIG $2
    fi
fi

# 802.1Q VLANs
vlan_setup
if is_yes $ETH_VLANS && echo "$DEVICE" | grep -q '\.' ; then
	VLANDEVICE=$(echo $DEVICE | awk ' { gsub(/\..*$/,NUL); print $0 } ')
	VLANNUMBER=$(echo $DEVICE | awk " { gsub(/$VLANDEVICE\./,NUL); print \$0 } ") 
	/sbin/ip link set $VLANDEVICE up
	/sbin/vconfig add $VLANDEVICE $VLANNUMBER 2>&1 > /dev/null
	/sbin/vconfig set_flag $DEVICE 1 2>&1 > /dev/null
fi

# is this device available? (this catches PCMCIA devices for us)
if ! (/sbin/ip link set multicast ${MULTICAST} dev ${DEVICE} > /dev/null 2>&1); then
    nls "Delaying %s initialization" ${DEVICE}
    exit 1
fi

if is_yes "$SLAVE" && [ -n "$MASTER" -a -x /sbin/ifenslave ]; then
    RFLAG="" && is_yes "${RECIEVE-ONLY}" && RFLAG="-r"

    ip link set ${DEVICE} down
    nls "Enslaving %s to %s" "$DEVICE" "$MASTER"
    ifenslave $RFLAG "$MASTER" "$DEVICE"

    exit 0
fi

if [ -n "$MACADDR" ]; then
   ip link set ${DEVICE} address ${MACADDR}
fi

ip link set ${DEVICE} multicast ${MULTICAST} ${ARP} down

if [ -n "$MTU" ]; then
    ip link set ${DEVICE} mtu ${MTU}
fi

if is_yes "${IPV6_NETWORKING}" && is_yes "${IPV6_DISABLE_AUTOCONF}"; then
	run_cmd "Disabling IPv6 autoconfiguration" sysctl -w net.ipv6.conf.${DEVICE}=0
fi

if is_wireless_device "${DEVICE}"; then
    [ -z "${WLAN_NICKNAME}" ] && WLAN_NICKNAME="$(hostname -f)"

    # wlan mode needs to be first
    [ -n "${WLAN_MODE}" ] && iwconfig ${DEVICE} mode ${WLAN_MODE}
    iwconfig ${DEVICE} nickname ${WLAN_NICKNAME}
    [ -n "${WLAN_ESSID}" ] && iwconfig ${DEVICE} essid ${WLAN_ESSID}
    [ -n "${WLAN_NWID}" ] && iwconfig ${DEVICE} nwid ${WLAN_NWID}
    if [ -n "${WLAN_FREQ}" -a "${WLAN_MODE}" != "Managed" ] ; then
        iwconfig ${DEVICE} freq ${WLAN_FREQ}
    elif [ -n "${WLAN_CHANNEL}" -a "${WLAN_MODE}" != "Managed" ]; then
        iwconfig ${DEVICE} channel ${WLAN_CHANNEL}
    fi
    [ -n "${WLAN_SENS}" ] && iwconfig ${DEVICE} sens ${WLAN_SENS}
    [ -n "${WLAN_AP}" ] && iwconfig ${DEVICE} ap ${WLAN_AP}
    [ -n "${WLAN_BITRATE}" ] && iwconfig ${DEVICE} rate ${WLAN_BITRATE}
    [ -n "${WLAN_RTS_THRESHOLD}" ] && iwconfig ${DEVICE} rts ${WLAN_RTS_THRESHOLD}
    [ -n "${WLAN_FRAGMENTATION_THRESHOLD}" ] && iwconfig ${DEVICE} frag ${WLAN_FRAGMENTATION_THRESHOLD}
    if [ -n "${WLAN_KEY}" ]; then
        iwconfig ${DEVICE} key ${WLAN_KEY}
        [ -n "${WLAN_ENCRYPTION}" ] && iwconfig ${DEVICE} key ${WLAN_ENCRYPTION}
    fi
    if is_no "${WLAN_ENCRYPTION}"; then
        iwconfig ${DEVICE} key off
    fi
    [ -n "${WLAN_POWER}" ] && iwconfig ${DEVICE} ${WLAN_POWER}
    [ -n "${WLAN_TXPOWER}" ] && iwconfig ${DEVICE} ${WLAN_TXPOWER}
    [ -n "${WLAN_RETRY}" ] && iwconfig ${DEVICE} ${WLAN_RETRY}
    if is_yes "${WLAN_COMMIT}"; then
        iwconfig ${DEVICE} commit
    fi
    [ -n "${WLAN_NWID}" ] && iwconfig ${DEVICE} nwid ${WLAN_NWID}
    # essid should be last due to netowork rescanning by wlan devices
    [ -n "${WLAN_ESSID}" ] && iwconfig ${DEVICE} essid ${WLAN_ESSID}
fi
	
if [ "$HANDLING" = "2" ]; then
	exit 0
fi

if [ -n "$BOOTPROTO" -a "$BOOTPROTO" != "none" ]; then

    if is_yes "$IPV4_NETWORKING"; then
	if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
		DHCP_CLIENT=/sbin/pump
	fi
    
	if [ -z "$DHCP_CLIENT" ]; then
		if [ -x /sbin/dhcpcd ]; then
	       		DHCP_CLIENT=/sbin/dhcpcd
		elif [ -x /sbin/dhclient ]; then
			DHCP_CLIENT=/sbin/dhclient
		elif [ -x /sbin/dhcpxd ]; then
			DHCP_CLIENT=/sbin/dhcpxd
		elif [ -x /sbin/pump ]; then
			DHCP_CLIENT=/sbin/pump
		else
			nls "Can't find a dhcp client."
			exit 1
		fi
	fi
	
	DHCP_ARGS=
	if [ -n "$DHCP_CLIENT" ]; then
		case $(basename $DHCP_CLIENT) in
			pump)
			[ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-h $DHCP_HOSTNAME"
			DHCP_ARGS="$DHCP_ARGS -i $DEVICE"
			;;
			dhcpcd)
			[ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-h $DHCP_HOSTNAME"
			DHCP_ARGS="$DHCP_ARGS $DEVICE"
			;;
			dhcpxd)
			[ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-H $DHCP_HOSTNAME"
			DHCP_ARGS="$DHCP_ARGS $DEVICE"
			;;
			dhclient)
			# Can't specify a host with dhclient ?
			DHCP_ARGS="$DEVICE"
			;;
		esac
	fi
	DHCP_ARGS="$DHCP_OPTIONS $DHCP_ARGS"
	
	if ! run_cmd "$(nls 'Determining IP information for') $DEVICE ($(basename $DHCP_CLIENT))" $DHCP_CLIENT $DHCP_ARGS ; then
		exit 1
	fi

	# give time for determining IP information
	check_device_down && sleep 1 && check_device_down && sleep 3
	check_device_down && ip link set ${DEVICE} up
    fi

else

    # IPv4 in use ?
    if is_yes "$IPV4_NETWORKING" && [ -n "${IP4ADDR}" ] ; then
	# If broadcast is missing then autocalculate it
	if ! (echo $IP4ADDROPT | egrep -q "brd|broadcast"); then
	    IP4ADDROPT="brd + ${IP4ADDROPT}"
	fi

	if [ -n "$REMIP" ]; then
	    ip addr add ${IP4ADDR} peer ${REMIP} dev ${DEVICE} ${IP4ADDROPT}
	else
	    ip addr add ${IP4ADDR} dev ${DEVICE} ${IP4ADDROPT}
	fi
    fi

    # Set device up
    ip link set ${DEVICE} up

    # IPv6 in use ?
    if is_yes "$IPV6_NETWORKING" && [ -n "${IP6ADDR}" ] ; then
    	ip addr add ${IP6ADDR} dev ${DEVICE} ${IP6ADDROPT}
    fi

    # IPv4/6 gateways and default routes
    setup_ip_gw_ro
fi


if [ "$HANDLING" = "3" ]; then
	exit 0
fi

if is_yes "$IPX"; then
	/etc/sysconfig/network-scripts/ifup-ipx $DEVICE
fi

if [ "$HANDLING" = "4" ]; then
	exit 0
fi

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

# This must be last line !
# vi:syntax=sh:tw=78:ts=8:sw=4
