#!/bin/sh
### BEGIN INIT INFO
# Provides:		devabiz-backend
# Required-Start:	$syslog $remote_fs $network
# Required-Stop:	$syslog $remote_fs $network
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description: Start the devabiz backend.
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/devabiz
DEVABIZ_DIR=/opt/devabiz
DEVABIZ_CONF=${DEVABIZ_DIR}/etc/devabiz-backend.ini
DAEMON=${DEVABIZ_DIR}/bin/devabiz-backend
NAME=devabiz-backend
DESC="PDD backend"
PIDFILE=${DEVABIZ_DIR}/var/run/$NAME.pid
SCRIPTNAME=${DEVABIZ_DIR}/etc/init.d/$NAME

DAEMON_OPTS=" ${DEVABIZ_CONF}"

test -x $DAEMON || exit 0

set -e

case "$1" in
	start)
		echo "Starting $DESC" $NAME

		if start-stop-daemon \
			--background \
			--make-pidfile \
			--start \
			--quiet \
			--pidfile $PIDFILE \
			--exec $DAEMON -- $DAEMON_OPTS
		then
			echo "."
		else
			echo -e " failed!"
		fi
		;;
	stop)
		echo "Stopping $DESC" $NAME
		if start-stop-daemon \
			--stop \
			--retry 30 \
			--quiet \
			--pidfile $PIDFILE \
			--exec $DAEMON
		then
			rm -f $PIDFILE
			echo "."
		else
	 		echo -e " failed!"
		fi
		;;
	reload|force-reload)
		echo "Reloading $DESC configuration" $NAME
		if start-stop-daemon \
			--stop \
			--signal INT \
			--quiet \
			--pidfile $PIDFILE --exec $DAEMON
		then
			rm $PIDFILE
			if start-stop-daemon \
				--background \
				--make-pidfile \
				--start \
				--quiet \
				--pidfile $PIDFILE \
				--exec $DAEMON -- $DAEMON_OPTS
			then
				echo "."
			else
	 			echo -e " failed!"
			fi
		else
	 		echo -e " failed!"
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
		exit 1
		;;
esac

exit 0
