#!/bin/sh
### BEGIN INIT INFO
# Provides:          rqworker
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop custom rqworker instance
### END INIT INFO

# Author: Leonid Borisenko <leo.borisenko@gmail.com>
# modified for Pootle by: Fran Boon <fran@aidiq.com>

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin

SCRIPTNAME=/etc/init.d/rqworker

DESC="Pootle rqworker"  # Introduce a short description here
DAEMON=/home/pootle/env/bin/pootle        # Introduce the server's location here
#DAEMON=/home/pootle/env/bin/python        # Introduce the server's location here

UID=pootle                   # Introduce uid here
GID=pootle                   # Introduve gid here

RUNDIR=/run/rqworker

PIDFILE=$RUNDIR/pid

DAEMON_ARGS=" \
  rqworker \
"

# Exit if the package is not installed
[ -x $DAEMON ] || exit 0

# 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

# Return:
#  0 if daemon has been started
#  1 if daemon was already running
#  2 if daemon could not be started
do_start()
{
  install -d -o $UID -g $GID -m 755 $RUNDIR

  cd /home/pootle
  . env/bin/activate
  export PYTHONPATH=.:/home/pootle/env/lib/python2.7/site-packages/:/home/pootle/env/lib/python2.7/site-packages/pootle/apps/
  #exec start-stop-daemon --start  --chuid pootle --exec $DAEMON /home/pootle/env/bin/pootle rqworker -- >> /home/pootle/rqworker.log 2>&1 &
  exec start-stop-daemon --start  --chuid pootle --exec $DAEMON rqworker -- >> /home/pootle/rqworker.log 2>&1 &

  local INTERVAL_START=$(date +%s)
  local INTERVAL_END=$(date +%s)
  local WAITING=2 # seconds

  # Wait until daemon getting to create pidfile.
  while [ ! -e "$PIDFILE" ]; do
    INTERVAL_END=$(date +%s)
    if [ $(expr $INTERVAL_END - $INTERVAL_START) -gt $WAITING ]; then
      return
    fi
    sleep 0.05
  done

  chown root:root $PIDFILE
  chmod 644 $PIDFILE

  return 0
}

# Return:
#  0 if daemon has been stopped
#  1 if daemon was already stopped
#  2 if daemon could not be stopped
#  other if a failure occurred
do_stop()
{
  start-stop-daemon --stop --quiet \
    --retry=QUIT/30/KILL/5 \
    --pidfile $PIDFILE \
    --exec $DAEMON

  RETVAL="$?"
  [ "$RETVAL" = 2 ] && return 2

  rm -rf $RUNDIR

  return "$RETVAL"
}

# Return:
#  0 if daemon has been reloaded
#  3 if daemon could not be reloaded
do_reload()
{
  start-stop-daemon --stop --quiet \
    --signal=HUP \
    --pidfile $PIDFILE \
    --exec $DAEMON

  RETVAL="$?"

  # There is no such process, nothing to reload!
  [ "$RETVAL" = 1 ] && RETVAL=3

  return "$RETVAL"
}

# Return:
#  0 if daemon has been reloaded
#  3 if daemon could not be reloaded
do_force_reload()
{
  start-stop-daemon --stop --quiet \
    --signal=TERM \
    --pidfile $PIDFILE \
    --exec $DAEMON

  RETVAL="$?"

  # There is no such process, nothing to reload!
  [ "$RETVAL" = 1 ] && RETVAL=3

  return "$RETVAL"
}

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    log_end_msg "$?"
  ;;

  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    log_end_msg "$?"
  ;;

  status)
    status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" \
      && exit 0 \
      || exit $?
  ;;

  reload)
    log_daemon_msg "Reloading $DESC" "$NAME"
    do_reload
    log_end_msg "$?"
  ;;

  force-reload)
    log_daemon_msg "Forced reloading $DESC" "$NAME"
    do_force_reload
    log_end_msg "$RETVAL"
  ;;

  restart)
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0)
        do_start
        log_end_msg "$?"
      ;;
      *)
        # Failed to stop
        log_end_msg 1
      ;;
    esac
  ;;

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

:
