#! /bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start: network hpasm
# Required-Stop:
# Default-Start:   3 5
# Default-Stop:
# 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
  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

# 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
	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
        rc_failed 1
        rc_status -v
        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
	startproc $vcagent
    rc_status -v
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`pidof $vcagent`
	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
