#!/bin/sh
cat <<EOF # Workaround for when the early ras_install is run. This line is replaced by perl.
EOF
test -f /tmp/_StorADE_DEBUG_ && set -x

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

# Watchdog Auto Restart of the WEBCONS for the System Edition.
WATCHDOG_NO_RESTART=/opt/se6x20/watchdog_no_restart

# Skip Java Web Console start/restart when SKIP_WEB_START exists.
SKIP_WEB_START=/tmp/.StorADE_Skip_Web_Start

# Skip running ras_install or the Java Web Console configuration when SKIP_CONFIG exists. 
SKIP_CONFIG=/tmp/.StorADE_Skip_Config

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
}

stopSnmpTrapd () {
   PROCLINE="`/usr/bin/ps -ef | grep SUNWstade | grep snmptrapd | grep -v grep`"
   if [ ! -z "$PROCLINE" ]; then
      echo ""
      echo "$PKGINST: Stopping snmptrapd."
      PROC_PID=`echo $PROCLINE | awk '{ print $2 }'`
      kill -9 $PROC_PID
   fi
}
   
stopRasServ () {
   /usr/bin/ps -e | /usr/bin/grep " rasserv$" >/dev/null 2>&1
   if [ $? -eq 0 ] ; then
      echo ""
      echo "$PKGINST: Stopping the rasserv daemon."
      $BASEDIR/$PKGDIR/rasserv/bin/apachectl stop
   fi
}

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

removeLinkedFiles () {
   /usr/bin/rm -rf \
      $BASEDIR/SUNWstads \
      $BASEDIR/$PKGDIR/bin/perl
   /usr/bin/rm -f \
     /etc/rc2.d/S91rasserv \
     $BASEDIR/$PKGDIR/DATA \
     $BASEDIR/$PKGDIR/log \
     $BASEDIR/$PKGDIR/htdocs/htdocs \
     $BASEDIR/$PKGDIR/lib/auto/Crypt/SSLeay/SSLeay.so \
     $BASEDIR/$PKGDIR/lib/auto/GD/GD.so \
     $BASEDIR/$PKGDIR/lib/auto/XML/LibXML/LibXML.so \
     $BASEDIR/$PKGDIR/lib/auto/Crypt/MD5/MD5.so \
     $BASEDIR/$PKGDIR/lib/auto/GDBM_File/GDBM_File.so \
     $BASEDIR/$PKGDIR/lib/auto/Storable/Storable.so \
     $BASEDIR/$PKGDIR/lib/auto/Crypt/Blowfish/Blowfish.so \
     $BASEDIR/$PKGDIR/lib/auto/FCGI/FCGI.so \
     $BASEDIR/$PKGDIR/lib/auto/Compress/Zlib/Zlib.so \
     $BASEDIR/$PKGDIR/lib/auto/IO/IO.so \
     $BASEDIR/$PKGDIR/lib/auto/MIME/Base64/Base64.so
}

removeChangeableFiles () {
   CPFILES=$BASEDIR/$PKGDIR/cpfiles
   if [ -d $CPFILES ] ; then
      echo ""
      echo "$PKGINST: Removing $CPFILES files and empty directories."
      FILES=`find $CPFILES -type f | sed -e "s~$CPFILES~$BASEDIR/$PKGDIR~"`
      test -n "$FILES" && /usr/bin/rm -f $FILES
      DIRS=`find $CPFILES -type d | sed -e "s~$CPFILES~$BASEDIR/$PKGDIR~" | sort -r`
      test -n "$DIRS" && /usr/bin/rmdir -ps $DIRS >/dev/null 2>&1 || :
   fi
}

unconfigWebConsole () {
   test -f $SKIP_CONFIG && return 0
   SMCBASEDIR=`pkgparam SUNWmcon BASEDIR 2>/dev/null`
   if [ $? -eq 0 ] ; then
      test "$SMCBASEDIR" = "/" && SMCBASEDIR=""

      $SMCBASEDIR/usr/sadm/bin/smreg list -a $STORADE | grep $STORADE >/dev/null 2>&1
      if [ $? -eq 0 ] ; then
         echo ""
         echo "$PKGINST: Checking the status of the $WEBCONS."
         state=`$SMCBASEDIR/usr/sadm/bin/smcwebserver status -p | grep running | cut -d"=" -f2`
         if [ "$state" = yes ]; then
            echo ""
            echo "$PKGINST: Stopping the $WEBCONS to unregister."
            $SMCBASEDIR/usr/sadm/bin/smcwebserver stop 
            status=$?
            if [ $status -ne 0 ] ; then
               echo $HIGHLIGHT
               echo "$PKGINST Warning: '$SMCBASEDIR/usr/sadm/bin/smcwebserver stop' exited with"
               echo "'$status' status. Unregistration of $PKGINST will attempt to continue."
               echo $HIGHLIGHT
            fi
         elif [ "$state" = no ]; then
            continue
         else
            echo $HIGHLIGHT 
            echo "$PKGINST Warning: An unrecognized status of '$state' was returned from"
            echo "'$SMCBASEDIR/usr/sadm/bin/smcwebserver status -p'. Unregistration of $PKGINST"
            echo "will attempt to continue."
            echo $HIGHLIGHT
         fi

         echo ""
         echo "$PKGINST: Unregistering $STORADE from the $WEBCONS."
         $SMCBASEDIR/usr/sadm/bin/smreg remove -a $STORADE
         status=$?
         if [ $status -ne 0 ] ; then
            echo $HIGHLIGHT
            echo "$PKGINST Warning: Unregistration from the $WEBCONS failed."
            echo "'$SMCBASEDIR/usr/sadm/bin/smreg remove -a $STORADE' exited with '$status'"
            echo "status. Please correct this problem and rerun any of the following"
            echo "commands that fail manually once the $PKGINST $METHOD completes."
            echo $HIGHLIGHT
         fi

         test -f $SKIP_WEB_START && return

         if [ "$state" = yes ] ; then
            echo ""
            echo "$PKGINST: Restarting the $WEBCONS after unregistration."
            $SMCBASEDIR/usr/sadm/bin/smcwebserver start
            status=$?
            if [ $status -ne 0 ] ; then
               echo $HIGHLIGHT
               echo "$PKGINST Warning: '$SMCBASEDIR/usr/sadm/bin/smcwebserver start' exited with"
               echo "'$status' status. Please correct this problem and rerun this command"
               echo "manually once the $PKGINST $METHOD completes. You will not"
               echo "be able to access the $WEBCONS until this has been done."
               echo $HIGHLIGHT
            fi
         else
            echo $HIGHLIGHT
            echo "$PKGINST Warning: The $WEBCONS was not running at $METHOD start"
            echo "so it will not be automatically started. Run the command"
            echo "'$SMCBASEDIR/usr/sadm/bin/smcwebserver start' manually to start it once the"
            echo "$PKGINST $METHOD is complete. You will not be able to access the"
            echo "$WEBCONS until this has been done."
            echo $HIGHLIGHT
         fi
      fi
   fi
}

if [ "$PKGINST" = SUNWstads ] ; then
   test -d `/usr/bin/dirname $WATCHDOG_NO_RESTART` && /usr/bin/touch $WATCHDOG_NO_RESTART
   unconfigWebConsole
   test "$WATCHDOG_FILE" = exists || /usr/bin/rm -f $WATCHDOG_NO_RESTART
   removeCronEntry
   stopRasAgent
   stopSnmpTrapd
   stopRasServ
   stopJVM
   removeLinkedFiles
   removeChangeableFiles

elif [ "$PKGINST" = SUNWstadm ] ; then
   unconfigWebConsole

elif [ "$PKGINST" = SUNWstade ] ; then
   removeCronEntry
   stopRasAgent
   stopSnmpTrapd
   stopRasServ
   stopJVM
   removeLinkedFiles
   removeChangeableFiles

fi

exit
