#!/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
  HPVCAINSTALLCOMPLETED=`echo "$HPVCAINSTALLCOMPLETED" | tr [:upper:] [:lower:]`
  if [ "$HPVCAINSTALLCOMPLETED" = "yes" ]; then
    return 1
  fi
  HPVCAINSTALLBYVCA=`echo "$HPVCAINSTALLBYVCA" | tr [:upper:] [:lower:]`
  [ "$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

# 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
	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
		echo_failure
		echo
		return 1
	fi
	cd $vcapath
	find /var/spool/compaq/wbem/cache -type d -name vcagent -exec rm -rf {} \; 2>/dev/null
	daemon $vcagent
	RETVAL=$?
	cd - >/dev/null 2>&1
	echo
	[ $RETVAL = 0 ] && touch /var/lock/subsys/hpvca
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`$PIDOF vcagentd`
	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/hpvca /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
