#!/bin/sh
chatter() {
	if test "$1" = "verbose"; then
		echo $2
	fi
}

DetermineFilePath() {
        RC_DIR=/etc/rc.d
        if [ -d /etc/rc.d/init.d ]; then
                RC_DIR=/etc/rc.d/init.d
        fi
}

startup_helper() {
# Restart snmpd
	SNMPDSSCRIPT="$RC_DIR/snmpd"

	if [ ! -z "`pidof snmpd`" ]; then
	  if [ ! -z "$SNMPDSSCRIPT" ]; then
	    (cd /; $SNMPDSSCRIPT restart) >> $INSTALLLOG 2>&1
	    [ -z "`pidof snmpd`" ] && sleep 2
	    echo
	    if [ ! -z "`pidof snmpd`" ]; then
	      echo "$INSTMSG: snmpd is restarted"
	      echo "`date`: $INSTMSG: snmpd is restarted" >> $INSTALLLOG
	    else
	      echo "$INSTMSG: failed to restart snmpd"
	      echo "`date`: $INSTMSG: failed to restart snmpd" >> $INSTALLLOG
	    fi
	  fi
	fi

	WEBDSCRIPT="/opt/compaq/foundation/etc/cmawebd"

# Restart Web agent
	if [ ! -z "`pidof cmawebd`" ]; then
	  if [ ! -z "$WEBDSCRIPT" ]; then
	    $WEBDSCRIPT restart >> $INSTALLLOG 2>&1
	    [ -z "`pidof cmawebd`" ] && sleep 2
	    if [ ! -z "`pidof cmawebd`" ]; then
	      echo "`date`: $INSTMSG: hp Web agent (cmawebd) is restarted"
	      echo "`date`: $INSTMSG: hp Web agent (cmawebd) is restarted" >> $INSTALLLOG
	    else
	     echo "`date`: $INSTMSG: failed to restart cmawebd"
	      echo "`date`: $INSTMSG: failed to restart cmawebd" >> $INSTALLLOG
	    fi
	  fi
	fi
}


NAME="server"

INSTALLLOG=/var/spool/compaq/cma.log
INSTMSG="cmasvr installation"


if test "$1" != "init"; then
	exit -1	
fi

# Create /opt/compaq/cma.conf if it does not exist
CMACONF=/opt/compaq/cma.conf
if [ ! -f $CMACONF ]; then
  cat <<EOF >$CMACONF
#trapemail command. Multiple trapemail lines are allowed
trapemail /bin/mail -s 'hp Management Agents Trap Alarm' root
EOF
  chmod 700 $CMACONF
fi

#Add cmasvrobjects.conf to cmaobjects.conf
CONFFILE=/opt/compaq/cmaobjects.conf
OBJSFILE="/opt/compaq/server/etc/cmasvrobjects.conf"
if [ ! -f $CONFFILE ]; then
  echo $OBJSFILE  >>$CONFFILE
else
  #check if the entry is already there
  grep $OBJSFILE $CONFFILE >/dev/null
  if [ "$?" != 0 ]; then
    echo $OBJSFILE >>$CONFFILE
  fi
fi

/opt/compaq/server/setuphelper
startup_helper
