#!/bin/bash

# This is the script used to launch the Guided Setup & Installation Wizard

x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
export LD_LIBRARY_PATH=/usr/sbin/rsct/lib:/usr/lib:/opt/hsc/lib:$LD_LIBRARY_PATH
export PATH=$JAVAPATH:$PATH

# Below is a temporary workaround for JRE 1.3.1 bug in the BigInteger class.
export JITC_COMPILEOPT=NINLINING{java/math/BigInteger}{oddModPow}

HOSTNAME=`/bin/hostname`
LOCALE=$LANG
EXIT_RC=0

# Ensure that the effective current user is hscroot.
USER_NAME=`id -nu`              
if [ "$USER_NAME" != "hscroot" ]
then
    exit 3 
fi

if [ ! -f /opt/hsc/data/GuidedSetupWizard.status ]; then

   # No status file exists - wizard has not been run yet, so do it!
   # Note that normal wizard program completion will reboot the HMC (after updating the status file)
   /usr/websm/bin/wjava -Djavax.net.ssl.keyStore=/opt/ccfw/data/SM.pubkr -Djavax.net.ssl.keyStorePassword=defp -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties -Djava.security.policy=/usr/websm/config/factory_defaults/websm.policy -DworkingCursorForActions=true -DhscConsole=true com.ibm.hsc.iw.WizardFrameTask /opt/hsc/data/GuidedSetupTasks.xml true
   RAN_WIZARD=1
else
   RAN_WIZARD=0
fi

# The status file should exist by now - see what's inside...
# there is the possibility that the HMC will not have been rebooted, but the status
# file may now exist.
if [ -f /opt/hsc/data/GuidedSetupWizard.status ]; then
   read STATUS < /opt/hsc/data/GuidedSetupWizard.status
   
   if [ $STATUS = "completed_reboot" -a $RAN_WIZARD = "1" ]; then
      EXIT_RC=1 
   elif [ $STATUS = "completed_logout" -a $RAN_WIZARD = "1" ]; then
      EXIT_RC=2 
   elif [ $STATUS = "completed" -o $STATUS = "completed_reboot" -o $STATUS = "completed_logout" ]; then
      # status file will be updated in the following class
      #if [ $RAN_WIZARD == 1 ]; then
      #   # No need to run the web browser in the background. 
      #   /usr/websm/bin/wjava com.ibm.hsc.iw.LaunchInfoCenter $HOSTNAME $LOCALE
      #else
      #   # Run the web browser in the background since this is being run at boot.
         /usr/websm/bin/wjava com.ibm.hsc.iw.LaunchInfoCenter $HOSTNAME $LOCALE &
      #fi
   fi
fi

exit $EXIT_RC
