#!/bin/sh

# {{ ansible_managed }}

# etherpad-lite init script based on:
# https://github.com/ether/etherpad-lite/wiki/How-to-deploy-Etherpad-Lite-as-a-service

### BEGIN INIT INFO
# Provides:          etherpad-lite
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Should-Start:      postgresql mysql
# Should-Stop:       postgresql mysql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts etherpad lite
# Description:       starts etherpad lite using start-stop-daemon
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/node/bin"
LOGFILE="{{ etherpad_log_dir }}/etherpad-lite.log"
EPLITE_DIR="/usr/share/etherpad-lite"
EPLITE_BIN="bin/safeRun.sh"
USER="etherpad-lite"
GROUP="etherpad-lite"
DESC="Etherpad Lite"
NAME="etherpad-lite"

DEFAULTS="/etc/default/etherpad-lite"

# Load defaults if available
# shellcheck disable=SC1090
[ -r "$DEFAULTS" ] && . "$DEFAULTS"

set -e

# shellcheck disable=SC1091
. /lib/lsb/init-functions

start() {
  echo "Starting $DESC... "

    start-stop-daemon --start --chuid "$USER:$GROUP" --background --make-pidfile --pidfile "/var/run/${NAME}.pid" --exec "$EPLITE_DIR/$EPLITE_BIN" -- "$LOGFILE" || true
  echo "done"
}

#We need this function to ensure the whole process tree will be killed
killtree() {
    _pid="$1"
    _sig="${2-TERM}"
    for _child in $(ps -o pid --no-headers --ppid "${_pid}"); do
        killtree "${_child}" "${_sig}"
    done
    kill "-${_sig}" "${_pid}"
}

stop() {
  echo "Stopping $DESC... "
  if test -f "/var/run/${NAME}.pid"; then
    while test -d "/proc/$(cat /var/run/${NAME}.pid)"; do
      killtree "$(cat /var/run/${NAME}.pid)" 15
      sleep 0.5
    done
    rm "/var/run/${NAME}.pid"
  fi
  echo "done"
}

status() {
  status_of_proc -p /var/run/$NAME.pid "" "etherpad-lite" && exit 0 || exit $?
}

case "$1" in
  start)
      start
      ;;
  stop)
    stop
      ;;
  restart)
      stop
      start
      ;;
  status)
      status
      ;;
  *)
      echo "Usage: $NAME {start|stop|restart|status}" >&2
      exit 1
      ;;
esac

exit 0
