#! /bin/sh
#
# skeleton      example file to build /etc/init.d/ scripts.
#               This file should be used to construct scripts for /etc/init.d.
#
#               Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#               Modified for Debian GNU/Linux
#               by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:      @(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# Modified for dhcpcd by Dennis Kelly <dpk@debian.org>
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/sbin/dhcpcd
NAME=dhcpcd
DESC="DHCP client daemon"

# DAEMON should be a link to dhcpcd version appropriate for the
# current kernel version.  dhcpcd needs to exist for PCMCIA support
if [ -L $DAEMON ]; then
    # Remove the current link
    rm -f $DAEMON
elif [ -e $DAEMON ]; then
    # Do not remove a user installed binary... simply move it
    mv -f $DAEMON $DAEMON.orig
fi

case `uname -r` in
    2.0.*)
        ln -s /sbin/dhcpcd-2.0.x $DAEMON
        ;;
    2.[1234].*)
        ln -s /sbin/dhcpcd-2.2.x $DAEMON
        ;;
    *)
        echo "$0: Unrecognized kernel version"
        exit 255
        ;;
esac

test -f $DAEMON || exit 0

#set -e

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

# Do not continue if no interface has been configured
if [ "$IFACE" = "none" ]; then
    echo "$DESC disabled or called by pcmcia-cs."
    exit 0
fi

case "$1" in
  start)
        # dhcpcd will fail to start if pid/info/cache file exists
        ps ax |grep -v grep |grep -q 'dhcpcd-'
        if [ $? = 0 ]; then
            echo "$DESC is already running."
            exit 0
        fi
        rm -f /var/run/dhcpcd-$IFACE.pid 
	rm -f /etc/dhcpc/dhcpcd-$IFACE.info /etc/dhcpc/dhcpcd-$IFACE.cache

        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile \
                /var/run/$NAME-$IFACE.pid --exec $DAEMON -- $OPTIONS $IFACE
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /var/run/$NAME-$IFACE.pid --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        #
        #       If the "reload" option is implemented, move the "force-reload"
        #       option to the "reload" entry above. If not, "force-reload" is
        #       just the same as "restart".
        #
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /var/run/$NAME-$IFACE.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /var/run/$NAME-$IFACE.pid --exec $DAEMON -- $OPTIONS $IFACE
        echo "$NAME."
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
