#! /bin/sh
### BEGIN INIT INFO
# Provides:          pootle
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Pootle Server
# Description:       Pootle is a web-based translation and a translation
#                    management tool
### END INIT INFO

# Pootle init script
#

set -e

[ -r /etc/default/pootle ] && . /etc/default/pootle

case "$POOTLE_ENABLE" in
	[Yy][Ee][Ss])
		;;
	*)
		echo "Pootle daemon not started (disabled by /etc/default/pootle)"
		exit 0
		;;
esac

# Defaults
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/PootleServer
PIDFILE=/var/run/pootle/pootle.pid
POOTLE_USER=pootle
POOTLE_GROUP=pootle
DESC="Pootle daemon: PootleServer"

OPTIONS="$POOTLE_OPTIONS"

case "$1" in
	start)
		if [ ! -d /var/run/pootle ]; then
			mkdir /var/run/pootle
			chown $POOTLE_USER:root /var/run/pootle
			chmod 755 /var/run/pootle
		fi
		echo -n "Starting $DESC..."
		if LC_ALL=C start-stop-daemon --start --quiet \
		    --oknodo \
		    --background \
		    --make-pidfile \
		    --pidfile $PIDFILE \
		    --exec /usr/bin/python \
		    --user $POOTLE_USER \
		    --chuid $POOTLE_USER:$POOTLE_GROUP \
		    --umask 002 \
		    --startas $DAEMON -- $OPTIONS >> /var/log/pootle 2>&1
		then
			echo "."
		else
			echo " (failed)."
			exit 1
		fi
		;;
	stop)
		echo -n "Stopping $DESC..."
		if start-stop-daemon --stop --quiet \
		    --oknodo \
		    --pidfile $PIDFILE \
		    --user $POOTLE_USER
		then
			rm -f $PIDFILE
			echo "."
		else
			echo " (failed)."
			exit 1
		fi
		;;
	restart|force-reload)
		sh $0 stop
		sleep 1
		sh $0 start
		;;
	status)
		echo -n "$DESC "
		if start-stop-daemon --stop --signal 0 --quiet \
		                     --pidfile $PIDFILE \
		                     --user $POOTLE_USER
		then
			echo "running"
			exit 0
		else
			if [ -e "$PIDFILE" ]
			then
				echo "failed"
				exit 1
			else
				echo "not running"
				exit 0
			fi
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|force-reload|status}"
		exit 1
		;;
esac

exit 0
