#!/bin/sh
#
## Don't edit this file
## Edit user configuation in /etc/default/tautulli to change
##
## Make sure init script is executable
## sudo chmod +x /path/to/init.ubuntu
##
## Install the init script
## sudo ln -s /path/to/init.ubuntu /etc/init.d/tautulli
##
## Create the tautulli daemon user:
## sudo adduser --system --no-create-home tautulli
##
## Make sure /opt/Tautulli is owned by the tautulli user
## sudo chown tautulli:nogroup -R /opt/Tautulli
##
## Touch the default file to stop the warning message when starting
## sudo touch /etc/default/tautulli
##
## To start Tautulli automatically
## sudo  update-rc.d tautulli defaults
##
## To start/stop/restart Tautulli
## sudo service tautulli start
## sudo service tautulli stop
## sudo service tautulli restart
##
## TAUTULLI_USER=         #$RUN_AS, username to run Tautulli under, the default is tautulli
## TAUTULLI_HOME=         #$APP_PATH, the location of Tautulli.py, the default is /opt/Tautulli
## TAUTULLI_DATA=         #$DATA_DIR, the location of plexpy.db, cache, logs, the default is /opt/Tautulli
## TAUTULLI_PIDFILE=      #$PID_FILE, the location of tautulli.pid, the default is /var/run/tautulli/tautulli.pid
## PYTHON_BIN=      #$DAEMON, the location of the python binary, the default is /usr/bin/python
## TAUTULLI_OPTS=         #$EXTRA_DAEMON_OPTS, extra cli option for Tautulli, i.e. " --config=/home/Tautulli/config.ini"
## SSD_OPTS=        #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users"
## TAUTULLI_PORT=         #$PORT_OPTS, hardcoded port for the webserver, overrides value in config.ini
##
## EXAMPLE if want to run as different user
## add TAUTULLI_USER=username to /etc/default/tautulli
## otherwise default tautulli is used
#
### BEGIN INIT INFO
# Provides:          tautulli
# 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 Tautulli
# Description:       starts instance of Tautulli using start-stop-daemon
### END INIT INFO

# Script name
NAME=tautulli

# App name
DESC=Tautulli

SETTINGS_LOADED=FALSE

. /lib/lsb/init-functions

# Source Tautulli configuration
if [ -f /etc/default/tautulli ]; then
    SETTINGS=/etc/default/tautulli
else
    log_warning_msg "/etc/default/tautulli not found using default settings.";
fi

check_retval() {
    if [ $? -eq 0 ]; then
        log_end_msg 0
        return 0
    else
        log_end_msg 1
        exit 1
    fi
}

load_settings() {
    if [ $SETTINGS_LOADED != "TRUE" ]; then
        . $SETTINGS

        ## The defaults
        # Run as username
        RUN_AS=${TAUTULLI_USER-tautulli}

        # Path to app TAUTULLI_HOME=path_to_app_Tautulli.py
        APP_PATH=${TAUTULLI_HOME-/opt/Tautulli}

        # Data directory where plexpy.db, cache and logs are stored
        DATA_DIR=${TAUTULLI_DATA-/opt/Tautulli}

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

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

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

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

        # Hardcoded port to run on, overrides config.ini settings
        [ -n "$TAUTULLI_PORT" ] && {
            PORT_OPTS=" --port=${TAUTULLI_PORT} "
        }

        DAEMON_OPTS=" Tautulli.py --quiet --daemon --nolaunch --pidfile=${PID_FILE} --datadir=${DATA_DIR} ${PORT_OPTS}${EXTRA_DAEMON_OPTS}"

        SETTINGS_LOADED=TRUE
    fi

    [ -x $DAEMON ] || {
        log_warning_msg "$DESC: Can't execute daemon, aborting. See $DAEMON";
        return 1;}

    return 0
}

load_settings || exit 0

is_running () {
    # returns 1 when running, else 0.
    if [ -e $PID_FILE ]; then
      PID=`cat $PID_FILE`

      RET=$?
      [ $RET -gt 1 ] && exit 1 || return $RET
    else
      return 1
    fi
}

handle_pid () {
    PID_PATH=`dirname $PID_FILE`
    [ -d $PID_PATH ] || mkdir -p $PID_PATH && chown -R $RUN_AS $PID_PATH > /dev/null || {
        log_warning_msg "$DESC: Could not create $PID_FILE, See $SETTINGS, aborting.";
        return 1;}

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

handle_datadir () {
    [ -d $DATA_DIR ] || mkdir -p $DATA_DIR && chown -R $RUN_AS $DATA_DIR > /dev/null || {
        log_warning_msg "$DESC: Could not create $DATA_DIR, See $SETTINGS, aborting.";
        return 1;}
}

handle_updates () {
    chown -R $RUN_AS $APP_PATH > /dev/null || {
        log_warning_msg "$DESC: $APP_PATH not writable by $RUN_AS for web-updates";
        return 0; }
}

start_tautulli () {
    handle_pid
    handle_datadir
    handle_updates
    if ! is_running; then
        log_daemon_msg "Starting $DESC"
        start-stop-daemon -o -d $APP_PATH -c $RUN_AS --start $EXTRA_SSD_OPTS --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS
        check_retval
    else
        log_success_msg "$DESC: already running (pid $PID)"
    fi
}

stop_tautulli () {
    if is_running; then
        log_daemon_msg "Stopping $DESC"
        start-stop-daemon -o --stop --pidfile $PID_FILE --retry 15
        check_retval
    else
        log_success_msg "$DESC: not running"
    fi
}

case "$1" in
    start)
        start_tautulli
        ;;
    stop)
        stop_tautulli
        ;;
    restart|force-reload)
        stop_tautulli
        start_tautulli
        ;;
    status)
        status_of_proc -p "$PID_FILE" "$DAEMON" "$DESC"
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0