#!/bin/sh
### BEGIN INIT INFO
# Provides:		gunicorn
# Required-Start:	$all
# Required-Stop:	$all
# Should-Start:		$local_fs
# Should-Stop:		$local_fs
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=gunicorn
DESC="Gunicorn workers"

HELPER=/usr/sbin/gunicorn-debian
PID_DIR=/var/run/gunicorn
LOG_DIR=/var/log/gunicorn
CONF_DIR=/etc/gunicorn.d

test -x $HELPER || exit 0

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

. /lib/lsb/init-functions

Action() {
	mkdir -p $PID_DIR
	mkdir -p $LOG_DIR
	chmod 750 $LOG_DIR
	chown root:adm $LOG_DIR

	log_daemon_msg "$1"
	shift

	if $HELPER \
		--conf-dir=$CONF_DIR \
		--pid-dir=$PID_DIR \
		--log-dir=$LOG_DIR \
		"$@"
	then
		log_success_msg
	else
		log_failure_msg
		exit 1
	fi
}

action="$1"
shift

case "$action" in
  start)
  	Action "Starting $DESC" start "$@"
	;;
  stop)
  	Action "Stopping $DESC" stop "$@"
	;;
  reload)
  	Action "Reloading $DESC" reload "$@"
	;;
  restart|force-reload)
	$0 stop "$@"
	$0 start "$@"
	;;
  *)
	echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|force-reload} [configs]" >&2
	exit 1
	;;
esac

exit 0
