#!/bin/sh
#
# (DHCPv6) This shell script takes care of starting and stopping dhcpd6.
#
# chkconfig: - 65 35
# description: dhcpd provides the Dynamic Host Configuration Protocol service.
# 	The Internet Software Consortium DHCPv6 Server, dhcpd, implements \
# 	the Dynamic Host Configuration Protocol for IPv6 (DHCPv6) and the Internet \
# 	Bootstrap Protocol (BOOTP).  DHCPv6 allows hosts on a TCP/IP network \
# 	to request and be assigned IPv6 addresses, and also to discover \
# 	information about the network to which they are attached.  BOOTP \
# 	provides similar functionality, with certain restrictions.
# processname: dhcpd6
# config: /etc/dhcpd6.conf
# pidfile: /var/run/dhcpd/dhcpd6.pid
#
### BEGIN INIT INFO
# Provides: dhcpd6
# Required-Start: $network
# Required-Stop: $network
# Short-Description: The dhcpd daemon
# Description: dhcpd6 provides the Dynamic Host Configuration Protocol service (DHCPv6).
### END INIT INFO

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

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

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x /usr/sbin/dhcpd ] || exit 0

# The following variables should be set in the file
# /etc/sysconfig/dhcpd.

# Which configuration file to use.
CONFIGFILE="/etc/dhcpd6.conf"
# Where to store the lease state information.
LEASEFILE="/var/lib/dhcp/dhcpd6.leases"
# Define OPTIONS with any other options to pass to the dhcpd server.
OPTIONS="-q"

# Source dhcpd configuration.  Values specified in this file override
# the defaults above.
[ -f /etc/sysconfig/dhcpd6 ] && . /etc/sysconfig/dhcpd6

# If null values were specified, use defaults so we don't get a syntax
# error below.
[ "${CONFIGFILE}" = "" ] && CONFIGFILE="/etc/dhcpd6.conf"
[ "${LEASEFILE}" = "" ] && LEASEFILE="/var/lib/dhcp/dhcpd6.leases"

[ -f $CONFIGFILE ] || exit 0
[ -f $LEASEFILE ] || exit 0

RETVAL=0

start() {
	# Start daemons.
	gprintf "Starting dhcpd (DHCPv6): "
	if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
		OPTIONS="${OPTIONS} -chroot ${ROOTDIR}"
	fi
	daemon /usr/sbin/dhcpd -6 -cf $CONFIGFILE -lf $LEASEFILE $OPTIONS $DHCPDARGS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dhcpd6
	return $RETVAL
}

stop() {
	# Stop daemons.
	gprintf "Shutting down dhcpd (DHCPv6): "
	if [ -r ${ROOTDIR}/var/run/dhcpd/dhcpd6.pid ]; then
	    kill -TERM `cat ${ROOTDIR}/var/run/dhcpd/dhcpd6.pid`
	    RETVAL=$?
	    [ "$RETVAL" = 0 ] && success "stop" || failure "stop"
	else
	    success "already stopped"
	    RETVAL=0
	fi
        [ $RETVAL -eq 0 ] && rm -f ${ROOTDIR}/var/run/dhcpd/dhcpd6.pid
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/dhcpd6
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/dhcpd6 ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status dhcpd6
	RETVAL=$?
	;;
  *)
	gprintf "Usage: dhcpd {start|stop|restart|condrestart|status}\n"
	exit 1
esac

exit $RETVAL
