#!/sbin/sh


# Sample SysV-style startup script for CMU+PRINCETON dhcpd
# Install as /etc/init.d/dhcpd, customize it, then make the appropriate
# symlinks so it starts/stops at the appropriate times; e.g.:
# /etc/rc3.d/S19dhcpd, /etc/rc1.d/K31dhcpd, /etc/rc0.d/K31dhcpd.

# /etc/init.d/dhcpd start|stop [-v] : Start/Stop the dhcpd daemon
# (The network and streams should already be built by the time the dhcpd daemon starts.)

# If the daemon is already executing, don't re-execute it.
# To try to minimize the likelihood that the processid stored in the PID file corresponds
# to an unrelated process (common with startup scripts), we'll clear the contents of the
# PID file when we kill the daemon using this script.  And when we find a pid in that file,
# we'll both check that a process with that pid exists, and that the commandname for the process
# looks like the one we're interested in.

PATH=/usr/bin:/bin

# variables associated with the dhcp server
SERVERROOT=/usr/local/etc
PIDFILE=/usr/local/etc/dhcpd.pid
BINDINGSDIR=/var/local/dhcp/bindings
CONFFILE=/var/local/dhcp/dhcpd.conf
BOOTPTAB=/var/local/dhcp/bootptab
DUMPFILE_BINDINGS=/var/local/dhcp/dumps/bindings.dump
DUMPFILE_BOOTPTAB=/var/local/dhcp/dumps/bootptab.dump
DEBUG="-d3"
RUNAS_USER="-U dhcp"
RUNAS_GROUP="-G dhcp"
CURRENT_WORKING_DIR="/"
MISC_OPTIONS="-a -A -e -I"

PS_SEARCH="dhcpd"										# string to look for (below) in process name to decide it it looks reasonable
PROGNAME="dhcpd"										# progname to print in echo output below
EXECUTABLE=$SERVERROOT/dhcpd                            # must exist before starting

STARTCOMMAND="$EXECUTABLE -c $CURRENT_WORKING_DIR $RUNAS_USER $RUNAS_GROUP -P $PIDFILE -b $BINDINGSDIR -u $DEBUG -B $DUMPFILE_BINDINGS -x $CONFFILE $MISC_OPTIONS $BOOTPTAB $DUMPFILE_BOOTPTAB"       # actual to invoke

CAT="/bin/cat"
CP="/usr/bin/cp"
PS="/bin/ps -f -p"	# command to do long (full) listing of a specific pid (this is SysV flavor)
TAIL="/usr/bin/tail"
GREP="/usr/bin/grep"

while [ $# -gt 0 ]
do
	case "$1" in 
		'-v')
			verbose=1
			;;

		'start')
			echo "$0: starting $PROGNAME...\c"
			if [ -f $EXECUTABLE ]; then

				# If we find the daemon already running, exit.  Else fall through to the $STARTCOMMAND
				if [ -f $PIDFILE ]; then
					pid=`$CAT $PIDFILE`
					if [ -n "$pid" ]; then
						if kill -0 "$pid" > /dev/null 2>&1 ; then
							# there is a process with that pid, but is it the right one?
							$PS $pid | $TAIL -1 | $GREP $PS_SEARCH > /dev/null 2>&1
							if [ $? -eq 0 ] ; then
								echo "$0: $PIDFILE=$pid processid exists with reasonable name, assuming daemon is already running"
								exit
							else
								# that process is something else, so we can assume ours isn't already running
								# Clearing our pidfile is just good housekeeping, not required
								$CP /dev/null $PIDFILE
							fi
						fi	# else no process running with that pid, so we assume it's not already running
					fi	# else pidfile doesn't contain a number, so we assume it's not already running
				fi	# else no pidfile, so we assume it's not already running
				# Success!  It really looks like the daemon isn't already running, so start it
				$STARTCOMMAND
				echo "done"
				exit

			else
				echo "$0: executable $EXECUTABLE does not exist, cannot start daemon"
				exit 1
			fi
			;;

		'stop')
			echo "$0: stopping $PROGNAME...\c"
			if [ -f $PIDFILE ] ; then
				pid=`$CAT $PIDFILE`
				if [ -n "$pid" ]; then
					if kill -0 "$pid" > /dev/null 2>&1 ; then
						# there is a process with that pid, but is it the right one?
						$PS $pid | $TAIL -1 | $GREP $PS_SEARCH > /dev/null 2>&1
						if [ $? -eq 0 ] ; then
							# Success! The process has reasonable name, go ahead and kill it
							kill $pid
							$CP /dev/null $PIDFILE
							echo "done"
							exit
						else
							if [ -n "$verbose" ]; then
								echo "$0: $PIDFILE=$pid processid is for different daemon, assuming ours is not running"
							fi
							# Clearing our pidfile is just good housekeeping, not required
							$CP /dev/null $PIDFILE
							echo "done"
							exit
						fi
					else
						if [ -n "$verbose" ]; then
							echo "$0: $PIDFILE=$pid processid does not exist, assuming is not running"
						fi
						# Clearing our pidfile is just good housekeeping, not required
						$CP /dev/null $PIDFILE
						echo "done"
						exit
					fi
				else 
					if [ -n "$verbose" ]; then
						echo "$0: pidfile $PIDFILE is empty, assuming is not running"
					fi
					echo "done"
					exit
				fi
		else
			if [ -n "$verbose" ]; then
				echo "$0: pidfile $PIDFILE not found, assuming is not running"
			fi
		fi
		echo "done"
		;;

		*)
			echo "usage: $0 [-v] {start|stop}"
			;;
	esac
	shift
done
