#!/bin/sh

# This shell script takes care of starting and stopping dhcpd.
#
# chkconfig: - 65 35
# description: \
# 	The Internet Software Consortium DHCP Server, dhcpd, implements \
# 	the Dynamic Host Configuration Protocol (DHCP) and the Internet \
# 	Bootstrap Protocol (BOOTP).  DHCP allows hosts on a TCP/IP network \
# 	to request and be assigned IP addresses, and also to discover \
# 	information about the network to which they are attached.  BOOTP \
# 	provides similar functionality, with certain restrictions.
# processname: dhcpd
# config: /etc/dhcp/dhcpd.conf
# pidfile: /var/run/dhcpd.pid

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

PIDFILE=/var/run/dhcpd.pid
DHCPD_CONF=/etc/dhcp/dhcpd.conf
DHCPD_CONF_SAMPLE=/etc/dhcp/dhcpd.conf.sample
LOCKFILE=/var/lock/subsys/dhcpd

# Source config.
SourceIfNotEmpty /etc/sysconfig/dhcpd

RETVAL=0

start()
{
	is_yes "$NETWORKING" || return 0
	if [ -e "$DHCPD_CONF" ]; then
		start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user dhcpd -- dhcpd $DHCPDARGS
		RETVAL=$?
	else
		msg_starting dhcpd
		printf "%s: %s" "$DHCPD_CONF" "No such file or directory"
		failure "dhcpd startup"
		echo
		if [ -e "$DHCPD_CONF_SAMPLE" ]; then
			echo "There is a sample configuration file under $DHCPD_CONF_SAMPLE" >&2
		fi
		RETVAL=1
	fi
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user dhcpd -- dhcpd
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condreload|condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user dhcpd -- dhcpd
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
