#!/bin/sh

test -f /tmp/_StorADE_DEBUG_ && set -x

PKGINST=117650
PACKAGE=SUNWstade
BASEDIR=`pkgparam $PACKAGE BASEDIR 2>/dev/null`
PKGDIR=`pkgparam $PACKAGE PKGDIR 2>/dev/null`

METHOD=patchrm
STORADE="Storage_A.D.E_2.4"
WEBCONS="Java Web Console"
HIGHLIGHT="------------------------------------------------------------------------"

removeCronEntry () {
   F=/var/spool/cron/crontabs/root
   echo ""
   echo "$PKGINST: Removing the cron entry."
   if [ -f $F ] ; then
      T=/tmp/ras_crons
      ERR=`/bin/cp $F $T 2>&1`
      if [ $? -eq 0 ] ; then
         /bin/rm -f $T
         cat $F | grep -v "Storage Automated Diagnostic Environment " | \
                  grep -v "\/bin\/rasagent " | \
                  grep -v "\/dhbin\/ras_datahost " | \
                  grep -v "\/SUNWstade\/sysbin\/patch_scheduler " | \
                  grep -v "\/bin\/rasinspect " > $T
         ERR="`/usr/bin/crontab $T 2>&1`"
         if [ $? -eq 0 ] ; then
            echo " ->  Agent cron removed."
         else
            echo "$HIGHLIGHT"
            echo "$PKGINST ERROR: The '/usr/bin/crontab $T' command used to"
            echo "update the cron failed with the following error message. Please correct"
            echo "this manually and re-run the $METHOD."
            echo ""
            echo "$ERR"
            exit 1
            echo "$HIGHLIGHT"
         fi
      else
         echo "$HIGHLIGHT"
         echo "$PKGINST ERROR: The following error occurred when copying"
         echo "$F to $T to update the"
         echo "cron file. Please correct this manually and re-run the $METHOD."
         echo ""
         echo "$ERR"
         exit 1
         echo "$HIGHLIGHT"
      fi
   else
      echo "$PKGINST NOTICE: The $F cron file does not exist."
   fi
}

stopRasAgent () {
   PROCLINE="`/usr/bin/ps -ef | grep -v grep | grep $BASEDIR/$PKGDIR/bin/rasagent`"
   if [ ! -z "$PROCLINE" ]; then
      echo ""
      echo "$PKGINST: Stopping the rasagent daemon."
      PROC_PID=`echo $PROCLINE | awk '{ print $2 }'`
      kill -9 $PROC_PID
   fi
}

if [ "$PKGINST" = 117651 ] ; then
   removeCronEntry
   stopRasAgent

elif [ "$PKGINST" = 117650 ] ; then
   removeCronEntry
   stopRasAgent

fi

exit
