#!/bin/bash
### BEGIN INIT INFO
# Provides:          kolibri-server
# Required-Start:    $local_fs $network $remote_fs $syslog $named nginx
# Required-Stop:     $local_fs $network $remote_fs $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop uwsgi & nginx setup for Kolibri
# Description:       Multicore server setup for Kolibri, an offline education platform
### END INIT INFO

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="kolibri-server"
NAME=kolibri-server
DAEMON_UWSGI=/usr/bin/uwsgi
MAIN=/usr/bin/kolibri
CONFIG_FILE=/etc/default/kolibri
PIDFILE_UWSGI=/var/run/$NAME/uwsgi.pid
PIDFILE_UWSGI_HASHI=/var/run/$NAME/uwsgi_hashi.pid
# Exit if the package is not installed
[ -x "$MAIN" ] || exit 0

# Prefer runuser over su
# See: https://github.com/learningequality/kolibri-installer-debian/issues/90
# Check if runuser exists - it doesn't on 14.04
if which runuser > /dev/null
then
  SU_COMMAND="runuser"
else
  SU_COMMAND="su"
fi

# Read configuration variable file if it is present
[ -r $CONFIG_FILE ] || { echo "Configuration not found: $CONFIG_FILE" && exit 1 ;}
[ -r $CONFIG_FILE ] && . $CONFIG_FILE

# Crash if a sub-process crashes. This is important to discover
# errors in this script. It might be changed in the future.
set -e

# Check that env vars were set after sourcing $CONFIG_FILE
[ "$KOLIBRI_USER"=="" ] || { echo "$KOLIBRI_USER not set" && exit 1 ;}
[ "$KOLIBRI_HOME"=="" ] || { echo "$KOLIBRI_HOME not set" && exit 1 ;}

KOLIBRI_GID=`id -g $KOLIBRI_USER`

DAEMON_UWSGI_ARGS="--ini /etc/kolibri/dist/uwsgi.ini --uid=$KOLIBRI_USER --gid=$KOLIBRI_GID \
  --env=KOLIBRI_HOME=$KOLIBRI_HOME --daemonize=$KOLIBRI_HOME/logs/uwsgi.log --pidfile=$PIDFILE_UWSGI \
  --logfile-chown"

DAEMON_HASHI_UWSGI_ARGS="--ini /etc/kolibri/dist/hashi_uwsgi.ini --uid=$KOLIBRI_USER \
  --gid=$KOLIBRI_GID --env=KOLIBRI_HOME=$KOLIBRI_HOME\
  --daemonize=$KOLIBRI_HOME/logs/hashi_uwsgi.log --pidfile=$PIDFILE_UWSGI_HASHI \
  --logfile-chown"

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

export KOLIBRI_INSTALLATION_TYPE="kolibriserver"

do_start()
{
  # Return
  #   0 if daemon has been started
  #   1 if daemon was already running
  #   2 if daemon could not be started
  # upgrade nginx and kolibri configurations:
  $SU_COMMAND $KOLIBRI_USER -c "/usr/share/kolibri-server/kolibri_server_setup.py"
  # ensure kolibri application is running:
  do_check_nginx
  # ensure PIDFILE_UWSGI is not a world-writable pidfile
  chmod 660 $PIDFILE_UWSGI || true
  chmod 660 $PIDFILE_UWSGI_HASHI || true
  $SU_COMMAND $KOLIBRI_USER -c "$KOLIBRI_COMMAND services &"
  mkdir -p /var/run/$NAME
  chown "$KOLIBRI_USER" /var/run/$NAME
  start-stop-daemon --start --quiet --exec $DAEMON_UWSGI --test -- $DAEMON_UWSGI_ARGS > /dev/null \
    || return 1
  start-stop-daemon --start --quiet --exec $DAEMON_UWSGI --  $DAEMON_UWSGI_ARGS || return 2
  start-stop-daemon --start --quiet --pidfile=$PIDFILE_UWSGI_HASHI  --startas $DAEMON_UWSGI -- $DAEMON_HASHI_UWSGI_ARGS\
    || return 2
  return 0
}

#
# Function that stops the daemon/service
#
do_stop_uwsgi()
{
  # 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
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE_UWSGI --name uwsgi
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE_UWSGI_HASHI --name uwsgi
  RETVAL="$?"
  # Many daemons don't delete their pidfiles when they exit.
  rm -f $PIDFILE_UWSGI
  rm -f $PIDFILE_UWSGI_HASHI
  rm -f /tmp/kolibri_uwsgi.sock
  rm -f /tmp/kolibri_hashi_uwsgi.sock
  [ "$RETVAL" = 2 ] && return 2
  return 0
}


do_check_nginx()
{
  if ps ax | grep -v grep | grep nginx > /dev/null
  then
      service nginx reload
  else
      $SU_COMMAND $KOLIBRI_USER -c "$KOLIBRI_COMMAND stop"
      $SU_COMMAND $KOLIBRI_USER -c "$KOLIBRI_COMMAND services"
      service nginx start
  fi
}

do_stop() {
  $SU_COMMAND $KOLIBRI_USER -c "$KOLIBRI_COMMAND stop"
  do_stop_uwsgi

  retval=$?
  return $retval
}


case "$1" in
  start)
    if [ -s "$CONFIG_FILE" ]; then
      [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
      do_start
      case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
      esac
      service nginx reload
    else
      [ "$VERBOSE" != no ] && log_warning_msg "$NAME daemon not configured, not starting... \(run $SETUP\)"
    fi
  ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
  ;;
  status)
    status_of_proc "$DAEMON_UWSGI" "uwsgi" && exit 0 || exit $?
  ;;

  restart|force-reload)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        if [ -s "$CONFIG_FILE" ]; then
        do_start
        case "$?" in
          0) log_end_msg 0 ;;
          1) log_end_msg 1 ;; # Old process is still running
          *) log_end_msg 1 ;; # Failed to start
        esac
        else
            [ "$VERBOSE" != no ] && log_warning_msg "$NAME daemon not configured, not starting... \(run $SETUP\)"
        fi
    ;;
    *)
      # Failed to stop
      log_end_msg 1
    ;;
    esac
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
  ;;
esac
