#!/bin/sh
#
# SyMON-Tivoli integration postinstall script
#
# installs the SyMON-Tivoli integration adapter
#
# Copyright 1998 by Sun Microsystems, Inc.  All rights reserved.
#

TIVOLI_PATH=`/usr/bin/pkgparam SUNWestia TIVOLI_PATH`
BINDIR=`/usr/bin/pkgparam SUNWestia BINDIR`
DBDIR=`/usr/bin/pkgparam SUNWestia DBDIR`
LIBDIR=`/usr/bin/pkgparam SUNWestia LIBDIR`
LD_LIBRARY_PATH=`/usr/bin/pkgparam SUNWestia LD_LIBRARY_PATH`
DISPLAY=`/usr/bin/pkgparam SUNWestia DISPLAY`

PATH=${TIVOLI_PATH}
export BINDIR
export DBDIR
export LIBDIR
export LD_LIBRARY_PATH
export PATH
export DISPLAY

TivoliPort=`/usr/bin/pkgparam SUNWestia TivoliPort`
Installdir=`/usr/bin/pkgparam SUNWestia Installdir`
Polregion=`/usr/bin/pkgparam SUNWestia Polregion`
StartAdapter=`/usr/bin/pkgparam SUNWestia StartAdapter`
StartApp=`/usr/bin/pkgparam SUNWestia StartApp`
Restart=`/usr/bin/pkgparam SUNWestia Restart`


echo "Using $Installdir for adapater directory"
echo " "

# add task/task lib for launching SyMON

echo "Installing the SunMC task library..."
echo " "
echo "Compiling and importing the task library into $Polregion, one moment please..."

${BINDIR}/bin/wtll -p $Polregion -r $Installdir/bin/SyMONTaskLib

if [ $? != 0 ]
then
  echo " "
  echo "ERROR: There was a problem compiling the task library."
  echo "Please note the above Tivoli error messages, fix the"
  echo "\terror condition and retry the install."
  echo " "
  echo "Quitting adapter install"
  echo " "
  exit 1
else
  echo "Done with task library."
fi
echo " "

# SyMON_rb is just a copy of the Default rule base.
# import tecad_symon.baroc into the rule base, then
# need to recompile and load new rule base and restart 
# the TEC server

rbdir=$DBDIR/tec/SyMON_rb
baroc=$Installdir/etc/tecad_symon.baroc
rule=$Installdir/etc/tecad_symon_default.rls
rulebase=SyMON_rb
defaultrb=Default

echo "Creating SunMC Rule Base..."

# first, delete any currently-configured SyMON Rule
# Base from the Tivoli Event Server

${BINDIR}/bin/wdelrb $rulebase

# if the Rule Base directory exists, move it to backup
bakfile=$rbdir.symonbak
if [ -d $rbdir ]
then
  if cp -r $rbdir $bakfile
  then
    if rm -rf $rbdir
    then
      echo "Moved $rbdir to $bakfile"
    else
      echo "ERROR: Couldn't remove old $rbdir"
      echo " "
      echo "Quitting adapter install"
      echo " "
      exit 1
    fi
  else
    echo "ERROR: Couldn't move $rbdir to backup"
    echo " "
    echo "Quitting adapter install"
    echo " "
    exit 1
  fi
fi

# now make a new, clean Rule Base directory
if mkdir -m 755 $rbdir
then
  echo "Created $rbdir"
else
  echo "ERROR: Couldn't create $rbdir"
  echo " "
  echo "Quitting adapter install"
  echo " "
  exit 1
fi
echo " "

${BINDIR}/bin/wcrtrb -d $rbdir $rulebase
if [ $? != 0 ]
then
  echo " "
  echo "ERROR: There was a problem creating the rule base."
  echo "Please note the above Tivoli error messages, fix the"
  echo "\terror condition and retry the install."
  echo " "
  echo "Quitting adapter install"
  echo " "
  exit 1
else
  echo "Importing SunMC .baroc file..."
  ${BINDIR}/bin/wimprbclass $baroc $rulebase
  if [ $? != 0 ]
  then
    echo " "
    echo "ERROR: There was a problem importing class file."
    echo "Please note the above Tivoli error messages, fix the error"
    echo "\tcondition and retry the install."
    echo " "
    echo "Quitting adapter install"
    echo " "
    exit 1
   else 
     echo "Importing SunMC rule into rule base $rulebase"
     ${BINDIR}/bin/wimprbrules $rule $rulebase
     if [ $? != 0 ]
     then 
       echo " "
       echo "ERROR: There was a problem importing the rule"
       echo "Please note the above Tivoli error messages, fix the error"
       echo "\tcondition and retry the install."
       echo " "
       echo "Quitting adapter install"
       echo " "
       exit 1
     else
      echo "Compiling and loading the SunMC rule base..."
      ${BINDIR}/bin/wcomprules $rulebase
      if [ $? != 0 ]
      then
	echo " "
	echo "ERROR: There was a problem compare the rule base."
	echo "Please note the above Tivoli error messages, fix the"
	echo "\terror condition and retry the install."
	echo " "
	echo "Quitting adapter install."
	echo " "
	exit 1
      else
	# load the rule base
	${BINDIR}/bin/wloadrb $rulebase
	if [ $? != 0 ]
	then
	  echo " "
	  echo "ERROR: There was a problem loading the rule base."
	  echo "Please note the above Tivoli error messages, fix the"
	  echo "\terror condition and retry the install."
	  echo " "
	  echo "Quitting adapter install."
	  echo " "
	  exit 1
	else
	  echo " "
	  echo "Done with rule base."
	  echo " "

	  while :
	  do
	    case $Restart in
	      yes) echo "Restarting Tivoli Event Server..."
		   ${BINDIR}/bin/wstopesvr
		   if [ $? != 0 ]
		   then
		     echo " "
		     echo "Couldn't stop the Event Server - Please do it manually."
		   else
		     ${BINDIR}/bin/wstartesvr
		     if [ $? != 0 ]
		     then
		       echo " "
		       echo "Couldn't start the Event Server - Please do it manually."
		       echo "You will also have to start both the adapter and SyMONInt"
		       echo "\tapp manually, as described in the documentation."
		       echo " "
		     else
		       echo "Restarted Event Server"
		       echo " "
		       # try to start the adapter
		       while :
		       do
			 case $StartAdapter in
			   yes) echo "Starting adapter..."
				$Installdir/bin/tecad_symon -l $Installdir/etc/tecad_symon.conf -p $TivoliPort > /dev/null 2>&1 &
				if [ $? != 0 ]
				then
				  echo " "
				  echo "Couldn't start the adapter."
				  echo "Please start both the adapter and the SyMONINt app manually," 
				  echo "\tas described in the documentation."
				  echo " "
				else
				  echo "Done"
				  echo " "
				  while :
				  do
				    case $StartApp in
				      yes) echo "Starting the SyMONInt app..."
					   CLASSDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`/SUNWsymon/SunMC-Tivoli
					   $CLASSDIR/sbin/si-start.sh  > /dev/null 2>&1 &
					   if [ $? != 0 ]
					   then
					     echo " "
					     echo "Couldn't start the SyMONInt app."
					     echo "Please start it manually as described in the documentation."
					     echo " "
					   else
					     echo "Done"
					   fi
					   break ;;
				      no) echo "You have chosen to not have me start the SyMONInt app."
					  echo "So you must do it manually, as described in the documentation."
					  echo " "
					  break ;;
				    esac
				  done
				fi
				break ;;
			   no) echo "You have chosen to not have me start the adapter."
			       echo "So you must start both the adapter and the SyMONInt app manually,"
			       echo "\tas described in the documentation."
				 echo " "
				 break ;;
			 esac
		       done
		     fi
		   fi
		   break ;;
	      no) echo "You have chosen to not have me restart the Tivoli Event Server."
		  echo "So you must do it manually for the SunMC rule base to be"
		  echo "\trecognized by Tivoli."
		  echo " "
		  echo "You will also have to manually start both the adapter and the SyMONInt app manually,"
		  echo "\tas described in the documentation."
		  echo " "
		  break ;;
	    esac
	  done

	fi
      fi
    fi
  fi
fi

echo " "
exit 0
