#!/bin/bash
#
# Init file for Gfarm filesystem metaserver
#
# chkconfig: 2345 90 10
# description: Gfarm filesystem metaserver
#
# processname: gfmd
# config: @config_gfarm_gfmd_config@
# pidfile: @config_gfarm_gfmd_pid_file@
#
# $Id: gfmd.in 10158 2016-05-17 05:32:10Z tatebe $

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

prog="gfmd"
FILE=$prog

DAEMON="@config_gfarm_prefix@/sbin/$prog"
PIDFILE="@config_gfarm_gfmd_pid_file@"
LOCKFILE="@config_gfarm_lock_dir@/$FILE"
OPTIONS="@config_gfarm_gfmd_option@"
warning_facility=local0
warning_priority=notice
export GLOBUS_THREAD_MODEL=pthread

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

call_daemon()
{
	# initlog is deprecated and warning is displayed since Fedora Core 4.
	if egrep -q '^[^#]*[     ]initlog' /etc/init.d/functions; then
		initlog $INITLOG_ARGS -c "$*"
	else
		$*
	fi
}

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

start()
{
	echo -n "Starting $FILE:"
	checkstatus > /dev/null
	if [ $? -eq 0 ]; then
		success
		echo
		return 0
	fi

	call_daemon $DAEMON -P $PIDFILE $OPTIONS && success || failure
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch "$LOCKFILE"
	echo
	return $RETVAL
}

stop()
{
	SIGNAL=$1
	[ X$SIGNAL = X ] && SIGNAL=TERM
	echo -n "Stopping $FILE:"
	checkstatus > /dev/null
	if [ $? -eq 3 ]; then
		success
		echo
		return 0
	fi

	if [ -f "$PIDFILE" ]; then
		pid=`cat "$PIDFILE"`
		kill -$SIGNAL $pid && success || failure
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; 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
		fi
		if [ $RETVAL -eq 0 ] ||
		   [ ! -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]; then
			rm -f "$PIDFILE" "$LOCKFILE"
		fi
	else
		echo "$FILE is already stopped (no $PIDFILE)"
		failure
		RETVAL=1
	fi
	echo
	return $RETVAL
}

checkstatus()
{
	if [ -f "$PIDFILE" ] && [ -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]
	then
		echo "$FILE (pid" `cat "$PIDFILE"`") is running"
		return 0
	elif [ -f "$PIDFILE" ]; then
		echo "$FILE (pid" `cat "$PIDFILE"`") is dead, but $PIDFILE remains"
		return 1
	elif [ -f "$LOCKFILE" ]; then
		echo "$FILE is dead (no $PIDFILE), but $LOCKFILE remains"
		return 2
	else
		echo "$FILE is stopped (no $PIDFILE)"
		return 3
	fi
}

usage()
{
	echo $"Usage: $0 {start|slavestart|stop|kill|restart|reload|status}"
	exit 1
}

RETVAL=0

SUB_COMMAND="$1"
shift

case "$SUB_COMMAND" in
  start)
	start
	RETVAL=$?
	;;
  slavestart)
	OPTIONS="-S $OPTIONS"
	start
	RETVAL=$?
	;;
  stop)
	stop
	RETVAL=$?
	;;
  kill)
	stop KILL
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  status)
	checkstatus
	RETVAL=$?
	;;
  *)
	usage
	;;
esac

exit $RETVAL
