#!/bin/sh

### BEGIN INIT INFO
# Provides:        packagemanager
# Required-Start:  $network $remote_fs $syslog
# Required-Stop:   $network $remote_fs $syslog
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start PackageManager update service
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

DAEMON=/usr/sbin/packagemanager-watchdb
PIDFILE=/var/run/packagemanager-watchdb.pid
OPTS="packagemanager packagemanager"

test -x $DAEMON || exit 5

if type log_daemon_msg 2>/dev/null >/dev/null
then
	: ok
else
log_daemon_msg()
{
	echo -n $@
}
log_end_msg()
{
	if [ "$?" = 0 ]
	then
		log_success_msg 
	else
		log_failure_msg
	fi
}
status_of_proc()
{
	if pidofproc -p $PIDFILE $DAEMON >/dev/null
	then
		log_success_msg $DAEMON
	else
		log_failure_msg $DAEMON
	fi
}
fi

case $1 in
	start)
		log_daemon_msg "Starting PackageManager update service" "packagemanager-watchdb"
  		start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON -- $OPTS
		status=$?
		log_end_msg $status
  		;;
	stop)
		log_daemon_msg "Stopping PackageManager update service" "packagemanager-watchdb"
  		start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
		log_end_msg $?
		rm -f $PIDFILE
  		;;
	restart|force-reload)
		$0 stop && sleep 2 && $0 start
  		;;
	try-restart)
		if $0 status >/dev/null; then
			$0 restart
		else
			exit 0
		fi
		;;
	reload)
		exit 3
		;;
	status)
		status_of_proc $DAEMON "PackageManager update service"
		;;
	*)
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
		exit 2
		;;
esac
