#! /bin/sh
### BEGIN INIT INFO
# Provides:	  hpvca
# Required-Start: network hpasm
# Required-Stop:
# Default-Start:   3 4 5
# Default-Stop:    0 1 2 6
# Description:     HP Version Control Agent
### END INIT INFO
#

ancestorpids() {
  TMPANCESTORPIDS=""
  CPID=$1
  while [ ! -z "$CPID" -a "$CPID" != "1" ]; do
    TMP=`ps -ef | awk '{if ($2=='$CPID') print $0;}'`
    [ -z "$TMP" ] && break
    CPID=`echo $TMP | awk '{print $3;}'`
    TMPANCESTORPIDS="$TMPANCESTORPIDS $CPID"
  done
  echo $TMPANCESTORPIDS
}

fuserpids () {
  echo `fuser $1 | sed 's/^[^ ]*: *//' | sed 's/[a-zA-Z]//g'` 2>/dev/null
}

pidsbyname () {
  echo `pidof $1`
}

intheset () {
  if [ "$1" != "" -a "$2" != "" ]; then
   for i in $1; do
    for j in $2; do
      if [ "$i" = "$j" ]; then
        return 1
      fi
    done
   done
  fi
  return 0
}

invokedbyvcagentd() {
  # must get VCAPIDS
  VCAPIDS=`pidsbyname /opt/hp/vcagent/bin/vcagentd`
  # Restart after VCA completed installation
  HPVCAINSTALLCOMPLETED=`echo "$HPVCAINSTALLCOMPLETED" | tr [A-Z] [a-z]`
  if [ "$HPVCAINSTALLCOMPLETED" = "yes" ]; then
    return 1
  fi
  HPVCAINSTALLBYVCA=`echo "$HPVCAINSTALLBYVCA" | tr [A-Z] [a-z]`
  [ "$HPVCAINSTALLBYVCA" = "yes" ] && exit 0
  ANCESTORPIDS=`ancestorpids $$`
  return `intheset "$ANCESTORPIDS" "$VCAPIDS"`
}

invokedbyvcagentd
if [ "$?" = "1" ]; then
    FUSERPIDS=`fuserpids /opt/hp/vcagent/bin/vcagentd`
    intheset "$FUSERPIDS" "$VCAPIDS"
    if [ "$?" = 1 ]; then
      exit 0
    fi
fi

# install cgi
if [ ! -d /opt/hp/hpsmh/data/cgi-bin/vcagent ]; then
  mkdir -p /opt/hp/hpsmh/data/cgi-bin/vcagent
fi
chown -R hpsmh:hpsmh /opt/hp/hpsmh/data/cgi-bin/vcagent
chmod -R go-rwx      /opt/hp/hpsmh/data/cgi-bin/vcagent
rm -f /opt/hp/hpsmh/data/cgi-bin/vcagent/cgi
cp -f /opt/hp/vcagent/bin/cgi /opt/hp/hpsmh/data/cgi-bin/vcagent/cgi
chown root:hpsmh     /opt/hp/hpsmh/data/cgi-bin/vcagent/cgi
chmod 4550           /opt/hp/hpsmh/data/cgi-bin/vcagent/cgi

# Source function library.
. /etc/rc.status

# Path to the server binary, and short-form for messages.
vcapath=/opt/hp/vcagent
vcagent=$vcapath/bin/vcagentd
prog='HP Version Control Agent'

RETVAL=0

test -x $vcagent || exit 1

mykillproc() {
  # Kill with -TERM signal first
	Pid=`pidof -s vcagentd`
	if [ "x$Pid" != "x" ] ; then
	  kill -TERM $Pid
	fi

  usleep 500000

  # Clean up any lingering processes with -KILL signal
	pidof1=`pidof vcagentd`
	pidof2=""
        while [ ! -z "$pidof1" -a ! "$pidof2" = "$pidof1" ]; do
          pidof2=$pidof1
	  kill -KILL `pidof vcagentd` > /dev/null 2>&1
	  usleep 500000 # sleeps for 0.5 secs
	  pidof1=`pidof vcagentd`
        done

        # All should be killed
	pid=`pidof vcagentd`
	if [ ! -z "$pid" ]; then
          return 1;
	fi
	find /var/spool/compaq/wbem/cache -type d -name vcagent -exec rm -rf {} \; 2>/dev/null
	return 0;
}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
	echo -n $"Starting $prog: "
	pid=`pidof vcagentd`
	if [ ! -z "$pid" ]; then
        rc_failed 1
        rc_status -v
        return 1
	fi
	cd $vcapath
	find /var/spool/compaq/wbem/cache -type d -name vcagent -exec rm -rf {} \; 2>/dev/null
	startproc $vcagent
    rc_status -v
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`pidof vcagentd`
	if [ -z "$pid" ]; then
        rc_failed 1
        rc_status -v
        return 1
	fi
	#killproc $vcagent -15
	mykillproc
    rc_status -v
}

# See how we were called.
rc_reset
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
	        echo -n 'Checking for process vcagent: '
		checkproc $vcagent
		rc_status -v
		;;
	restart)
		stop
                rc_reset
		start
		rc_status
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

rc_exit
