#!/bin/sh
#
# Startup script for the Casa Authtoken Validate Daemon (casa_atvd)
#
# /etc/init.d/casa_atvd
#
# description: casa_atvd validates CASA
# authentication tokens on behalf of native (non-java)
# services.
#
# processname: casa_atvd
# pidfile: None
# config utility: None


### BEGIN INIT INFO
# Provides: casa_atvd
# Required-Start: $local_fs $remote_fs
# X-UnitedLinux-Should-Start: $syslog $time
# Required-Stop: $local_fs $remote_fs
# X-UnitedLinux-Should-Stop: $syslog $time
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Casa Authtoken Validate Daemon
# Description: Start Casa Authtoken Validate Daemon
### END INIT INFO

. /etc/rc.status

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

MyStatus()
{
  ps ax | grep "$DAEMON" | grep -v grep 2>&1 > /dev/null
  if [ "x$?" = "x0" ]; then
    RVAL=0
  else
    RVAL=3
  fi
}

START_DAEMON_CMD=start_daemon
START_DAEMON_CMD_FLAG=-u
STATUS=MyStatus
LOG_SUCCESS=log_success_msg
LOG_FAILURE=log_failure_msg
LOG_WARNING=log_warning_msg
ECHO=

DAEMON=/usr/bin/casa_atvd
DAEMON_USER=casaatvd

StartDAEMON()
{
  # Source the environments file for our daemon
  . /etc/CASA/authtoken/validate/envvars
  
  # Update the limit parameters
  #
  # Do not allow for unlimited core dumps if the daemon is automatically
  # re-starting crashed processes.
  if [ $DAEMON_NO_AUTORESTART_AFTER_CRASH ]; then
    if [ $DAEMON_NO_AUTORESTART_AFTER_CRASH -ne 0 ]; then
      # Feature disabled, allow core dumping.
      ulimit -c unlimited
    else
      # Check if core dumping is allowed with the feature enabled
      if [ $DAEMON_COREDUMPS_WANTED ]; then
        ulimit -c unlimited
      fi
    fi
  else    
    # Check if core dumping is allowed with the feature enabled
    if [ $DAEMON_COREDUMPS_WANTED ]; then
      ulimit -c unlimited
    fi
  fi
  
  ulimit -f unlimited

  # Make sure that the client keystore has been setup
  /usr/share/java/CASA/authtoken/bin/client_keystore_setup.sh -s
  
  # Start the daemon
  echo -n "Starting casa_atvd..."
  if [ "${JVM_VER}" = "SUN" ]; then
    # We need to specify the single-threaded option :-( due to Sun's
    # JVM bug (Bug 221420). This will be changed once the issue is
    # resolved.
    echo "Starting daemon using single threaded mode :-("
    $START_DAEMON_CMD $START_DAEMON_CMD_FLAG $DAEMON_USER $DAEMON -d -s
  else
    $START_DAEMON_CMD $START_DAEMON_CMD_FLAG $DAEMON_USER $DAEMON -d
  fi
    
  RVAL=$?
  $ECHO
}


StopDAEMON()
{
  echo -n "Stopping casa_atvd..."
  killproc $DAEMON
  RVAL=$?
  $ECHO
}

case "$1" in
start)
  StartDAEMON
  ;;
stop)
  StopDAEMON
  ;;
restart|reload|force-reload)
  StopDAEMON
  sleep 1
  StartDAEMON
  ;;
status)
  $STATUS
  ;;
*)
  echo -n "Usage: $0 <start|stop|restart|reload|force-reload>" > /dev/stderr
  RVAL=1
  ;;
esac

rc_failed $RVAL
rc_status -v
rc_exit