#!/bin/sh
### BEGIN INIT INFO
# Provides:          uwsgi-core
# 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 uWSGI server instance
### END INIT INFO

# Author: Leonid Borisenko <leo.borisenko@gmail.com>

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

NAME="custom_instance_name_here"     # Introduce the short server's name here
SCRIPTNAME=/etc/init.d/$NAME

DESC="custom configured app server"  # Introduce a short description here
DAEMON=/usr/bin/uwsgi-core           # Introduce the server's location here

UWSGI_UID=www-data                   # Introduce uWSGI uid here
UWSGI_GID=www-data                   # Introduve uWSGI gid here

UWSGI_RUNDIR=/run/uwsgi-core
UWSGI_SPECIFIC_RUNDIR="$UWSGI_RUNDIR/$NAME"

PIDFILE=$UWSGI_SPECIFIC_RUNDIR/pid

DAEMON_ARGS=" \
  --daemonize /var/log/uwsgi-core/${NAME}.log \
  --pidfile $PIDFILE \
  --uid $UWSGI_UID \
  --gid $UWSGI_GID \
  --xmlconfig /etc/uwsgi-core/${NAME}.xml \
"

# 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()
{
  INTERVAL_START
  INTERVAL_END
  WAITING=2 # seconds

  install -d -o root -g root -m 755 $UWSGI_RUNDIR
  install -d -o $UWSGI_UID -g $UWSGI_GID -m 755 $UWSGI_SPECIFIC_RUNDIR

  start-stop-daemon --start --quiet \
    --pidfile $PIDFILE \
    --exec $DAEMON \
    --test > /dev/null \
      || return 1

  # shellcheck disable=SC2086
  start-stop-daemon --start --quiet \
    --pidfile $PIDFILE \
    --exec $DAEMON -- $DAEMON_ARGS \
      || return 2

  INTERVAL_START=$(date +%s)
  INTERVAL_END=$(date +%s)

  # Wait until daemon getting to create pidfile.
  while [ ! -e "$PIDFILE" ]; do
    INTERVAL_END=$(date +%s)
    if [ $((INTERVAL_END-INTERVAL_START)) -gt $WAITING ]; then
      return
    fi
    sleep 1
  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

:
