#!/bin/sh
#
# Startup script for the Shibboleth Service Provider Daemon
#
# pidfile: /var/local/shibboleth/var/run/shibd.pid
# config: /var/local/shibboleth/etc/shibboleth/shibboleth.xml

shibd_wrapper="@-PREFIX-@/sbin/shibd-wrapper"
SHIBD_USER=root
pidfile="@-VARRUNDIR-@/shibd.pid"
prog=shibd
RETVAL=0

start() {
	if [ -f $pidfile ]; then
		read kpid < $pidfile
		if kill -0 $kpid > /dev/null 2>&1 ; then
			echo "$prog already running"
			return -1
		fi
	fi
 
	echo "Starting $prog"
	SHIBD_PID=$pidfile
	export SHIBD_PID
 	touch $pidfile
 	chown $SHIBD_USER:$SHIBD_USER $pidfile
	su - $SHIBD_USER -c "$shibd_wrapper -p $pidfile -f &"
	RETVAL=$?
	return $RETVAL
}

stop() {
	if [ -f $pidfile ]; then
		echo "Stopping $prog"
		read kpid < $pidfile
		kill $kpid > /dev/null 2>&1
		RETVAL=$?
	fi
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	sleep 5
	start
	;;
  *)
	echo $"Usage: $prog {start|stop|restart}"
	exit 1
esac

exit $RETVAL
