#!/bin/sh
#
# percona-qan-api  This shell script takes care of starting and stopping
#                  the percona-qan-api service.
#
# chkconfig: 2345 65 35
# description: Percona Query Analytics App
#
### BEGIN INIT INFO
# Provides:          percona-qan-api
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop percona-qan-api
# Description:       Percona Query Analytics App
### END INIT INFO

export LANG="en_US"

SERVICE="percona-qan-api"
BASEDIR="/usr/local/percona/qan-api"

if [ ! -d "$BASEDIR" ]; then
   echo "$BASEDIR does not exist. Check that $SERVICE has been installed correctly." >&2
   exit 1
fi

cd "$BASEDIR"

# Source the api start script so we get all its global vars and functions here.
. "$BASEDIR/start"

START_TIMEOUT="5"
STOP_TIMEOUT="10"

# Allow configuration overrides in /etc/sysconfig/$SERVICE
CONFIGFILE="/etc/sysconfig/$SERVICE"
[ -x $CONFIGFILE ] && . $CONFIGFILE
 
# Change the current directory to the location of the script

# Check for ps in path
ps >/dev/null 2>&1
if [ $? -ne 0 ]; then
     echo "Unable to locate 'ps'." >&2
     echo "Please report this with the location on your system." >&2
     exit 1
fi

# Build the nice clause
if [ "X$PRIORITY" = "X" ]
then
    CMDNICE=""
else
    CMDNICE="nice -$PRIORITY"
fi

# seq N, return 1, ..., 5
_seq() {
   local i="$1"
   awk "BEGIN { for(i=1; i<=$i; i++) print i; }"
}

start() {
   if app_is_running; then
      echo "$SERVICE is already running (PID $PID)"
      exit 0
   fi

   echo "Starting $SERVICE on $LISTEN..."

   BG="yes"
   QUIET="yes"
   start_app # in $BASEDIR/start

   local ok=""
   for x in $(_seq $START_TIMEOUT); do
       if app_is_running; then
           ok="ok"
           break
       fi
       sleep 1
   done

   # Process is started in background so $? does not have its exit status.
   # Instead, check that it's running by trying to get its PID.
   if [ ! "$ok" ]; then
      echo "FAIL" >&2
      echo "Check $LOGFILE for details" >&2
      exit 1
   fi

   echo "OK"
   return 0
}

stop() {
   FIXPIDFILE="no"
   if ! app_is_running; then
      echo "$SERVICE is not running"
      return 0
   fi

   echo "Stopping $SERVICE on $LISTEN..."

   kill "$PID" >/dev/null
   if [ $? -ne 0 ]; then
      echo "Cannot stop $SERVICE: kill returned non-zero exit status: $?" >&2
      return 1
   fi

   for x in $(_seq $STOP_TIMEOUT); do
      app_pid
      [ -z "$PID" ] && break
      sleep 1
   done

   app_pid 
   if [ "$PID" ]; then
      echo "Timeout waiting for $SERVICE to stop, trying kill -9 $PID..."
      kill -9 $PID
      sleep 1
   fi

   app_pid
   if [ "$PID" ]; then
      echo "FAIL" >&2
      echo "To stop $SERVICE, kill PID $PID and remove $PIDFILE" >&2
      return 1
   fi

   [ -f "$PIDFILE" ] && rm -f "$PIDFILE"
   echo "OK"
}

status() {
   if app_is_running; then
      echo "$SERVICE is running (PID $PID)"
      exit 0
   else
      echo "$SERVICE is not running"
      exit 1
   fi
}
 
case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'restart')
        stop
        if [ $? -eq 0 ]; then
            start
        fi
        ;;
    'status')
        status
        ;;
    *)
        echo "Usage: $0 start|stop|restart|status"
        exit 1
        ;;
esac
 
exit 0
