#!/bin/bash
# Copyright (c) 2003-2011 by Silvan Calarco <silvan.calarco@mambasoft.it>
# Copyright (c) 2003-2009 by Davide Madrisan <davide.madrisan@gmail.com>

. /etc/sysconfig/rc
. $rc_functions
. $rc_networkfunctions

DEVICE=${1}

[ "${DEVICE}" ] || exit 1

[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -r $network_devices/ifcfg-${DEVICE} ] && . $network_devices/ifcfg-${DEVICE}

[ "$2" = "--udev" -a "$ONBOOT" != "yes" -a "$ONBOOT" != "1" -a "$ONBOOT" != "true" ] \
   && exit 0

# udev boot: exit if root filesystem is read only
[ "$2" = "--udev" -a ! -w /var/run ] && exit 0

if [ -x $network_devices/ifup-${DEVICE} ]; then
   $network_devices/ifup-${DEVICE}
   exit 0
fi

[ -r $network_devices/ifup-wireless ] && . $network_devices/ifup-wireless

if [ "$BOOTPROTO" = "dhcp" ]; then
   if [ ! -r /var/run/dhclient.${DEVICE}.pid ]; then
      echo -n "Bringing up the ${DEVICE} interface: "
      /sbin/ifconfig ${DEVICE} up
      dhclient -q ${DEVICE} -nw -pf /var/run/dhclient.${DEVICE}.pid >/dev/null
      evaluate_retval
      echo
   else
      echo -n "Error: dhclient is already running for the interface"
   fi
else
   [ "$IPADDR" ] || IPADDR=$IP

   if [ "$IPADDR" ]; then
      if [ -z "$NETMASK" ]; then
         echo "\
NETMASK variable missing in ifcfg-${DEVICE}, echo using 255.255.255.0"
         NETMASK=255.255.255.0
      fi

      [ "$BROADCAST" ] || eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`

      echo -n "Bringing up the ${DEVICE} interface: "
      /sbin/ifconfig ${DEVICE} $IPADDR netmask $NETMASK broadcast $BROADCAST
      evaluate_retval
      echo
   else
      # if IPADDR is missing interface is brought up anyway without address
      echo -n "Bringing up the ${DEVICE} interface with no address: "
      /sbin/ifconfig ${DEVICE} up
      evaluate_retval
      echo
   fi

   if [ -n "$ETHTOOL_OPTS" ] ; then
      /usr/sbin/ethtool -s ${DEVICE} $ETHTOOL_OPTS
   fi

   if [[ "$GATEWAY_IF" = "${DEVICE}" && -n "$GATEWAY" ]]; then
      echo -n "Setting up default gateway: "
      route add default gateway $GATEWAY metric 1 dev $GATEWAY_IF
      evaluate_retval
      echo
   fi
fi

[ -r $network_devices/ifup-routes ] && . $network_devices/ifup-routes ${DEVICE}

exit 0