#!/bin/sh	
# Network Interface Configuration System
# Copyright (c) 1996-2001 Red Hat, Inc. all rights reserved.
#
# This software may be freely redistributed under the terms of the GNU
# public license.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

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

CONFIG=${1}

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

[ -f "${CONFIG}" ] || {
    echo $"$0: configuration for ${1} not found." >&2
    echo $"Usage: ifup <device name>" >&2
    exit 1
}

. ./${CONFIG}

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

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

if [ "${BOOTPROTO}" = "bootp" -o "${BOOTPROTO}" = "dhcp" ]; then
    DYNCONFIG=true
fi

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

# Remove any temporary references which were previously added to dhclient config
#if [ -w /etc/dhclient-${DEVICE}.conf ] && [ -x /sbin/dhclient ] ; then
#   grep -v "# temporary RHL ifup addition" /etc/dhclient-${DEVICE}.conf > /etc/dhclient-${DEVICE}.conf.ifupnew 2> /dev/null
#   cat /etc/dhclient-${DEVICE}.conf.ifupnew > /etc/dhclient-${DEVICE}.conf
#   rm -f /etc/dhclient-${DEVICE}.conf.ifupnew
#fi

if [ -n "${DYNCONFIG}" ]; then
    DHCPCDARGS="$DHCPCDARGS -n -t 18"
    
    if [ -n "${DHCP_HOSTNAME}" ]; then
       DHCPCDARGS="${DHCPCDARGS} -h ${DHCP_HOSTNAME}"
    fi

#    if need_hostname; then
#       # Set hostname of host to host-name option supplied by DHCP.
#       DHCPCDARGS="${DHCPCDARGS} -H"
#    fi

    if [ "${PEERDNS}" = "no" ]; then
       # Do not update/replace resolv.conf.
       DHCPCDARGS="${DHCPCDARGS} -R"
    fi
#    echo
#    echo -n $"Determining IP information for ${DEVICE}..."

#    if check_link_down ${DEVICE}; then
#	echo $" failed; no link present.  Check cable?"
#	ip link set ${DEVICE} down >/dev/null 2>&1
#	exit 1
#    fi

    /sbin/dhcpcd ${DHCPCDARGS} ${DEVICE}
    

    # DHCP likes to create duplicate routes.  Fix that up.
#    NUMDEFROUTES=`ip -o route | \
#                  grep "^default" | \
#                  awk '{ nlines++ } END { print nlines }'`
#    if [ -n "$NUMDEFROUTES" -a "$NUMDEFROUTES" -gt 1 ]; then
#	# remove the default route for the new device (old route wins)
#	ip route del default dev ${DEVICE}
#    fi

# end dynamic device configuration
else
    if [ ! -z "${IPADDR}" ]; then
         ifconfig ${DEVICE} ${IPADDR}
    fi

    if [ ! -z "${NETMASK}" ]; then
         ifconfig ${DEVICE} netmask ${NETMASK}
    fi

# ] -a "${GATEWAYDEV}" = "${DEVICE}"
    # Set a default route.
    if [ ! -z "${GATEWAY}" ]; then
	    route add default gw ${GATEWAY} dev ${DEVICE}
    fi
fi

need_hostname ()
{
    CHECK_HOSTNAME=`hostname`
    if [ "$CHECK_HOSTNAME" = "(none)" -o "$CHECK_HOSTNAME" = "localhost" -o \
	"$CHECK_HOSTNAME" = "localhost.localdomain" ]; then
	return 0
    else
	return 1
    fi
}
