#! /bin/sh
### BEGIN INIT INFO
# Provides:          couchpotato
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start couchpotato at boot time
# Description:       Start couchpotato
### END INIT INFO

#Author              QuidsUp
#Last Edited	     15 Jun 2014

#Required -- To be changed!
USER=you        #Set Ubuntu or Linux Mint username here.
APPPath=/home/you/CouchPotato/
PIDFile=/home/you/.couchpotato.pid

set -e

case "$1" in
  start)
    echo "Starting CouchPotato"
    start-stop-daemon --chdir $APPPath --chuid $USER --start \
     --pidfile $PIDFile --exec /usr/bin/python -- CouchPotato.py --daemon --pid_file=$PIDFile
  ;;
  stop)
    echo "Stopping CouchPotato"
    start-stop-daemon --stop --chuid $USER --pidfile $PIDFile
  ;;

  restart|force-reload)
    echo "Stopping CouchPotato for Restart"
    start-stop-daemon --stop --chuid $USER --pidfile $PIDFile
    echo "Stop Command Issued. Waiting for CouchPotato to Shutdown"
    sleep 25s
    echo "Restarting CouchPotato"
    start-stop-daemon --chdir $APPPath --chuid $USER --start \
     --pidfile $PIDFile --exec /usr/bin/python -- CouchPotato.py --daemon --pid_file=$PIDFile
  ;;

  status)
    echo "Checking Status of CouchPotato"
    if (test -f $PIDFile); then
      PIDVal=$(head -1 $PIDFile)
      if [ -e /proc/$PIDVal ]; then
        echo -n "CouchPotato Running on Process ID: "
        echo $PIDVal
        echo
        ps -H $PIDVal
      else
        echo "CouchPotato Not Running"
	exit 1
      fi
    else
      echo "No PID File found for CouchPotato"
    fi
  ;;

  *)
    echo "Usage: couchpotato {start|stop|restart|force-reload|status}" >&2
    exit 1
  ;;
esac

exit 0

