#!/bin/sh
### BEGIN INIT INFO
# Provides:          peroxide
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Peroxide Service Manager
# Description:       Start/stop peroxide service
### END INIT INFO

if [ "$NETWORKING" = "no" ]; then
	exit 0
fi

CONFIG=/path/to/peroxide.json
PORT=8000
USE_SSL="no"
PREFIX=/usr/local
NAME=peroxide
DESC=Peroxide
PID_FILE=/var/run/peroxide.pid
DAEMON=$PREFIX/bin/peroxide

test -x $DAEMON || exit 1
set -e
. /lib/lsb/init-functions

# Include defaults if available
if [ -f /etc/default/peroxide ] ; then
	. /etc/default/peroxide
fi

DAEMON_OPTS=" --port=$PORT --config=$CONFIG --daemon --pidfile=$PID_FILE"

if [ "x$USE_SSL" == "xyes" ]; then
	DAEMON_OPTS="$DAEMON_OPTS --ssl --key $SSL_KEY --cert $SSL_CERT"
fi

start() {
	if [ -f $PID_FILE ]; then
		echo "$PID_FILE exists, process is already running"
	else
		echo "Starting $DESC: "
		$DAEMON $DAEMON_OPTS
		if [ $? -ne 0 ]; then
			echo "failed."
			exit 1
		fi
		echo "started."
	fi
}

stop() {
	if [ ! -f $PID_FILE ]; then
		echo "$PID_FILE does not exist, process is not running"
	else
		echo "Stopping $DESC..."
		PID=`cat $PID_FILE`
		rm $PID_FILE
		kill $PID
		echo "stopped."
	fi
}

restart() {
	echo "Restarting $DESC..."
	if [ -f $PID_FILE ]; then
		PID=`cat $PID_FILE`
		rm $PID_FILE
		kill $PID
	fi

	sleep 1
	$DAEMON $DAEMON_OPTS
	if [ $? -ne 0 ]; then
		echo "failed."
		exit 1
	fi
	echo "restarted."
}

status() {
	status_of_proc -p $PID_FILE $DAEMON "$NAME" && exit 0 || exit $?
}

info() {
	echo $"Usage: $0 {start|stop|status|restart|reload}"
	exit 1
}

case "$1" in

	start)
		start
		;;

	stop)
		stop
		;;

	restart)
		restart
		;;

	reload)
		restart
		;;

	status)
		status
		;;

	*)
		info
		;;

esac

exit 0