#!/bin/bash

### BEGIN INIT INFO
# Provides:          zammad
# Required-Start:    $local_fs $remote_fs $network $syslog 
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Zammad application
# Description:       Zammad application
### END INIT INFO


APP_ROOT="/home/zammad/zammad"
PID_PATH="$APP_ROOT/tmp/pids"
WEB_SERVER_PID="$PID_PATH/puma.pid"
WEBSOCKET_SERVER_PID="$PID_PATH/websocket.pid"
SCHEDULER_SERVER_PID="$PID_PATH/websocket.pid"

APP_USER="zammad"
PUMA_OPTS="-p 3000 -d -e production --pidfile $WEB_SERVER_PID"
WEBSOCKET_OPTS="-d"
SCHEDULER_OPTS=""

NAME="zammad"
DESC="Zammad application"


check_daemons() {
    PUMA_PID=-1
    PUMA_OK=1
    if [ -f $WEB_SERVER_PID ]; then
      PUMA_PID=$(pgrep -F $WEB_SERVER_PID)
      PUMA_OK=$?
    fi
    WEBSOCKET_PID=-1
    WEBSOCKET_OK=1
    if [ -f $WEBSOCKET_SERVER_PID ]; then
      WEBSOCKET_PID=$(pgrep -F $WEBSOCKET_SERVER_PID)
      WEBSOCKET_OK=$?
    fi
    SCHEDULER_PID=1
    SCHEDULER_OK=1
    if [ -f $SCHEDULER_SERVER_PID ]; then
      SCHEDULER_PID=$(pgrep -F $SCHEDULER_SERVER_PID)
      SCHEDULER_OK=$?
    fi
}

execute() {
  sudo -u $APP_USER -H bash -l -c "$1"
}

start() {
  cd $APP_ROOT
  check_daemons
  if [ $PUMA_OK -eq 0 ]; then
    echo Puma is already running
  else
    execute "RAILS_ENV=production puma $PUMA_OPTS"
  fi
  if [ $WEBSOCKET_OK -eq 0 ]; then
    echo Websocket server is already running
  else
    execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
  fi
  execute "RAILS_ENV=production script/scheduler.rb start $SCHEDULER_OPTS"
  echo "$DESC started"
}

stop() {
  cd $APP_ROOT
  check_daemons
  if [ "$PUMA_OK" -eq 0 -o $WEBSOCKET_OK -eq 0 ]; then
    ## Program is running, stop it.
    if [ $PUMA_OK -eq 0 ]; then
      kill -QUIT $PUMA_PID
      test -f "$WEB_SERVER_PID" && rm -f "$WEB_SERVER_PID" 
    fi
    if [ $WEBSOCKET_OK -eq 0 ]; then
      execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
      test -f "$WEBSOCKET_SERVER_PID" && rm -f "$WEBSOCKET_SERVER_PID" 
    fi 
    execute "RAILS_ENV=production script/scheduler.rb stop $SCHEDULER_OPTS"
    echo "$DESC stopped"
  else
    ## Program is not running, exit with error.
    echo "Error! $DESC is not started!"
    exit 1
  fi
}

restart() {
  cd $APP_ROOT
  check_daemons
  if [ "$PUMA_OK" -eq 0 -o WEBSOCKET_OK -eq 0 ]; then
    echo "Restarting $DESC..."
    kill -USR2 $PUMA_PID
    execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
    execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
    execute "RAILS_ENV=production script/scheduler.rb restart $SCHEDULER_OPTS"
    echo "$DESC restarted."
  else
    echo "Error, $NAME not running!"
    exit 1
  fi
}

status() {
  cd $APP_ROOT
  check_daemons
  if [ $PUMA_OK -eq 0 ]; then
    echo "$DESC / Puma (pid $PUMA_PID) is running."
  else
    echo "$DESC / Puma (pid $PUMA_PID) is not running."
  fi
  if [ $WEBSOCKET_OK -eq 0 ]; then
    echo "Websocket server (pid $WEBSOCKET_PID) is running."
  else
    echo "Websocket server (pid $WEBSOCKET_PID) is not running."
  fi
  execute "RAILS_ENV=production script/scheduler.rb status $SCHEDULER_OPTS"
}

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root"
    exit 1
fi

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

exit 0
