#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          sickrage
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: starts SickBeard
# Description:       starts SickBeard
### END INIT INFO

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

SICKBEARD_BIN=/usr/share/sickrage/SickBeard.py
SICKBEARD_CONFIG=/etc/sysconfig/sickrage

test -x $SICKBEARD_BIN || { echo "$SICKBEARD_BIN not installed"; if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }
test -r $SICKBEARD_CONFIG || { echo "$SICKBEARD_CONFIG missing"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }
 
# Read config
. $SICKBEARD_CONFIG


# Source SickBeard configuration
PROG=sickrage
PYTHON_BIN=/usr/bin/python
LOCKFILE=/var/lock/$PROG
OPTIONS="${OPTIONS}  --daemon --nolaunch --PIDFILE=${PIDFILE} --DATADIR=${DATADIR}"

# create PID directory if not exist and ensure the SickBeard user can write to it
if [ ! -d $pidpath ]; then
        mkdir -p $pidpath
        chown $USERNAME $pidpath
fi

if [ ! -d $DATADIR ]; then
        mkdir -p $DATADIR
        chown $USERNAME $DATADIR
fi

# See how we were called.
case "$1" in
  start)
        # Start daemon.
        echo -n $"Starting $PROG: "
        startproc -u ${USERNAME} -p ${PIDFILE} -n ${NICE} ${PYTHON_BIN} ${SICKBEARD_BIN} ${OPTIONS}
        # Remember status and be verbose
        rc_status -v
        ;;
  stop)
        echo -n $"Shutting down $PROG: "
        killproc -p ${PIDFILE} python
        # Remember status and be verbose
        rc_status -v
        ;;
  status)
        status $PROG
        rc_status -v
        ;;
  restart|force-reload)
        $0 stop
        $0 start
        rc_status
        ;;
  try-restart|condrestart)
        if status $PROG > /dev/null; then
           $0 stop
           $0 start
        rc_status
        fi
        ;;
  reload)
        exit 3
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
esac
