#!/bin/sh
#
# Copyright 2006 VMware, Inc.  All rights reserved.
#

# Basic support for IRIX style chkconfig 
# chkconfig: 3 97 09
# description: Initializes VMware VMkernel authorization daemon

# load system helper functions.
. /etc/init.d/functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Note vmware-vmkauthd-start is a shell script that execs the real vmware-vmkauthd
STARTVMKAUTHD=/usr/lib/vmware/bin/vmware-vmkauthd-start

LNAME="VMware VMkernel authorization daemon"


# Return pid if running or return empty string if not running
function VmkAuthdPid () {
    # vmware-vmkauthd is launched by vmkload_app.  See vmware-vmkauthd-start
    pgrep -f "^/usr/lib/vmware/(bin|bin-debug)/vmkload_app .* /usr/lib/vmware/(bin|bin-debug)/vmware-vmkauthd"
}


# vmkernel loaded?
vmkernel_loaded() {
   [ -e /proc/vmware/version ] 
}

function start () {
   if ! vmkernel_loaded ; then
      exit 0
   fi
    rpid=$(VmkAuthdPid)
    if [ -n "$rpid" ]; then
	# Already running.  Bail
	action "${LNAME} already running" /bin/true
    else 
	action "Starting ${LNAME}" $STARTVMKAUTHD --daemon
    fi
}


function stop () {
    rpid=$(VmkAuthdPid)
    if [ -n "$rpid" ]; then
	action "Stopping ${LNAME}" kill -TERM $rpid
    else
	action "${LNAME} already not running" /bin/true
    fi
}


function status () {
    rpid=$(VmkAuthdPid)
    if [ -n "$rpid" ]; then
	echo "${LNAME} is running (pid $rpid)."
    else 
	echo "${LNAME} is not running."
    fi
}


# See how we were called.
case "$1" in
   start)
	start
        ;;
   stop)
	stop
        ;;
   status)
	status
        ;;
   restart)
        stop 
	start
        ;;
   *)
        echo "Usage: $(basename "$0") {start|stop|status|restart}"
        exit 1
esac

exit 0
