#!/bin/sh
#
# imdc                Start/Stop the InterMapper DataCenter service
#
#                     Usually, you will copy this file into /etc/init.d and
#                     create links to this file in /etc/rc3.d/S97imdc
#                     and /etc/rc6.d/K03imdc.
#
# chkconfig:          345 97 3
# description:        InterMapper DataCenter is a collection of servers and \
#                     tools to augment InterMapper's capabilities
# processname:        imdc

### BEGIN INIT INFO
# Provides:           imdc
# Required-Start:     $remote_fs $syslog $named $time
# Required-Stop:      $remote_fs $syslog $named $time
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Start InterMapper DataCenter at boot time
# Description:        InterMapper DataCenter is a collection of servers and
#                     tools to augment InterMapper capabilities.
### END INIT INFO

PREFIX="/usr/local"

PROGNAME="imdc"
PROGPATH="$PREFIX/imdc/core/python/bin/$PROGNAME"
MAINPATH="$PREFIX/imdc/imdc/main.pyo"
LOCKPATH="/var/lock/subsys/$PROGNAME"

test -x "$PROGPATH" || exit 5
test -f "$MAINPATH" || exit 5

# Load RedHat's library of shell functions

. /etc/rc.d/init.d/functions

RETVAL=0


# replacement __pids_pidof for use by killproc and status
__pids_pidof() {
    if [ -x /usr/bin/pgrep ]; then
	# IMDC is daemonized, extension server is not
	pgrep -P 1 -f "$1" || \
	    pgrep -P 1 "${1##*/}"
    else
	pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
	    pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
    fi
}

# replacement killproc, does not wait the full timeout if daemon exits early
killproc() {

	local RC killlevel= base pid pid_file= delay delayseconds

	RC=0; delay=3

	if [ "$#" -eq 0 ]; then
		echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
		return 1
	fi
	if [ "$1" = "-p" ]; then
		pid_file=$2
		shift 2
	fi
	if [ "$1" = "-d" ]; then
		delay=$2
		shift 2
	fi

	# Check for second arg to be kill level

	[ -n "${2:-}" ] && killlevel=$2

	base=${1##*/}

	# Find pid

	__pids_var_run "$1" "$pid_file"
	if [ -z "$pid_file" -a -z "$pid" ]; then
		pid="$(__pids_pidof "$1")"
	fi

	# Kill it.

	if [ -n "$pid" ] ; then
		[ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
		if [ -z "$killlevel" ]; then
			if checkpid $pid 2>&1; then

				# TERM first, then KILL if not dead

				kill -TERM $pid >/dev/null 2>&1
				usleep 100000
				delayseconds=0

				while [ $delayseconds -lt $delay ] && checkpid $pid ; do 
					delayseconds=`expr $delayseconds + 1`
					sleep 1
				done

				if checkpid $pid ; then
					kill -KILL $pid >/dev/null 2>&1
					usleep 100000
				fi
			fi

			checkpid $pid
			RC=$?
			[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
			RC=$((! $RC))

		else
			if checkpid $pid; then
				kill $killlevel $pid >/dev/null 2>&1
				RC=$?
				[ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
			elif [ -n "${LSB:-}" ]; then
				RC=7 # Program is not running
			fi
		fi
	else
		if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
			RC=7 # Program is not running
		else
			failure $"$base shutdown"
			RC=0
		fi
	fi

	# Remove pid file if any.

	if [ -z "$killlevel" ]; then
		rm -f "${pid_file:-/var/run/$base.pid}"
	fi

	return $RC
}


start() {
    echo -n "Starting InterMapper DataCenter: "
    "$PROGPATH" -OO "$MAINPATH" setuid daemonize
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch "$LOCKPATH"
    return $RETVAL
}

stop() {
    echo -n "Stopping InterMapper DataCenter: "
    killproc -d 600 "$PROGPATH"
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f "$LOCKPATH"
    return $RETVAL
}

restart() {
    stop
	sleep 5
    start
}

condrestart() {
    [ -e "$LOCKPATH" ] && restart
    return 0
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
condrestart)
	condrestart
	;;
status)
	status "$PROGPATH"
	RETVAL=$?
	;;
*)
	echo "Usage: $0 (start|stop|status|restart|condrestart)"
	RETVAL=1
esac

exit $RETVAL
