#!/bin/sh
#
# storm-webdav-server init script
#
# chkconfig:   - 20 80
# description: The StoRM webdav server
#
#
#### BEGIN INIT INFO
# Provides:          storm-webdav
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: The StoRM WebDAV service
# Description:       The StoRM WebDAV service
### END INIT INFO
#
# set -x

if [ ! -e /lib/lsb/init-functions ]; then
  echo "This init script requires lsb init functions!"
  exit 1
else
  source "/lib/lsb/init-functions"
fi

prog="storm-webdav"

config="/etc/sysconfig/${prog}"

lockfile="/var/lock/subsys/${prog}"

pidfile="/var/run/${prog}.pid"

# Delay (in seconds) for killproc
killproc_delay=5

# prepare_env():
#  sets up environment for storm-webdav-server execution
prepare_env() {
  export STORM_WEBDAV_JVM=${STORM_WEBDAV_JVM:-java}
  export STORM_WEBDAV_JAR=${STORM_WEBDAV_JAR:-/usr/share/java/storm-webdav/storm-webdav-server.jar}
  export STORM_WEBDAV_USER=${STORM_WEBDAV_USER:-storm}
  export STORM_WEBDAV_LOG=${STORM_WEBDAV_LOG:-/var/log/storm/webdav/storm-webdav-server.log}
  export STORM_WEBDAV_OUT=${STORM_WEBDAV_OUT:-/var/log/storm/webdav/storm-webdav-server.out}
  export STORM_WEBDAV_ERR=${STORM_WEBDAV_ERR:-/var/log/storm/webdav/storm-webdav-server.err}

  export STORM_WEBDAV_SUSPEND=${STORM_WEBDAV_SUSPEND:-n}

  if [ ! -z "${STORM_WEBDAV_DEBUG}" ]; then
    export STORM_WEBDAV_JVM_OPTS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=${STORM_WEBDAV_DEBUG_PORT},suspend=${STORM_WEBDAV_SUSPEND} ${STORM_WEBDAV_JVM_OPTS}"
  fi

  export STORM_WEBDAV_LOG_CONFIGURATION=${STORM_WEBDAV_LOG_CONFIGURATION:-/etc/storm/webdav/logback.xml}
  export STORM_WEBDAV_JVM_OPTS="-Dlogging.config=${STORM_WEBDAV_LOG_CONFIGURATION} ${STORM_WEBDAV_JVM_OPTS}"
}

if [ -r "${config}" ]; then
  # Auto-export variables
  set -a
  source "${config}"
fi

prepare_env

# _current_status():
#  tests if the storm-webdav-server process is running.
#  takes care of ensuring pid and lock files are in good shape
#
#  returns 0 if the process is running.
#  returns 1 if the process is running, and the function restored pid or lock files.
#  returns 2 if the process is NOT running.
_current_status(){
  local pid=$(pgrep -f storm-webdav-server.jar)
  local restored_pid_or_lock=0

  if [ -z "${pid}" ]; then
    rm -f ${pidfile}
    if [ -f "${lockfile}" ]; then
      rm -f ${lockfile}
    fi
    return 2
  fi

  ## We have a running process, ensure
  ## pid and lockfiles are there
  if [ ! -f "${pidfile}" ]; then
    restored_pid_or_lock=1
    touch ${pidfile} 2>&1
    echo ${pid} > ${pidfile}
  fi

  if [ ! -f "${lockfile}" ]; then
    restored_pid_or_lock=1
    touch ${lockfile} 2>&1
  fi

  return ${restored_pid_or_lock}
}


start() {
  local startcmd="nohup ${STORM_WEBDAV_JVM} ${STORM_WEBDAV_JVM_OPTS} -jar ${STORM_WEBDAV_JAR} > ${STORM_WEBDAV_OUT} 2>${STORM_WEBDAV_ERR} &"
  echo -n $"Starting $prog: "

  _current_status
  _status=$?

  if [ ${_status} -eq 0 ] || [ ${_status} -eq 1 ]; then
    log_success_msg "already running."
    return 0
  fi

  cd /etc/storm/webdav
  start_daemon -p ${pidfile} -u ${STORM_WEBDAV_USER} ${startcmd}
  retval=$?

  if [ ${retval} -eq 0 ]; then
    sleep 5
    _current_status
    _status=$?
    if [ ${_status} -eq 0 ] || [ ${_status} -eq 1 ]; then
      log_success_msg
    else
      log_failure_msg "failed to start daemon."
      retval=1
    fi
  fi

  return $retval
}

stop() {
  echo -n $"Stopping $prog: "
  _current_status
  _status=$?

  if [ ${_status} -eq 2 ]; then
    echo "not running."
    return 1
  fi

  killproc -p ${pidfile} -d ${killproc_delay} ${prog}
  retval=$?
  if [ ${retval} -eq 0 ]; then
    rm -f ${lockfile}
    log_success_msg
  else
    log_failure_msg "failed to kill process."
  fi

  return $retval
}

restart() {
  stop
  start
}

reload() {
  restart
}

force_reload() {
  restart
}

rh_status() {
  # run checks to determine if the service is running or use generic status
  _current_status
  _status=$?
  if [ ${_status} -eq 0 ] || [ ${_status} -eq 1 ]; then
    local pid=$(cat $pidfile)
    echo "${prog} (pid $pid) is running..."
  else
    echo "${prog} is stopped."
  fi

  return ${_status}
}

rh_status_q() {
  rh_status >/dev/null 2>&1
}

case "$1" in
  start)
    $1
    ;;
  stop)
    $1
    ;;
  restart)
    $1
    ;;
  reload)
    $1
    ;;
  force-reload)
    force_reload
    ;;
  status)
    rh_status
    ;;
  condrestart|try-restart)
    restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
    exit 2
esac
exit $?
