#! /bin/sh

# Source Common sestup, including hostconfig
#
. /etc/rc.common

User=<your domain here>
Password=<your password here>
Service=<select -2 or -d>
Interval=180
LogFile=/var/log/ivyupdater.log
Flags="-a $User:$Password $Service -i $Interval -l $LogFile"

GetPid(){
	pid=$(ps ax | grep ivyupdater | grep -v grep | awk '{print $1}')
	if [ "X$pid" == "X" ] ; then
		return 1
	else
		echo $pid
		return 0
	fi
}

StartService()
{
	# Don't start unless IvyUpdater is enabled in /etc/hostconfig
	if [ "${IvyUpdater:=-NO-}" = "-YES-" ] ; then
		if pid=$(GetPid ivyupdater); then
			ConsoleMessage "IvyUpdater already running"
		else
			ConsoleMessage "Starting IvyUpdater"
			/usr/sbin/ivyupdater $Flags
		fi
	fi
}

StopService()
{
	if pid=$(GetPid ivyupdater); then
		ConsoleMessage "Stopping IvyUpdater"
		kill -TERM "${pid}"
	else
		ConsoleMessage "IvyUpdater is not running"
	fi
}

RestartService()
{
	if [ "${IvyUpdater:=-NO-}" = "-YES-" ] ; then
		ConsoleMessage "Restarting IvyUpdater"
		StopService
		StartService
	else
		StopService
	fi
}

RunService "$1"
