#!/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/share/gunicorn/initscript-helper.py
PID_DIR=/var/run/gunicorn
LOG_DIR=/var/log/gunicorn
CONF_DIR=/etc/gunicorn.d

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

. /lib/lsb/init-functions

Action() {
	mkdir -p $PID_DIR
	mkdir -m770 -p $LOG_DIR

	log_daemon_msg "$1"

	if PYTHONDONTWRITEBYTECODE=1 python $HELPER $CONF_DIR $PID_DIR $LOG_DIR $2
	then
		log_success_msg
	else
		log_failure_msg
		exit 1
	fi
}

case "$1" 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|status}" >&2
	exit 1
	;;
esac

exit 0
