#!/bin/sh
#
# dhcrelay      This shell script takes care of starting and stopping
#               dhcrelay.
#
# chkconfig: - 65 35
# description: dhcrelay6 provides the DHCPv6 Relay service.
#
# processname: dhcrelay6
# pidfile: /var/run/dhcpd/dhcrelay.pid
#
### BEGIN INIT INFO
# Provides: dhcrelay
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 1 6
# Short-Description: The dhcrelay daemon
# Description: dhcrelay provides the DHCP Relay service.
### 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/dhcrelay6 ] || exit 0

# The following variables can be set in the file
# /etc/sysconfig/dhcrelay6.

#LOWER_INTERFACES="eth0 2001:db8:1::1%eth1"
LOWER_INTERFACES=""
#UPPER_INTERFACES="eth2 2001:db8:2::1%eth3"
UPPER_INTERFACES=""
# Define OPTIONS with any other options to pass to the dhcrelay server.
# See dhcrelay(8) for available options and syntax.
OPTIONS="-q"

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

start() {
	# Start daemons.
		if [ -z "$LOWER_INTERFACES" -o -z "$UPPER_INTERFACES" ]; then
		printf "%s" "At least one lower and one upper network interface must be specified"
		echo_failure
		echo
		return 1
	fi

	export UPPER_INTERFACES LOWER_INTERFACES OPTIONS
	gprintf "Starting dhcrelay: "
	daemon /usr/sbin/dhcrelay6
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dhcrelay6
	return $RETVAL
}

stop() {
	# Stop daemons.
	gprintf "Shutting down dhcrelay: "
	killproc dhcrelay6
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dhcrelay6
	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/dhcrelay ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status dhcrelay
	RETVAL=$?
	;;
  *)
	gprintf "Usage: dhcrelay {start|stop|restart|condrestart|status}\n"
	exit 1
esac

exit $RETVAL
