#!/bin/ksh
#
# OSSTech Apache Tomcat 6
#
# chkconfig: - 80 20
# chkconfig.solaris: 3 50 16
# description: Apache Tomcat 6

cd /
. /opt/osstech/etc/sv/svfunc || exit 1

sv_title="Apache Tomcat 6"
sv_initname=`echo "${0##*/}" |sed 's/^[._]*[SK]*[0-9]*//'`
sv_id="${sv_initname#osstech-}"
sv_user="tomcat"
sv_config_file="/opt/osstech/etc/tomcat6/$sv_id.conf"
sv_bin_dir="/opt/osstech/share/tomcat6/bin"
sv_pid_file="/opt/osstech/var/run/tomcat6/$sv_id.pid"
sv_shutdown_wait="30"

runuser_cmd="/sbin/runuser"

. "$sv_config_file" || exit 1
eval `sed -n 's/^\([A-Z][A-Z0-9_]*\)=.*/export \1/p' "$sv_config_file"`

if [ -n "$TOMCAT_USER" ]; then
  sv_user="$TOMCAT_USER"
fi
if [ -n "$SHUTDOWN_WAIT" ]; then
  sv_shutdown_wait="$SHUTDOWN_WAIT"
fi
if [ -n "$CATALINA_PID" ]; then
  sv_pid_file="$CATALINA_PID"
else
  export CATALINA_PID="$sv_pid_file"
fi

function sv_start {
  echo_n "Starting $sv_title: $sv_initname: "
  if sv_status >/dev/null 2>&1; then
    failure; echo
    return 1
  fi
  echo

  "$runuser_cmd" -m -s "$sv_bin_dir/startup.sh" -- "$sv_user"
  typeset status="$?"

  if [ "$status" != 0 ]; then
    failure; echo
    return 1
  fi

  /bin/sleep 1
  if sv_status >/dev/null; then
    : OK
  else
    failure; echo
    return 1
  fi

  success; echo
  [ -d /var/lock/subsys ] && touch "/var/lock/subsys/$sv_initname"
  return 0
}

function sv_jpda {
  echo_n "Starting $sv_title: $sv_initname: jpda mode: "
  if sv_status >/dev/null 2>&1; then
    failure; echo
    return 1
  fi
  echo

  "$runuser_cmd" -m -s "$sv_bin_dir/startup-jpda.sh" -- "$sv_user"
  typeset status="$?"

  if [ "$status" != 0 ]; then
    failure; echo
    return 1
  fi

  /bin/sleep 1
  if sv_status >/dev/null; then
    : OK
  else
    failure; echo
    return 1
  fi

  success; echo
  [ -d /var/lock/subsys ] && touch "/var/lock/subsys/$sv_initname"
  return 0
}

function sv_stop {
  echo_n "Stopping $sv_title: $sv_initname: "
  if sv_status >/dev/null 2>&1; then
    : OK
  else
    failure; echo
    return 1
  fi
  echo

  "$runuser_cmd" -m -s "$sv_bin_dir/shutdown.sh" -- "$sv_user"
  typeset status="$?"
  if [ "$status" != 0 ]; then
    failure; echo
    return 1
  fi

  typeset wait="0"
  while sv_status >/dev/null; do
    if [ "$wait" -ge "$sv_shutdown_wait" ]; then
      failure; echo
      return 1
    fi
    sleep 1
    let 'wait = wait + 1'
  done

  success; echo
  [ -d /var/lock/subsys ] && rm -f "/var/lock/subsys/$sv_initname"
  return 0
}

function sv_status {
  typeset pid=`cat "$sv_pid_file" 2>/dev/null`
  if [ -n "$pid" ] && pgrep -f -u "$sv_user" /bin/java |grep "^$pid$" >/dev/null; then
    echo "$sv_initname (pid $pid) is running..."
    return 0
  else
    echo "$sv_initname is stopped"
    return 3
  fi
}

function sv_init {
  case "$1" in
  start)
    sv_start
    return $?
    ;;
  stop)
    sv_stop
    return $?
    ;;
  status)
    sv_status
    return $?
    ;;
  restart)
    sv_stop
    sv_start
    return $?
    ;;
  condrestart)
    sv_status >/dev/null 2>&1 || return 0
    sv_stop
    sv_start
    return $?
    ;;
  jpda)
    sv_jpda
    return $?
    ;;
  *)
    echo "Usage: $sv_argv0 {start|stop|status|restart|condrestart|jpda}"
    return 1
    ;;
  esac
}

sv_init ${1+"$@"}

