#!/bin/sh

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

PKGINST=117654
PACKAGE=SUNWstadm
BASEDIR=`pkgparam $PACKAGE BASEDIR 2>/dev/null`
PKGDIR=`pkgparam $PACKAGE PKGDIR 2>/dev/null`

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

runRasInstall () {
   test -f $SKIP_CONFIG && return 0
   RAS_OPTIONS="$*"
   if [ "$BASEDIR" != "" -a "$PKGDIR" != "" ] ; then
      echo ""
      echo "$PKGINST: Running '$BASEDIR/$PKGDIR/bin/ras_install $RAS_OPTIONS'."
      $BASEDIR/$PKGDIR/bin/ras_install $RAS_OPTIONS
      status=$?
      if [ $status -ne 0 ] ; then
         echo $HIGHLIGHT
         echo "$PKGINST Warning: '$BASEDIR/$PKGDIR/bin/ras_install $RAS_OPTIONS' exited"
         echo "with '$status' status. Please correct this problem and rerun ras_install"
         echo "manually once the $PKGINST $METHOD is complete."
         echo $HIGHLIGHT
      fi
   else
      echo $HIGHLIGHT
      echo "$PKGINST Warning: Cannot determine the BASEDIR or PKGDIR for running"
      echo "the $PACKAGE version of ras_install. Please correct this problem"
      echo "and manually run ras_install once the $PKGINST $METHOD is complete.."
      echo $HIGHLIGHT
   fi
}

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

      echo ""
      echo "$PKGINST: Enabling the $WEBCONS console_debug_log file"
      echo "to automatically grow and rollover using the default settings."
      $SMCBASEDIR/usr/sadm/bin/smreg add -p -c debug.trace.level=1
      status=$?
      if [ $status -ne 0 ] ; then
         echo $HIGHLIGHT
         echo "$PKGINST Warning: '$SMCBASEDIR/usr/sadm/bin/smreg add -p -c debug.trace.level=1'"
         echo "exited 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."
        $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 be able"
            echo "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
}

if [ "$PKGINST" = 117651 ] ; then
   $BASEDIR/$PKGDIR/sysbin/fix_exe
   runRasInstall -m -S Y -c -R
   /usr/bin/rm -f $BASEDIR/$PKGDIR/System/Tests/6120oob.test
   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

elif [ "$PKGINST" = 117650 ] ; then
   echo ""
   echo $HIGHLIGHT
   echo "After the patch is completely installed, execute"
   echo "the program '$BASEDIR/$PKGDIR/bin/ras_install'."
   echo ""
   echo "Please refer to the patch README and release notes for"
   echo "additional installation, configuration, and usage"
   echo "information. The latest version of the release notes"
   echo "can be found at:"
   echo ""
   echo "    http://docs.sun.com/app/docs?q=819-0432 "
   echo $HIGHLIGHT

elif [ "$PKGINST" = 117654 ] ; then
   reconfigWebConsole
   echo ""
   echo $HIGHLIGHT
   echo "Complete the configuration by pointing your browser to:"
   echo ""
   echo "    https://<hostname>:6789 "
   echo ""
   echo "Please refer to the patch README and release notes for"
   echo "additional installation, configuration, and usage"
   echo "information. The latest version of the release notes"
   echo "can be found at:"
   echo ""
   echo "    http://docs.sun.com/app/docs?q=819-0432 "
   echo $HIGHLIGHT

fi

exit
