#!/bin/sh
#
# $Id: gfmd.in 10159 2016-05-17 05:50:14Z n-soda $
#
## For RedHat Linux style rc.d, and start_service() in config-gfarm.sysdep
# chkconfig: 2345 90 10
#
## For RedHat Linux style rc.d
# description: Gfarm filesystem metaserver
# processname: gfmd
# config: @config_gfarm_gfmd_config@
# pidfile: @config_gfarm_gfmd_pid_file@
#
## For SuSE/United-Linux style rc.d
### BEGIN INIT INFO
# Provides: gfmd
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Gfarm filesystem metaserver
### END INIT INFO

PROG="gfmd"
FILE=$PROG

CONF="@config_gfarm_gfmd_config@"
DAEMON="@config_gfarm_prefix@/sbin/$PROG"
PIDFILE="@config_gfarm_gfmd_pid_file@"
OPTIONS="@config_gfarm_gfmd_option@"
warning_facility=local0
warning_priority=notice
GLOBUS_THREAD_MODEL=pthread; export GLOBUS_THREAD_MODEL

case $1 in 
start|slavestart)
	if [ $1 = slavestart ]; then
		OPTIONS="-S $OPTIONS"
	fi
	if [ ! -x "$DAEMON" ]; then
		echo -n "$DAEMON is not installed"
		exit 1
	fi
	if [ ! -f "$CONF" ]; then
		echo -n "$CONF is not configured"
		exit 1
	fi
	$DAEMON -P "$PIDFILE" $OPTIONS
	;;

stop|kill)
	SIGNAL=TERM
	[ $1 = kill ] && SIGNAL=KILL
	if [ -f "$PIDFILE" ]; then
		pid=`cat "$PIDFILE"`
		if kill -$SIGNAL $pid; then
			while kill -0 $pid 2>/dev/null; do
				echo "old $FILE (pid $pid) is still running, sleeping..."
				logger -p ${warning_facility}.${warning_priority} "old $FILE (pid $pid) is still running, sleeping..."
				sleep 1
			done
			rm -f "$PIDFILE"
		else
			echo "$FILE (pid $pid) is not running? (check $PIDFILE)"
			exit 1
		fi
	else
		echo "$FILE is already stopped (no $PIDFILE)"
		exit 1
	fi
	;;

restart)
	$0 stop
	$0 start
	;;

status)
	if [ -f "$PIDFILE" ] && kill -0 `cat "$PIDFILE"`; then
		echo "$FILE (pid" `cat "$PIDFILE"`") is running"
	elif [ -f "$PIDFILE" ]; then
		echo "$FILE (pid" `cat "$PIDFILE"`") is not running? (check $PIDFILE)"
		exit 1
	else
		echo "$FILE is stopped (no $PIDFILE)"
		exit 1
	fi
	;;

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