#!/bin/sh

OSNAME=`uname -s`
PREFIX=/usr/local/imdc

##########################################################################
# This function is used to guarantee IMDC is dead, after unloading it.
##########################################################################

killIMDC()
{
	local pid=`ps ax | awk "\\\$0 ~ /.*\/core\/python\/bin\/imdc.*\$/ { print \\\$1 } "`
	if [ -n "$pid" ]; then
		if kill $pid; then
			return 0
		fi
	fi

	return 1
}

##########################################################################
# This function starts IMDC directly, without going through the
# OS-specific 'load daemon' process.  Any arguments to execIMDC are passed
# to IMDC, so this is used for, i.e. 'pwreset' or '--help'.
##########################################################################

execIMDC()
{
	${PREFIX}/core/python/bin/imdc -OO ${PREFIX}/imdc/main.pyo $*
}

##########################################################################
# This function starts IMDC through the proper OS-specific channel.
##########################################################################

startIMDC()
{
	if [ $OSNAME = "Darwin" ]; then
		/bin/echo -n "Starting IMDC Service..."
		/bin/launchctl load -w /Library/LaunchDaemons/com.dartware.InterMapperDataCenter.plist &> /dev/null && echo "done"
	else
		execIMDC $* setuid daemonize
	fi
}

##########################################################################
# This function stops IMDC through the proper OS-specific channel, then
# checks whether the PID is still around, and really kills it if so.
##########################################################################

stopIMDC()
{
	/bin/echo -n "Stopping IMDC Service..."

	if [ $OSNAME = "Darwin" ]; then
		/bin/launchctl unload -w /Library/LaunchDaemons/com.dartware.InterMapperDataCenter.plist &> /dev/null && echo "done"
	elif [ $OSNAME = "SunOS" ]; then
		pkill -x imdc &> /dev/null && echo "done"
	elif [ $OSNAME = "Linux" ]; then
		if [ -f /etc/SuSE-release ]; then
			/sbin/killproc -TERM imdc &> /dev/null && echo "done"
		elif [ -f /etc/debian_version ]; then
			/sbin/start-stop-daemon --retry 15 --quiet --stop --name imdc &> /dev/null && echo "done"
		else
			killIMDC && echo "done"
		fi
	else
		killIMDC && echo "done"
	fi

	if [ $? -ne 0 ]; then
		if killIMDC; then
			echo "done"
		else
			echo "not running"
		fi
	fi
}

##########################################################################
# Check for known arguments like 'start' or 'stop', and act on them by
# loading/unloading IMDC as necessary.  Any further arguments are passed
# through to IMDC itself, which has a more extensive command-line handler.
##########################################################################

if [ $# -eq 0 ]; then
	execIMDC "--help"
else
	eval action=\$$#

	if [ $action = "stop" ]; then
		stopIMDC
	elif [ $action = "start" ]; then
		startIMDC $*
	elif [ $action = "restart" ]; then
		stopIMDC
		startIMDC $*
	elif [ $action = "pwreset" ]; then
		stopIMDC
		execIMDC $*
	else
		execIMDC $*
	fi
fi
