#!/bin/sh

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

PKGINST=118221

METHOD=patchadd
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

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

      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 reregister."
         $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. Reregistration 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'. Reregistration 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

      echo ""
      echo "$PKGINST: Reregistering $STORADE with the $WEBCONS."
      $SMCBASEDIR/usr/sadm/bin/smreg add -a /usr/share/webconsole/storade
      status=$?
      if [ $status -ne 0 ] ; then
        echo $HIGHLIGHT
         echo "$PKGINST Warning: Reregistration with the $WEBCONS failed."
         echo "'$SMCBASEDIR/usr/sadm/bin/smreg add -a /usr/share/webconsole/storade' exited"
         echo "with '$status' status. Please correct this problem and rerun this"
         echo "command manually once the $PKGINST $METHOD completes."
         echo $HIGHLIGHT
      fi

      test -f $SKIP_WEB_START && return

      if [ "$state" = yes ] ; then
         echo ""
         echo "$PKGINST: Starting the $WEBCONS after reregistration."
        /usr/bin/env LC_ALL=C $SMCBASEDIR/usr/sadm/bin/smcwebserver start
         status=$?
         if [ $status -ne 0 ] ; then
            echo $HIGHLIGHT
            echo "$PKGINST Warning: '/usr/bin/env LC_ALL=C $SMCBASEDIR/usr/sadm/bin/smcwebserver start'"
            echo "exited with '$status' status. Please correct this problem and rerun"
            echo "this command manually once the $PKGINST $METHOD completes. You will"
            echo "not 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"
         echo "start so it will not be automatically started. Run the command"
         echo "'/usr/bin/env LC_ALL=C $SMCBASEDIR/usr/sadm/bin/smcwebserver start' manually"
         echo "to start it once the $PKGINST $METHOD is complete. You will not be"
         echo "able to access the $WEBCONS until this has been done."
         echo $HIGHLIGHT
      fi
   fi
}

if [ $PKGINST -ge 118226 -a $PKGINST -le 118230 ] ; then
   test -f $WATCHDOG_NO_RESTART && WATCHDOG_FILE=exists
   test -d `/usr/bin/dirname $WATCHDOG_NO_RESTART` && /usr/bin/touch $WATCHDOG_NO_RESTART
   reconfigWebConsole
   test "$WATCHDOG_FILE" = exists || /usr/bin/rm -f $WATCHDOG_NO_RESTART
else
   reconfigWebConsole
fi

echo "-------postpatch completed--------"
exit $STATUS
