#!/bin/bash
#
#	/etc/rc.d/init.d/pgjobs
#
# Starts the pgjobs agent
#
# chkconfig: 2345 86 14
# description: Job scheduling and executing agent for PostgreSQL
# processname: pgjobsagent
#
# --------------------------------------------------------------
#
# Author:  Zlatko Michailov
# Created: 11-Jun-2004
# Updated: 17-Jun-2004
#
# --------------------------------------------------------------



# Source function library.
. /etc/init.d/functions


appDir="/usr/share/pgjobs"
tmpDir="/tmp/pgjobs"
exeName="pgjobsagent"
exePid=""



# If the pgjobs agent executable is not installed, quit
test -x "$appDir/$exeName" || exit 0



# --------------------------------------------------------------

echoExt()
{
	if [ "$ext" == "0" ]
	then
		echo_success
	else
		echo_failure
	fi
	echo
}



findExePid()
{
	exePid=`ps -e | grep "$exeName" | grep -Eo "^[[:space:]]*[[:digit:]]+"`
}



prepare()
{
	ext="0"

	if [ ! -d "$tmpDir" ]
	then
		mkdir "$tmpDir"
		ext=$?
	fi

	if [ "$ext" == "0" ]
	then
		chmod 777 "$tmpDir"
		ext=$?
	fi

	return $ext
}



start()
{
	findExePid
	if [ "$exePid" == "" ]
	then
	    echo -n $"Starting $exeName: "

		prepare
		if [ "$ext" == "0" ]
		then
		    "$appDir/$exeName" &
			ext=$?
		fi

		echoExt
	fi
}



stop()
{
	findExePid
	if [ "$exePid" != "" ]
	then
		echo -n $"Stopping $exeName: "

		kill "$exePid"
		ext=$?

		echoExt
	fi
}



# --------------------------------------------------------------

ext="0"

case "$1" in
	start)
		start
		;;

	stop)
		stop
		;;

	restart)
		stop
		start
		;;

	condrestart)
		findExePid
		if [ "$exePid" != "" ]
		then
			stop
			start
		fi
		;;

	status)
		status "$appDir/$exeName"
		;;

	*)
		echo $"Usage: $0 {condrestart|start|stop|restart|status}"
		ext="1"
		;;
esac

exit $ext


