#!/bin/bash
# Begin services/dhcpcd

# Origianlly dased 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>
# Update for LFS 7.0 by Bruce Dubbs <bdubbs@linuxfromscratch,org>

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

#$LastChangedBy: dj $
#$Date: 2011-11-27 05:08:57 +0000 (Sun, 27 Nov 2011) $

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

case "$2" in
    up)
       log_info_msg "\nStarting dhcpcd on the $1 interface..."

       # Test to see if there is a stale pid file
       pidfile="/var/run/dhcpcd-$1.pid"

       if [ -f "$pidfile" ]; then
          ps `cat "$pidfile"` | grep dhcpcd > /dev/null

          if [ $? != 0 ]; then
             rm -f /var/run/dhcpcd-$1.pid > /dev/null
          
          else
             log_warning_msg "dhcpcd is already running!"
             exit 2
          fi
       fi

       /sbin/dhcpcd $1 $DHCP_START
       evaluate_retval
       ;;

     down)
       log_info_msg "Stopping dhcpcd on the $1 interface..."

       if [ -z "$DHCP_STOP" ]; then
          killproc -p "${PIDFILE}" /sbin/dhcpcd

       else
          /sbin/dhcpcd $1 $DHCP_STOP &> /dev/null

          if [ "$?" -eq 1 ]; then
             log_warning_msg "dhcpcd not running!"
             exit 2
          fi
       fi

       evaluate_retval
       ;;

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

# End services/dhcpcd
