#!/bin/sh
# Begin $network-devices/services/dhcpcd

# Based upon lfs-bootscripts-1.12 $NETWORK_DEVICES/if{down,up}
# Rewritten by Nathan Coulson <nathan@linuxfromscratch.org>
# Adapted for dhcpcd by DJ Lucas <dj@linuxfromscratch.org>

#$LastChangedBy: bdubbs $
#$Date: 2005-08-01 13:29:19 -0600 (Mon, 01 Aug 2005) $

#. /etc/sysconfig/rc
#. $RC_FUNCTIONS

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

PIDFILE="/var/run/dhcpcd-$1.pid"
LEASEINFO="/var/lib/dhcpcd/dhcpcd-$1.info"

case "$2" in
        up)
                message="Starting dhcpcd on the $1 interface..."
                # Test to see if there is a stale pid file
                if [ -f "$PIDFILE" ]
                then
                    ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
                    if [ $? != 0 ]
                    then
                        rm -f /var/run/dhcpcd-$1.pid > /dev/null
                    else
			message="${message}dhcpcd already running!"
                        log_warning_msg "${message}"
                        exit 2
                    fi
                fi
                /sbin/dhcpcd $1 $DHCP_START
		# Save the return value
                RET="${?}"
                ( exit "${RET}"; )
                evaluate_retval standard
		# Print the assigned settings if requested
		if [ "$RET" = "0" -a "$PRINTIP" = "yes" ]; then
			. "$LEASEINFO"
			if [ "$PRINTALL" = "yes" ]; then
				echo "           DHCP Assigned Settings for $1:"
				echo "           IP Address:      $IPADDR"
				echo "           Subnet Mask:     $NETMASK"
				echo "           Default Gateway: $GATEWAYS"
				echo "           DNS Server:      $DNSSERVERS"
			else
				echo " IP Addresss: ""$IPADDR"
			fi
                fi
        ;;

        down)
		message="Stopping dhcpcd on the $1 interface..."
		if [ -z "$DHCP_STOP" ]
		then
		    killproc -p "${PIDFILE}" /sbin/dhcpcd
		    evaluate_retval standard
		else
		    /sbin/dhcpcd $1 $DHCP_STOP &> /dev/null
		    RET="$?"
		    if [ "$RET" -eq 0 ]; then
			echo -n ""
			evaluate_retval standard
		    elif [ "$RET" -eq 1 ]; then
			message="${message}dhcpcd not running!"
			log_warning_msg "${message}"
		    else
			log_failure_msg "${message}"
		    fi
		fi
        ;;

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

# End $NETWORK_DEVICES/services/dhcpcd
