#!/bin/sh

# dhcpcd doesn't support a config file, just command line options.
# ifup can set some  options (-h -i -I -l) but no others.
# This wrapper adds any other options set in /etc/dhcpc/config
# and then calls the dhcpcd binary, named dhcpcd-bin.

# get interface
eval INTERFACE=\${$#}

# default is eth0
case ${INTERFACE} in
  eth*) ;;
     *) 
        INTERFACE=eth0
        ;;
esac

# dhcpcd will fail to start if pid file exists, so delete it
# if there is no process around. Note that calling dhcpcd -k or dhcpcd -n
# both affect running processes, so skip this for those.
keeppid=no

for o 
do
   if [ $o = "-k" ]; then
      keeppid=yes
   fi
   if [ $o = "-n" ]; then
      keeppid=yes
   fi
done

ps ax | grep -v grep | grep -s dhcpcd-bin | grep -q $INTERFACE
if [ $? = 0 ] &&
   [ $keeppid = no ] 
then
    echo "Dhcpcd is already running."
    exit 0
fi
     
if [ $keeppid = no ]; then
   rm -f /var/run/dhcpcd-$INTERFACE.pid
fi

# load configuration file
if [ -f /etc/dhcpc/config ] ; then
    . /etc/dhcpc/config
fi

# Note that in the absence of /etc/dhcpc/config we play safe and disallow
# changes to /etc/resolv.conf and friends.

if [ "$SET_DNS" != "yes" ]; then
  OPTIONS="-R $OPTIONS"
fi

if [ "$SET_DOMAIN" = "yes" ]; then
  OPTIONS="-D $OPTIONS"
fi

if [ "$SET_HOSTNAME" = "yes" ]; then
  OPTIONS="-H $OPTIONS"
fi

if [ "$SET_NTP" != "yes" ]; then
  OPTIONS="-N $OPTIONS"
fi

if [ "$SET_YP" != "yes" ]; then
  OPTIONS="-Y $OPTIONS"
fi

exec /sbin/dhcpcd-bin $OPTIONS $*
