#!/bin/sh
# chkconfig: 345 13 86
# description: Manage the VPN deamon for this host


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

# This grabs only the hostname. Larger networks may require FQNs.
HOST=`hostname -s`

case "$1" in
  start)
	echo -n "Starting vpnd: "
	if [ ! -f /var/lock/subsys/vpnd ]; then
		CONF=`ls /etc/vpnd/${HOST}*.conf`
		for conf in $CONF; do
#			echo "/usr/local/sbin/vpnd -f $conf"
			/usr/local/sbin/vpnd -f $conf
			if [ $? != 0 ]; then
				failure; echo
				exit 1
			fi
		done
		touch /var/lock/subsys/vpnd
		success; echo
	fi
	;;
  stop)
	echo -n "Stopping vpnd: "
	PIDS=`ls /var/run/vpnd_*.pid 2>/dev/null`
	if [ $? != 0 ]; then
		failure; echo
		echo "Failed to find PID of running vpnd process!"
		exit 1
	fi
	for apid in $PIDS; do
		kill `cat $apid` 
		#   ^^ Adding -9 here will prevent vpnd from removing its pid file
		if [ $? != 0 ]; then
			failure; echo
			exit 1
		fi
	done
	rm -f /var/lock/subsys/vpnd
	success; echo
	;;
  restart)
	$0 stop
	$0 start
	;;
  status)
	status vpnd
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac

exit 0
