#! /bin/sh

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

set -e

test -f /home/mirror/merlin/merlin.py || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
	log_daemon_msg "Starting Merlin" "merlin"
	if start-stop-daemon --start --make-pidfile --background --quiet --oknodo --chuid mirror --pidfile /var/run/merlin.pid --chdir /home/mirror/merlin --exec /usr/bin/python -- merlin.py -d; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;
  stop)
	log_daemon_msg "Stopping Merlin" "merlin"
	if start-stop-daemon --stop --quiet --oknodo --user mirror --pidfile /var/run/merlin.pid; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;

  restart|force-reload)
	log_daemon_msg "Restarting Merlin" "merlin"
	start-stop-daemon --stop --quiet --oknodo --user mirror --retry 30 --pidfile /var/run/merlin.pid
	if start-stop-daemon --start --make-pidfile --background --quiet --oknodo --chuid mirror --pidfile /var/run/merlin.pid --chdir /home/mirror/merlin --exec /usr/bin/python -- merlin.py -d; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;

  status)
	status_of_proc -p /var/run/merlin.pid /usr/bin/python merlin && exit 0 || exit $?
	;;

  *)
	log_action_msg "Usage: /etc/init.d/merlin {start|stop|force-reload|restart|status}"
	exit 1
esac

exit 0
