#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          CouchPotato application instance
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts CouchPotato
# Description:       starts CouchPotato
### END INIT INFO

# Source function library.
#. /etc/init.d/functions
METHOD=''
RC_VALUE=0
rc_reset() {
RC_VALUE=0
}
rc_status() {
RC_VALUE=$?
}
rc_exit() {
if [ $RC_VALUE == 0 ]
 then 
   case "$METHOD" in
     'status')
       echo "...running"
       ;;
     'stop')
       echo "...stopped"
       ;;
     'start')
       echo "...running"
       ;;
     *)
       echo "...unknown"
       ;;
   esac
 else
   case "$1" in
     'stop') 
       echo "...stopped"
       ;;
     *)
       echo "...failed"
       ;;
   esac
 fi 
 exit $RC_VALUE
}
# Reset status
rc_reset

prog=couchpotato

# Source couchpotato configuration
if [ -f /etc/sysconfig/couchpotato ]; then
        . /etc/sysconfig/couchpotato
fi

## Edit user configuation in /etc/sysconfig/couchpotato to change
## the defaults
CP_USER=${CP_USER:-couchpotato}
CP_HOME=${CP_HOME:-/usr/lib/couchpotato}
DATADIR=${CP_DATA:-/var/lib/couchpotato/.config}
PIDFILE=${CP_PIDFILE:-/var/run/couchpotato.pid}
##

# create PID directory if not exist and ensure the couchpotato user can write to it
pidpath=`dirname $PIDFILE`
test -d $pidpath || install -d -m755 $pidpath
test -d $DATADIR || install -d -m700 -o $CP_USER -g $CP_USER "$DATADIR"

# workaround if startproc choaks
make_pid_by_hand() {
sleep 3
PID=$( /usr/bin/ps -ef | /usr/bin/grep CouchPotato.py | /usr/bin/awk '/python / {print $2}' )
if [ "${PID} " == " " ]
then
 : # bail?  or ignore it
else
 /bin/echo ${PID} >$PIDFILE  
fi
}

check_pid_by_hand() {
if [ -f "$PIDFILE" ]
then
PID=$( cat "$PIDFILE" )
PIDD=$( /usr/bin/ps -ef | /usr/bin/grep CouchPotato.py | /usr/bin/awk '/python / {print $2}' )
if [ "${PIDD} " != " " ] && [ "${PID}" == "${PIDD}" ]
then
 /usr/bin/true
else
 /usr/bin/false
fi
else
 /usr/bin/false
fi
}

options=" --daemon --pid_file=$PIDFILE --data_dir=$DATADIR"
CP_BIN="$CP_HOME/CouchPotato.py"

start() {
        METHOD='start'
        # Start daemon.
        echo -n $"Starting $prog: "
        startproc -u ${CP_USER} -p $PIDFILE /usr/bin/python $CP_BIN ${options}
        # Remember status and be verbose
        test -f $PIDFILE || make_pid_by_hand
        rc_status -v
}

stop() {
        METHOD='stop'
        echo -n $"Shutting down $CP_BIN: "
        killproc -p $PIDFILE /usr/bin/python
        rc_status -v
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        /sbin/checkproc -p $PIDFILE /usr/bin/python
        if [ ! $? -eq 0 ] 
        then
          check_pid_by_hand
        else
          rc_reset
        fi
        rc_status -v
        METHOD='status'
        ;;
  restart|force-reload)
        stop
        start
        ;;
  try-restart|condrestart)
        if status $prog > /dev/null; then
            stop
            start
        fi
        ;;
  reload)
        exit 3
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
esac
rc_exit
