#!/bin/sh
# Begin services/dhclient

# Origianlly based upon lfs-bootscripts-1.12 $NETWORK_DEVICES/if{down,up}
# Rewritten by Nathan Coulson <nathan@linuxfromscratch.org>
# Adapted for dhclient by DJ Lucas <dj@linuxfromscratch.org>
# Update for LFS 7.0 by Ken Moffat <ken@linuxfromscratch.org>

# Call with: IFCONFIG=<filename> /lib/services/dhclient <IFACE> <up | down>

#$LastChangedBy: bdubbs $
#$Date: 2014-08-27 13:01:33 -0500 (Wed, 27 Aug 2014) $

. /lib/lsb/init-functions
. $IFCONFIG

if [ -f "/etc/sysconfig/dhclient" ]; then
  . /etc/sysconfig/dhclient
  fi

if [ -n "${PREFIX6}" ] ; then
  setlength6="--address-prefix-len ${PREFIX6}"
  fi


PIDFILE=/run/dhclient-$1.pid
LFILE=/var/lib/dhclient/dhclient-$1.leases
PIDFILE6=/run/dhclient6-$1.pid
LFILE6=/var/lib/dhclient/dhclient6-$1.leases

getipstats()
{
   # Print the last 16 lines of dhclient.leases
   sed -e :a -e '$q;N;17,$D;ba' ${LFILE}
}

# Make compatible with older versions of init-functions
unset is_true

is_true()
{
   [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] ||
   [ "$1" = "y" ] || [ "$1" = "t" ]
}

case "$2" in
  up)

      if [ -n "${HWADDR}" -a "${2}" = "up" ] ; then
	NORMHWADDR=`echo ${HWADDR} | tr '[:upper:]' '[:lower:]'`
  	for unit in `ls /sys/class/net`
    	  do
    	  if [ ! -L /sys/class/net/$unit ] ; then
      	    continue
      	    fi
    	  unithwaddr=`cat /sys/class/net/$unit/address`
    	  if [ "${NORMHWADDR}" = "${unithwaddr}" ] ; then
      	    ip link set $unit name ${1}
      	    break; 
      	    fi
    	  done
	fi

     if [ -e ${PIDFILE} ]; then
        ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
        if [ "$?" = "0" ]; then
           log_warning_msg "dhclient appears to be running on $1"
           exit 0
        else
           rm ${PIDFILE}
        fi
     fi

      log_info_msg "Starting IPV4 dhclient on the $1 interface..."
      /usr/sbin/dhclient					\
		-4						\
		-lf ${LFILE}					\
		-pf ${PIDFILE}					\
	       	$1 > /dev/null
      if [ "$?" != "0" ]; then
        log_failure_msg2
        exit 1
        fi
      log_success_msg2

      # Print the assigned settings if requested
      if  is_true "$PRINTIP"  -o  is_true "$PRINTALL"; then
        # Get info from dhclient.leases file

        IPADDR=`getipstats | grep "fixed-address" | \
          sed 's/ fixed-address //' | \
          sed 's/\;//'`

        NETMASK=`getipstats | grep "subnet-mask" | \
          sed 's/ option subnet-mask //' | \
          sed 's/\;//'`

        GATEWAY=`getipstats | grep "routers" | \
          sed 's/ option routers //' | \
          sed 's/\;//'`

        DNS=`getipstats | grep "domain-name-servers" | \
          sed 's/ option domain-name-servers //' | \
          sed 's/\;//' | sed 's/,/ and /'`

        if [ "$PRINTALL" = "yes" ]; then
           # This is messy, the messages are on one very long
           # line on the screen and in the log
           log_success_msg2 "           DHCP Assigned Settings for $1:"
           log_success_msg2 "           IP Address:      $IPADDR"
           log_success_msg2 "           Subnet Mask:     $NETMASK"
           log_success_msg2 "           Default Gateway: $GATEWAY"
           log_success_msg2 "           DNS Server:      $DNS"
        else
           log_success_msg2 "  IP Addresss:""$IPADDR"
        fi
      fi
    if [ -n "${IPV6}"  -a "${IPV6}" = "yes" ] ; then
      log_info_msg "Starting IPV6 dhclient on the $1 interface..."
      /usr/sbin/dhclient					\
		-6						\
		-lf ${LFILE6}					\
		-pf ${PIDFILE6}					\
		-cf /dev/null					\
		${setlength6}					\
	       	$1 > /dev/null
      if [ "$?" != "0" ]; then
        log_failure_msg2
	fi
      log_success_msg2
      fi
     ;;

   down)
     if [ -n "${IPV6}" -a -e ${PIDFILE6} ] ; then
       log_info_msg "Stopping IPV6 dhclient on the $1 interface..."
       /usr/sbin/dhclient					\
		-6						\
	     	-r						\
		-lf ${LFILE6}					\
		-pf ${PIDFILE6}					\
		-cf /dev/null					\
		$1 2> /dev/null
       if [ -e ${PIDFILE6} ]; then 
         ps $(cat ${PIDFILE6}) | grep dhclient >/dev/null
         if [ "$?" != "0" ]; then
           rm -f ${PIDFILE6}
           fi
         fi
       evaluate_retval
       fi

     if [ ! -e ${PIDFILE} ]; then
       log_warning_msg "dhclient doesn't appear to be running on $1"
       exit 0
       fi
     log_info_msg "Stopping IPV4 dhclient on the $1 interface..."
     /usr/sbin/dhclient						\
		-4						\
	     	-r						\
		-lf ${LFILE}					\
		-pf ${PIDFILE}					\
		$1 2> /dev/null
     if [ -e ${PIDFILE} ]; then 
       ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
       if [ "$?" != "0" ]; then
         rm -f ${PIDFILE}
         fi
       fi
     evaluate_retval
     ;;


   *)
      echo "Usage: $0 [interface] {up|down}"
      exit 1
   ;;
esac

# End services/dhclient
