#!/bin/sh
# Init script for openproject-rake-PROCESS_NUM
# Generated by pkgr, heavily borrowed from pleaserun
# Implemented based on LSB Core 3.1:
#   * Sections: 20.2, 20.3
#
### BEGIN INIT INFO
# Provides:          openproject-rake-PROCESS_NUM
# Required-Start:    openproject-rake
# Required-Stop:     openproject-rake
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       rake component of openproject (PROCESS_NUM)
### END INIT INFO

export PATH=/sbin:/usr/sbin:/bin:/usr/bin
export APP_PROCESS_INDEX=__PROCESS_INDEX__

name="openproject"
process_name="rake"
user="openproject"
group="openproject"
full_process_name="${name}-${process_name}-${APP_PROCESS_INDEX}"
pidfile="/var/run/${full_process_name}.pid"

start() {
  # Run the program!
  ${name} run ${process_name} >> /var/log/${name}/${process_name}-${APP_PROCESS_INDEX}.log 2>&1 &

  # Generate the pidfile from here. If we instead made the forked process
  # generate it there will be a race condition between the pidfile writing
  # and a process possibly asking for status.
  echo $! > $pidfile

  echo "${full_process_name} started."
  return 0
}

subpids() {
  local parent=$1
  ps -o pid --ppid ${parent} --noheaders
}

process_kill() {
  local pid=$1
  local sig=$2
  process_status $pid && kill -${sig} ${pid}
}

process_status() {
  local pid=$1
  if kill -0 $pid > /dev/null 2> /dev/null ; then
    return 0
  else
    return 2
  fi
}

stop_childs() {
  local pids=$1

  if [ ! "$pids" = "" ]; then
    echo "Subprocesses detected ($pids), attempting to kill them first"
    for pid in $pids ; do

      # start in subshell, otherwise pid variable pollutes parent functions
      (
        stop_childs $(subpids $pid)
      )

      process_kill $pid "TERM"

      for i in 1 2 3 4 5 ; do
        echo -n "Waiting for pid $pid to die... "
        process_status $pid || break
        echo ""
        sleep 1
      done

      if $(process_status $pid) ; then
        echo "subprocess $pid stop failed; still running."
        return 1
      else
        echo "stopped."
      fi
    done
  fi
}

force_stop_childs() {
  local pids=$1
  for pid in $pids ; do
    (
      force_stop_childs $(subpids $pid)
    )
    process_kill $pid "KILL"
  done
}


stop() {
  # Try a few times to kill TERM the program
  if status ; then
    local pid=`cat "$pidfile"`
    echo "Killing ${full_process_name} (pid $pid) with SIGTERM"

    # start in subshell, otherwise pid variable pollutes parent functions
    if ( stop_childs $(subpids $pid) ) ; then
      process_kill $pid "TERM"

      # Wait for it to exit.
      for i in 1 2 3 4 5 ; do
        echo "Waiting ${full_process_name} (pid $pid) to die..."
        status || break
        sleep 1
      done
    fi

    if status ; then
      echo "${full_process_name} stop failed; still running."
    else
      echo "${full_process_name} stopped."
    fi
  fi
}

status() {
  if [ -f "$pidfile" ] ; then
    local pid=`cat "$pidfile"`
    if kill -0 $pid > /dev/null 2> /dev/null ; then
      return 0
    else
      return 2 # program is dead but pid file exists
    fi
  else
    return 3 # program is not running
  fi
}

force_stop() {
  if status ; then
    pid=$(cat "$pidfile")
    stop
    # start in subshell, otherwise pid variable pollutes parent functions
    (
      force_stop_childs $(subpids $pid)
    )
    process_kill $pid "KILL"
  fi
}

case "$1" in
  start) status || start ;;
  stop) stop ;;
  force-stop) force_stop ;;
  status)
    status
    code=$?
    if [ $code -eq 0 ] ; then
      echo "${full_process_name} is running"
    else
      echo "${full_process_name} is not running"
    fi
    exit $code
    ;;
  restart)
    stop && start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|force-stop|status|restart}" >&2
    exit 3
  ;;
esac

exit $?
