#!/bin/bash
#
# vcagent script.
#
# description: HP Version Control Agent.
# pidfile: /var/run/vcagent.pid
# chkconfig: 345 99 1
#   345 - levels 3, 4 and 5
#   99 - start after the snmpd (level 50) and hpasm (90)
#   1 - stop before hpasm

[ -x /sbin/pidof ] && PIDOF=/sbin/pidof || PIDOF=pidof

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
  if [ "$HPVCAINSTALLCOMPLETED" = "yes" ]; then
    [ "$HPVCAINSTALLSELFONLY" = "yes" ] && return 1 || return 0
  fi
  [ "$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 [ "$?" = 0 ]; then
      RETRY=10
      # make sure there's no competing HTTP Server
      WBEMSS1=`(ls -ldurt --full-time /var/spool/compaq/wbem/*; ls -ldrt --full-time /var/spool/compaq/wbem/*)  2>/dev/null`
      while true; do
        sleep 2
        WBEMSS2=`(ls -ldurt --full-time /var/spool/compaq/wbem/*; ls -ldrt --full-time /var/spool/compaq/wbem/*)  2>/dev/null`
        [ "$WBEMSS2" = "$WBEMSS1" ] && break
        RETRY=`expr $RETRY - 1`
        [ $RETRY -lt 1 ] && break
        WBEMSS1="$WBEMSS2"
      done
    else
      exit 0
    fi
fi

# description: HP Version Control Agent

# Source function library.
. /etc/rc.d/init.d/functions

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

mykillproc() {
        # Kill with -TERM signal first
	pidof1=`$PIDOF $vcagent`
	pidof2=""
        while [ ! -z "$pidof1" -a ! "$pidof2" = "$pidof1" ]; do
          pidof2=$pidof1
	  kill -TERM `$PIDOF $vcagent` > /dev/null 2>&1
	  usleep 500000 # sleeps for 0.5 secs
	  pidof1=`$PIDOF $vcagent`
        done

        # Kill with -KILL signal
	pidof1=`$PIDOF $vcagent`
	pidof2=""
        while [ ! -z "$pidof1" -a ! "$pidof2" = "$pidof1" ]; do
          pidof2=$pidof1
	  kill -KILL `$PIDOF $vcagent` > /dev/null 2>&1
	  usleep 500000 # sleeps for 0.5 secs
	  pidof1=`$PIDOF $vcagent`
        done

        # All should be killed
	pid=`$PIDOF $vcagent`
	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 $vcagent`
	if [ ! -z "$pid" ]; then
		echo_failure
		echo
		return 1
	fi
	cd $vcapath
	export LD_LIBRARY_PATH=$vcapath/lib
	export LD_ASSUME_KERNEL="2.4.1"
	find /var/spool/compaq/wbem/cache -type d -name vcagent -exec rm -rf {} \; 2>/dev/null
	daemon $vcagent
	RETVAL=$?
	cd -
	echo
	[ $RETVAL = 0 ] && touch /var/lock/subsys/vcagent
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`$PIDOF $vcagent`
	if [ -z "$pid" ]; then
		echo_failure
		echo 
		return 1
	fi
	#killproc $vcagent -15
	mykillproc
	RETVAL=$?
	if [ "$RETVAL" = "0" ]; then
	    echo_success
	else
	    echo_failure
	fi
	echo
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/vcagent /var/run/vcagent.pid
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $vcagent
		RETVAL=$?
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

exit $RETVAL
