#! /bin/sh
## /etc/init.d/oops - init.d script for nstxd/nstxcd
## Based on skeleton by Miquel van Smoorenburg <miquels@cistron.nl>.
## Modified by Tamas SZERB <toma@rulez.org>

RUNNING_TYPE=standalone
# * run : respawning if the server dies
# * standalone : just run the server
# Suggested to use 'run' on high loaded server,
# but do not forget to install the 'run' package first!

#oops log level:
#This can be like this: "acdfhins" (positive) or "ACDFHINS" (negative)
#To enable one each type of log, use the lower case
#to disable, use the upper case letter!
#The letter's meanings are:
#a|A = all
#c|C = notice
#d|D = debug
#f|F = FTP
#h|H = HTTP
#i|I = information
#n|N = DNS
#s|S = storages
#
VERBOSITY="ACDFHINS"

# The following value is extracted by debstd to figure out how to generate
# the postinst script. Edit the field to change the way the script is
# registered through update-rc.d (see the manpage for update-rc.d!)
FLAGS="defaults 30"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/oops
DAEMONCTL=/usr/sbin/oopsctl
NAME=oops
DESC="oops proxy"
CFG=/etc/oops/oops.cfg
SOCKET=/var/run/oops/oopsctl
PIDFILE="/var/run/oops/oops.pid"

OOPS="-x$VERBOSITY -c$CFG -d"
OOPS_RUN="-x$VERBOSITY -c$CFG"

RUN="/usr/bin/run --restart --daemon --null --robust"
STOP="/usr/bin/stop --quiet"
#--force
USER="proxy"
GROUP="proxy"

test -f $DAEMON || exit 0
test -f $DAEMONCTL || exit 0

set -e

# /var/run might be tempfs. Be sure the directory exists.
if [ ! -d /var/run/oops ]; then
	mkdir /var/run/oops
	chown proxy:proxy /var/run/oops
fi


case "$1" in
  start)
		echo -n "Starting $DESC: "
		case "$RUNNING_TYPE" in
			standalone)
				if [ -e $PIDFILE ] &&
					( $(kill -0 `cat $PIDFILE` 2>/dev/null) && [ $? -eq 0 ] ); then
						echo "$NAME is already running."
				else
					start-stop-daemon --chuid $USER:$GROUP --quiet --start --pidfile $PIDFILE \
						--exec $DAEMON -- $OOPS > /dev/null && echo "$NAME." || echo "error."
					sleep 1
				fi
			;;
			run)
				su proxy -c "$RUN $DAEMON $OOPS_RUN" && echo "$NAME."
			;;
		esac
		;;
  stop)
		echo -n "Stopping $DESC: "
		case "$RUNNING_TYPE" in
			standalone)
				if [ -e $PIDFILE ] &&
					( $(kill -0 `cat $PIDFILE` 2>/dev/null) && [ $? -eq 0 ] ); then
					#$DAEMONCTL stop > /dev/null && echo "$NAME." || echo "error."
					start-stop-daemon --chuid $USER:$GROUP --quiet --stop --pidfile $PIDFILE \
						--exec $DAEMON -- $OOPS > /dev/null && echo "$NAME." || echo "error."
					sleep 1
				else
					echo "$NAME is not running."
				fi
			;;
			run)
				su proxy -c "$STOP $DAEMON" && echo "$NAME."
				sleep 2; su proxy -c "kill -TERM `cat $PIDFILE` >/dev/null 2>&1" && echo ".."
			;;
		esac
		;;
  reload|force-reload)
		echo -n "Reloading $DESC configuration files: "
		$DAEMONCTL reconfigure > /dev/null && echo "$NAME." || echo "reconfigure error."
		#$DAEMONCTL verbosity=$VERBOSITY
	;;
  restart)
		$0 stop
		$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
