#!/bin/bash -e

# Pyrone supervisor start/stop script
# suitable for all init.d-base distributions


#SUPERVISORD=/usr/local/bin/supervisord
#SUPERVISORCTL=/usr/local/bin/supervisorctl

# REMEMBER TO CHANGE THIS VARIABLE!
BLOG=/home/user/pyrone-blog

SUPERVISORD=$BLOG/env/bin/supervisord
SUPERVISORCTL=$BLOG/env/bin/supervisorctl
PIDFILE=$BLOG/env/supervisord.pid
OPTS="-c $BLOG/supervisord.conf"

test -x $SUPERVISORD || exit 0

. /lib/lsb/init-functions

export PATH="${PATH:+$PATH:}/usr/local/bin:/usr/sbin:/sbin"

start () {
    if test -f $PIDFILE; then
        # check is process with that PID is exists
        if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
            log_failure_msg "already running: PID file exists and valid"
            exit 1
        fi
    fi

	log_begin_msg "Starting Supervisor daemon manager..."
	$SUPERVISORD $OPTS || log_end_msg 1
	log_end_msg 0
}

stop () {
	log_begin_msg "Stopping Supervisor daemon manager..."
    kill -TERM `cat "$PIDFILE"` || log_end_msg 1
	log_end_msg 0
}

case "$1" in
  start)
    start
	;;

  stop)
    stop
	;;

  restart|reload|force-reload)
    stop
    start
    ;;

esac

exit 0
