#!/bin/sh
#
# racoon       This shell script takes care of starting and stopping
#              racoon.
#
# chkconfig: 2345 11 89
#
# description: racoon -- IKE (ISAKMP/Oakley) key management daemon

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

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

#
prog="racoon"

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

[ -f /sbin/setkey ] || exit 0
[ -f /usr/sbin/racoon ] || exit 0

case "$1" in
    start)
	# Start daemons.

	# Setting up SAD and SPD policies is not required.
	if [ -f /etc/ipsec.conf ]; then
		echo -n "Setting up IPsec policies"
		/sbin/setkey -f /etc/ipsec.conf
		echo
	fi
	
	echo -n "Starting $prog: "
	daemon /usr/sbin/racoon -f /etc/racoon/racoon.conf
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ]; then
		touch /var/lock/subsys/racoon
	fi;
	;;
    stop)
	# Stop daemons.
	echo -n "Shutting down $prog: "
	killproc racoon
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ]; then
		rm -f /var/lock/subsys/racoon
		# Flush SAD and SPD policies
		echo -n "Flush IPsec policies"
		/sbin/setkey -FP
		echo
	fi;
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
	status racoon
	;;

    *)
	echo "Usage: $0 {start|stop|status|restart|}"
	exit 1
	;;
esac

exit 0
