#!/bin/sh 

# {{ ansible_managed }}

### BEGIN INIT INFO
# Provides:       cs_comments_service 
# Required-Start: $local_fs $syslog
# Required-Stop:  $local_fs $syslog
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Sinatra process for cs_comments_service 
### END INIT INFO

USER={{ forum_user }}
NAME="cs_comments_service"
RETVAL=0
APP_ROOT={{ forum_code_dir }}
DAEMON={{ forum_app_dir }}/.rbenv/shims/ruby
PID=/var/tmp/cs_comments_service.pid
OPTIONS="app.rb"

# source function library
. /lib/lsb/init-functions

start()
{
    echo "Starting $NAME."
    start-stop-daemon -b --start --pidfile ${PID} -m -c ${USER} -d ${APP_ROOT} --exec ${DAEMON} -- ${OPTIONS}
}

stop()
{
    if [ -f ${PID} ]; then
        echo "Stopping $NAME"
        kill $(cat ${PID})
    fi
}

reload()
{
    echo "Reloading $NAME:"
    if [ -f ${PID} ]
    then kill -HUP `cat ${PID}` && echo "OK" || echo "failed";
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        reload
        ;;
    reload)
        reload
        ;;
    force-reload)
        stop && start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        RETVAL=1
esac
exit $RETVAL
