#! /bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
RUN_INDEX=yes
RUN_NODE=yes
DAEMON_INDEX=/usr/bin/flarei
DAEMON_NODE=/usr/bin/flared
DAEMON_USER=flare
DESC=flare
DESC_INDEX=`basename $DAEMON_INDEX`
DESC_NODE=`basename $DAEMON_NODE`
NAME=flare
NAME_INDEX=$DESC_INDEX
NAME_NODE=$DESC_NODE
CONF_INDEX=/etc/flarei.conf
CONF_NODE=/etc/flared.conf
DATA_INDEX=/var/lib/flare
DATA_NODE=/var/lib/flare

test -x $DAEMON_INDEX || exit 0
test -x $DAEMON_NODE || exit 0

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

set -e

start() {
	start_node
	start_index
  }

start_index() {
	echo -n "Starting $DESC_INDEX: "
	if [ $RUN_INDEX = "yes" ]; then
		start-stop-daemon -c $DAEMON_USER --start --quiet --pidfile $DATA_INDEX/$NAME_INDEX.pid --exec $DAEMON_INDEX -- -f $CONF_INDEX --daemonize
		echo "$NAME_INDEX."
	else
		echo "disabled (skip starting)."
	fi
}

start_node() {
	echo -n "Starting $DESC_NODE: "
	if [ $RUN_NODE = "yes" ]; then
		start-stop-daemon -c $DAEMON_USER --start --quiet --pidfile $DATA_NODE/$NAME_NODE.pid --exec $DAEMON_NODE -- -f $CONF_NODE --daemonize
		echo "$NAME_NODE."
	else
		echo "disabled (skip starting)."
	fi
}

stop() {
	stop_node
	stop_index
  }

stop_index() {
	echo -n "Stopping $DESC_INDEX: "
	if [ $RUN_INDEX = "yes" ]; then
		start-stop-daemon -c $DAEMON_USER --stop --quiet --pidfile $DATA_INDEX/$NAME_INDEX.pid --exec $DAEMON_INDEX
		echo "$NAME_INDEX."
	else
		echo "disabled (skip stopping)."
	fi
}

stop_node() {
	echo -n "Stopping $DESC_NODE: "
	if [ $RUN_NODE = "yes" ]; then
		start-stop-daemon -c $DAEMON_USER --stop --quiet --pidfile $DATA_NODE/$NAME_NODE.pid --exec $DAEMON_NODE
		echo "$NAME_NODE."
		sleep 10
	else
		echo "disabled (skip stopping)."
	fi
}

reload() {
	reload_node
	reload_index
  }

reload_index() {
	if [ $RUN_INDEX = "yes" ]; then
		echo "Reloading $DESC_INDEX configuration files."
		start-stop-daemon -c $DAEMON_USER --stop --signal 1 --quiet --pidfile $DATA_INDEX/$NAME_INDEX.pid --exec $DAEMON_INDEX
	else
		echo "$DESC_INDEX disabled (skip reloading)."
	fi
}

reload_node() {
	if [ $RUN_NODE = "yes" ]; then
		echo "Reloading $DESC_NODE configuration files."
		start-stop-daemon -c $DAEMON_USER --stop --signal 1 --quiet --pidfile $DATA_NODE/$NAME_NODE.pid --exec $DAEMON_NODE
	else
		echo "$DESC_NODE disabled (skip reloading)."
	fi
}

restart() {
	stop
	start
}

restart_index() {
	if [ $RUN_INDEX = "yes" ]; then
		stop_index
		start_index
	else
		echo "$DESC_INDEX disabled (skip restarting)."
	fi
}

restart_node() {
	if [ $RUN_NODE = "yes" ]; then
		stop_node
		start_node
	else
		echo "$DESC_NODE disabled (skip restarting)."
	fi
}

case "$1" in
	start)
		start
	;;
	start-index)
		start_index
	;;
	start-node)
		start_node
	;;
	stop)
		stop
	;;
	stop-index)
		stop_index
	;;
	stop-node)
		stop_node
	;;
	reload)
		reload
	;;
	reload-index)
		reload_index
	;;
	reload-node)
		reload_node
	;;
	restart)
		restart
	;;
	restart-index)
		restart_index
	;;
	restart-node)
		restart_node
	;;
	*)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|start-index|start-node|stop|stop-index|stop-node|restart|restart-index|restart-node|reload|reload-index|reload-node}" >&2
	exit 1
	;;
esac

exit 0
