#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          sickgear
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:     $local_fs $network $remote_fs
# Should-Start:      $NetworkManager
# Should-Stop:       $NetworkManager
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts instance of SickGear
# Description:       starts instance of SickGear using start-stop-daemon
### END INIT INFO

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Source SickGear configuration
if [ -f /etc/default/sickgear ]; then
    . /etc/default/sickgear
else
    [ "${VERBOSE}" != no ] && echo "/etc/default/sickgear not found. Using default settings.";
fi

## Don't set -e
## Don't edit this file!
## Edit user configuration in /etc/default/sickgear to change
##
## SG_USER=         #$RUN_AS, username to run sickgear under, the default is sickgear
## SG_GROUP=        #$RUN_GROUP, group to run sickgear under, the default is sickgear
## SG_HOME=         #$APP_PATH, the location of sickgear.py, the default is /opt/sickgear
## SG_DATA=         #$DATA_DIR, the location of sickbeard.db, cache, logs, the default is /opt/sickgear
## SG_PIDFILE=      #$PID_FILE, the location of sickgear.pid, the default is /var/run/sickgear/sickgear.pid
## PYTHON_BIN=      #$DAEMON, the location of the python binary, the default is /usr/bin/python
## SG_OPTS=         #$EXTRA_DAEMON_OPTS, extra cli option for sickgear, i.e. " --config=/home/sickgear/config.ini"
## SSD_OPTS=        #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users"
##
## EXAMPLE if want to run as different user
## add SG_USER=username to /etc/default/sickgear
## otherwise default sickgear is used

# Script name
NAME=$(basename "$0")

# App name
DESC=SickGear

## The defaults
# Run as username
RUN_AS=${SG_USER-sickgear}

# Run as group
RUN_GROUP=${SG_GROUP-sickgear}

# Path to app SG_HOME=path_to_app_sickgear.py
APP_PATH=${SG_HOME-/opt/sickgear}

# Data directory where sickbeard.db, cache and logs are stored
DATA_DIR=${SG_DATA-/opt/sickgear}

# Path to store PID file
PID_FILE=${SG_PIDFILE-/var/run/sickgear/sickgear.pid}

# path to python bin
DAEMON=${PYTHON_BIN-/usr/bin/python}

# Extra daemon option like: SG_OPTS=" --config=/home/sickgear/config.ini"
EXTRA_DAEMON_OPTS=${SG_OPTS-}

# Extra start-stop-daemon option like START_OPTS=" --group=users"
EXTRA_SSD_OPTS=${SSD_OPTS-}
##

PID_PATH=$(dirname $PID_FILE)
DAEMON_OPTS=" sickgear.py -q --daemon --nolaunch --pidfile=${PID_FILE} --datadir=${DATA_DIR} ${EXTRA_DAEMON_OPTS}"

##

test -x $DAEMON || exit 0

# Create PID directory if not exist and ensure the SickGear user can write to it
if [ ! -d $PID_PATH ]; then
    mkdir -p $PID_PATH
    chown $RUN_AS $PID_PATH
fi

if [ ! -d $DATA_DIR ]; then
    mkdir -p $DATA_DIR
    chown $RUN_AS $DATA_DIR
fi

if [ -e $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if ! kill -0 $PID > /dev/null 2>&1; then
        [ "$VERBOSE" != no ] && echo "Removing stale $PID_FILE"
        rm -f $PID_FILE
    fi
fi

start_sickgear() {
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon -d $APP_PATH -c $RUN_AS --group=${RUN_GROUP} $EXTRA_SSD_OPTS --start --quiet --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS
    RETVAL="$?"
    case "${RETVAL}" in
        # Service was started or was running already
        0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
        # Service couldn't be started
        2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
    esac
    [ "${RETVAL}" = 2 ] && return 2
    return 0
}

stop_sickgear() {
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --pidfile $PID_FILE --quiet --retry TERM/30/KILL/5
    RETVAL="$?"
    case "${RETVAL}" in
        # Service was stopped or wasn't running
        0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
        # Service couldn't be stopped
        2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
    esac
    [ "${RETVAL}" = 2 ] && return 2
    [ -f "${PID_FILE}" ] && rm -f ${PID_FILE}
    return 0
}

case "$1" in
    start)
        start_sickgear
        exit $?
        ;;
    stop)
        stop_sickgear
        exit $?
        ;;

    restart|force-reload)
        stop_sickgear
        sleep 2
        start_sickgear
        return $?
        ;;
    status)
        status_of_proc -p "$PID_FILE" "$DAEMON" "$DESC"
        exit 0
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0