#!/bin/sh
#
#  Copyright 2005 VMware, Inc.  All rights reserved.
#
# vmware-vpxa		Start/Stop the VMware VirtualCenter Agent 
#
#

platform=`/bin/uname -s`

if [ X$platform = XVMkernel ]  # VMVisor
then
   SCHED_PARAM="++min=0,swap,group=vpxa"
else
   SCHED_PARAM=
fi

# Basic support for IRIX style chkconfig 
# chkconfig: 345 99 1
# description: starts up VirtualCenter management agent.

# load system helper functions.
if [ -f /etc/init.d/functions ]
then
  . /etc/init.d/functions
else
  # non-rh systems don't have functions.
  echo_failure() { echo "failed"; }
  echo_success() { echo "success"; }
fi

watchdog=/opt/vmware/vpxa/bin/vmware-watchdog
pidfile=/var/run/vmware/vmware-vpxa.PID
cd /opt/vmware/vpxa/bin

get_vpxa_pid() {
  if type pidof >/dev/null 2>&1
  then
     pidof vpxa
  elif [ X$platform = XVMkernel ]  # VMVisor
  then 
     ps | grep vpxa | head -n 1 | awk '{print $1}'
  fi
}


start_vpxa() {
  if $watchdog -r vpxa > /dev/null 2>&1 ; then
    echo -n " (vmware-vpxa is already running)"
    return 1
  else
    setsid $watchdog -s vpxa -u 30 -q 5 "/opt/vmware/vpxa/sbin/vpxa $SCHED_PARAM" > /dev/null 2>&1 &  
  fi

  # Wait for a second to give things a chance to start off.
  MAX_ATTEMPTS=10
  local attempts=0
  local vpxapid=`get_vpxa_pid`
  while [ "$vpxapid" = '' ]
  do
     if [ $attempts -gt $MAX_ATTEMPTS ] ; then
        return 1
     fi
     sleep 1
     vpxapid=`get_vpxa_pid`
     attempts=$((attempts+1))
  done

  if [ "$vpxapid" != '' ]; then
    return 0
  else 
    return 1
  fi
}

stop_vpxa() {
  local vpxapid=`get_vpxa_pid`
  # Kill the watchdog.
  $watchdog -k vpxa > /dev/null 2>&1
  # Kill vpxa.
  kill -TERM $vpxapid > /dev/null 2>&1
  
  # Wait a second, to give things a chance to either die or
  # restart (if the watchdog is still up for some reason).
  sleep 1
  
  # Now we must make sure things died.
  local attempts=0
  MAX_ATTEMPTS=10
  while [ "$vpxapid" != '' ]
  do
     # If we've already tried a bunch of times,
     # just give up and print an error message.
     if [ $attempts -gt $MAX_ATTEMPTS ] ; then
        return 1
     fi
     
     # Retry to kill the watchdog, just for completeness. 
     # It may already be dead.
     $watchdog -k vpxa > /dev/null 2>&1
     
     # Send vpxa a KILL signal to show we're serious.
     kill -KILL $vpxapid > /dev/null 2>&1
     
     sleep 1
     vpxapid=`get_vpxa_pid`
     attempts=$((attempts+1))
  done

  # Remove the PID file
  rm -f $pidfile

  return 0
}

status_vpxa() {
  if $watchdog -r vpxa > /dev/null 2>&1 ; then
    echo "vmware-vpxa is running"
    return 0
  else
    echo "vmware-vpxa is stopped"
    return 1
  fi
}

VPXA_RC=0

case $1 in
  start)
   echo -n "Starting vmware-vpxa:"
   start_vpxa
   VPXA_RC=$? 
   if [ "$VPXA_RC" -gt 0 ]; then
      echo_failure
   else
      echo_success
   fi
   echo
   ;;
  
  stop)
   echo -n "Stopping vmware-vpxa:"
   stop_vpxa
   VPXA_RC=$?
   if [ "$VPXA_RC" -gt 0 ]; then
     echo_failure
   else
     echo_success
   fi
   echo
   ;;

  status)
   status_vpxa
   VPXA_RC=$?
   ;;

  restart)
   $0 stop
   $0 start
   VPXA_RC=$?
   ;;

  *)
   echo "Usage: `basename "$0"` {start|stop|status|restart}"
   exit 1
esac

exit $VPXA_RC

