#!/bin/sh
# ------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

APP_USER=""
SERVICE="process"
APP_CONSOLE_OUT="/dev/null"
APP_CONSOLE_ERR="/dev/null"

#
# Discover the APP_BASE from the location of this script.
#
if [ -z "$APP_BASE" ] ; then
  ## resolve links - $0 may be a link to apollo's home
  PRG="$0"
  saveddir=`pwd`

  # need this for relative symlinks
  dirname_prg=`dirname "$PRG"`
  cd "$dirname_prg"

  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
      PRG="$link"
    else
      PRG=`dirname "$PRG"`"/$link"
    fi
  done

  APP_BASE=`dirname "$PRG"`
  cd "$saveddir"

  # make it fully qualified
  APP_BASE=`cd "$APP_BASE/.." && pwd`
  export APP_BASE
fi
PID_FILE="${APP_BASE}/var/${SERVICE}.pid"

# Add the jars in the lib dir
CLASSPATH=""
for file in "$APP_BASE"/lib/*.jar
do
    if [ -z "$CLASSPATH" ]; then
        CLASSPATH="$file"
    else
        CLASSPATH="$CLASSPATH:$file"
    fi
done

JVM_EXEC="java"
JVM_ARGS=""
APP_ARGS=""
MAIN_JAR="$APP_BASE/lib/main.jar"
MAIN_CLASS=""

# Source the /etc/defaults script if it exists so it can modify the 
# env vars setup so far..
if [ -f "${APP_BASE}/etc/defaults" ] ; then
  . "${APP_BASE}/etc/defaults"
fi

if [ ! -z "$APP_USER" -a ! `id -un` = "$APP_USER" ] ; then
  # re-run the launch under the right user id...
  exec sudo -n -u ${APP_USER} $0 $@
fi

status() {
  if [ -f "${PID_FILE}" ] ; then
    pid=`cat "${PID_FILE}"`
    # check to see if it's gone...
    ps -p ${pid} > /dev/null
    if [ $? -eq 0 ] ; then
      return 1
    else
      rm "${PID_FILE}"
    fi
  fi
  return 0
}

stop() {
  if [ -f "${PID_FILE}" ] ; then
    pid=`cat "${PID_FILE}"`
    kill $@ ${pid} > /dev/null
  fi
  for i in 1 2 3 4 5 ; do
    status
    if [ $? -eq 0 ] ; then
      return 0
    fi
    sleep 1
  done
  echo "Could not stop process ${pid}"
  return 1
}

start() {

  status
  if [ $? -eq 1 ] ; then
    echo "Already running."
    return 1
  fi

  if [ -z "$MAIN_CLASS" ]; then
    RUN_COMMAND="${JVM_EXEC} ${JVM_ARGS} -classpath ${CLASSPATH} -jar ${MAIN_JAR} ${JVM_ARGS}"   
  else
    RUN_COMMAND="${JVM_EXEC} ${JVM_ARGS} -classpath ${CLASSPATH} ${MAIN_CLASS} ${JVM_ARGS}"   
  fi

  # If you have bash, then you can use the 'exec -a newname' syntax to rename the java process.
  if [ -x "/bin/bash" ] ; then
    echo exec -a ${SERVICE} $RUN_COMMAND | nohup /bin/bash -s > ${APP_CONSOLE_OUT} 2> ${APP_CONSOLE_ERR} &
  elif [ -x "/usr/bin/bash" ] ; then
    echo exec -a ${SERVICE} $RUN_COMMAND | nohup /usr/bin/bash -s > ${APP_CONSOLE_OUT} 2> ${APP_CONSOLE_ERR} &
  else 
    nohup $RUN_COMMAND > ${APP_CONSOLE_OUT} 2> ${APP_CONSOLE_ERR} &
  fi

  pid="$!"
  mkdir -p `dirname ${PID_FILE}` > /dev/null 2> /dev/null
  echo $pid > "${PID_FILE}"

  # check to see if stays up...
  sleep 1
  status
  if [ $? -eq 0 ] ; then
    echo "Could not start ${SERVICE}"
    return 1
  fi
  echo "${SERVICE} is now running (${pid})"
  return 0
}

case $1 in
  start)
    echo "Starting ${SERVICE}"
    start
    exit $?
  ;;

  force-stop)
    echo "Forcibly Stopping ${SERVICE}"
    stop -9
    exit $?
  ;;

  stop)
    echo "Gracefully Stopping ${SERVICE}"
    stop
    exit $?
  ;;

  restart)
    echo "Restarting ${SERVICE}"
    stop
    start
    exit $?
  ;;

  status)
    status
    if [ $? -eq 0 ] ; then
      echo "${SERVICE} is stopped"
    else
      echo "${SERVICE} is running (${pid})"
    fi
    exit 0
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-stop|status}" >&2
    exit 2
  ;;
esac
