#! /bin/sh
# $Id$
#
# /etc/init.d/@config_gfarm_gfsd_rc_name@
#
#   and its symbolic link
#
# /usr/sbin/rc@config_gfarm_gfsd_rc_name@
#
### BEGIN INIT INFO
# Provides: @config_gfarm_gfsd_rc_name@
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Gfarm filesystem node daemon
### END INIT INFO

. /etc/rc.status

prog="gfsd"
FILE="@config_gfarm_gfsd_rc_name@" # usually "gfsd", or maybe "gfsd-IP_ADDRESS"

DAEMON="@config_gfarm_prefix@/sbin/$prog"
PIDFILE="@config_gfarm_gfsd_pid_file@"
OPTIONS="@config_gfarm_gfsd_option@"

# gfsd service certificate
X509_USER_CERT=/etc/grid-security/@config_gfarm_gfsd_rc_name@/gfsdcert.pem
X509_USER_KEY=/etc/grid-security/@config_gfarm_gfsd_rc_name@/gfsdkey.pem
export X509_USER_CERT X509_USER_KEY

# pull in sysconfig settings
[ -f "/etc/sysconfig/$FILE" ] && . "/etc/sysconfig/$FILE"

# We won't just use "checkproc", "startproc" and "killproc" here,
# because these functions don't allow multiple server processes for
# same binary.

rc_reset

case $1 in
    start)
	echo -n "Starting $FILE"
	if [ ! -x "$DAEMON" ]; then
		rc_failed 5 # program is not installed
	elif "$DAEMON" -P "$PIDFILE" $OPTIONS; then
		:
	else
		rc_failed 1 # generic or unspecified error
	fi		
	rc_status -v
	;;
    stop)
	echo -n "Shutting down $FILE"
	if [ -f "$PIDFILE" ] && kill -TERM `cat "$PIDFILE"`; then
		rm -f "$PIDFILE"
	elif [ -f "$PIDFILE" ] && [ -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]
	then
		rc_failed 4 # user had insufficient privileges
	else
		if [ -f "$PIDFILE" ]; then
			echo -n "$FILE (pid" `cat "$PIDFILE"`") is dead, but $PIDFILE remains"
		fi
		rc_failed 7 # program is not running
	fi
	rc_status -v
	;;
    try-restart)
	## Do a restart only if the service was active before.
	$0 status >/dev/null && $0 restart
	rc_status # Remember status and be quiet
	;;
    restart)
	$0 stop
	$0 start
	rc_status # Remember status and be quiet
	;;
    force-reload)
	echo -n "Reload service $FILE"
	$0 stop && $0 start
	rc_status # Remember status and be quiet
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signaling, do nothing (!)
	echo -n "Reload service $FILE"
	rc_failed 3 # unimplemented feature
	rc_status -v
	;;
    status)
	echo -n "Checking for service $FILE: "
	if [ -f "$PIDFILE" ] && [ -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]
	then
		:
	elif [ -f "$PIDFILE" ]; then
		rc_failed 1 # service dead, but pid file exists
	else
		rc_failed 3 # service not running (unused)
	fi
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload}"
	exit 1
	;;
esac
rc_exit
