#!/bin/sh
#
# start/stop the identity manager daemon.
#
### BEGIN INIT INFO
# Provides:          identity_manager
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the identity manager daemon.
# Description:       Starts and stops the identity manager daemon to
#                    create and refresh user proxy files
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

if ! [ -x /opt/submit/manageIdentities.py ]; then
	exit 1
fi

case "$1" in
    start)
	if [ -f /sbin/start-stop-daemon ]; then
		echo -n "Starting identity manager:"
		echo -n " identityman" ; pgrep -fl '/opt/submit/manageIdentities.py' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/manageIdentities.py
	else
		echo -n " identityman" ; daemon --check manageIdentities --user=gridman /opt/submit/manageIdentities.py
	fi

	echo "."
	;;
    stop)
	if [ -f /sbin/start-stop-daemon ]; then
		echo -n "Stopping identity manager:"
	fi
	echo -n " identityman" ; pkill -TERM -u gridman -f '/opt/submit/manageIdentities.py'
	echo "."
	;;
    restart|force-reload)
	if [ -f /sbin/start-stop-daemon ]; then
		echo -n "Restarting identity manager:"
	fi
	echo -n " identityman"
	pkill -TERM -u gridman -f '/opt/submit/manageIdentities.py'
	if [ -f /sbin/start-stop-daemon ]; then
		/sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/manageIdentities.py
	else
		daemon --check manageIdentities --user=gridman /opt/submit/manageIdentities.py
	fi
	echo "."
	;;
    *)
	echo "Usage: /etc/init.d/identityman {start|stop|restart|force-reload}"
	exit 1
	;;
esac

exit 0
