#!/bin/ksh -p
#
# ident "@(#)utinstall-srss.ksh	1.127	11/03/10 Oracle"
#
# Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
#


PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

#
# Solaris 10 Trusted Extension guard
#
ORIGIN=`/usr/bin/dirname $0`
UTLIB=${ORIGIN:-/opt/SUNWut/lib}
UTIL_LIB=${UTLIB}/../lib/support_lib/util_lib
. $UTIL_LIB
FailExecInLocalZoneOnTx 
#
# Solaris 10 Trusted Extension guard 
#

trap "CleanupAndExit 1" HUP INT QUIT TERM

#
# function Usage
#
# Description:
#    Shows usage string
#
# Parameters:
#    (none)
#

function Usage {
  cat 1>&2 <<-EOF
	Usage: $G_PROGRAM_ID [opts]

	 -a admin_file
	         specify an admin(4) file to be used by pkgadd(1m)
		 calls during install (Solaris only)

	 -d media_dir
	         specify a directory to install the software from

	 -j jre
	         specify a directory to find the Java Runtime Environment

	 -u      uninstall the software
	 -A	 use responses from utdialog_responses.props
	 -c	 use responses from utdialog_defaults.props
	 -n	 error if user interaction is required
	EOF

  CleanupAndExit 1
}

#
# function CleanupAndExit
#
# Description:
#    clean up procedure before exiting. Send Exit event in case of normal
#    termination (error code 0). Abort event otherwise (failure or 
#    interruption)
#
# Parameters:
#    $1 : exit code
#

function CleanupAndExit {
  trap "" HUP INT QUIT TERM

  #
  # remove preserve/upgrade temporary files if procedure terminated 
  # successfully and send termination event (Exit or Abort) to all modules to 
  # do their cleanup procedure
  #
  if [[ $1 = "0" ]]; then

     rm -rf ${G_UPGRADE_DIR}/preserve 1> /dev/null 2>&1

     if [[ $G_MODE = "install" ]]; then
        rm -rf ${G_UPGRADE_DIR} 1> /dev/null 2>&1
     fi
     SendEventToModule Exit
  else
     SendEventToModule Abort
  fi

  #
  # remove all temporary files created
  #
  rm -rf ${G_TMP_DIR} 1> /dev/null 2>&1

  exit $1
}


function PrintFinalMessage {
   #
   # Description:
   #    Just to print a message at the end of installation/uninstallation
   #
   # Parameters:
   #    (none)
   #


   if [[ $G_MODE = "install" ]]; then
      fmt <<-EOF

	Installation of $G_UT_PROD_NAME has completed.

	EOF

   elif [[ $G_MODE = "uninstall" ]]; then
      print "\nThe system must be rebooted in order to complete un-install."
   fi
}

#
# function CheckDirectories
#
# Description:
#    Check existence of install directories (support_lib, modules)
#
# Parameters:
#    (none)
#
# Globals used:
#    G_MEDIA_DIR

function CheckDirectories {

   # Check existence of support_lib dir. if it doesn't, libraries cannot be
   # sourced
   if [[ ! -d $G_MEDIA_DIR/support_lib ]]; then
      echo "$0: Error, cannot find support_lib directory"
      exit 1
   fi

   # Check existence of modules dir. if it doesn't, modules cannot be 
   # called.
   if [[ ! -d $G_MEDIA_DIR/iu_modules ]]; then
      echo "$0: Error, cannot find modules directory"
      exit 1
   fi

   return 0
}

#
# function StartInstall
#
# Description:
#    To kick off the install/uninstall procedure
#
# Parameters:
#    (none)
#

function StartInstall {
   #
   # Initialize all modules
   #
   SendEventToModule Init

   if [[ $G_MODE = "install" ]] ; then
       print "\nAbout to Install Sun Ray Server Software 4.3"
       if ! YesOrNoDialog StartInstallContinueYN Y; then
           CleanupAndExit 1
       fi
   fi

   PrintPostInitMessage
   #
   # Start the install/unistall process
   #
   if [[ $G_MODE = "install" ]]; then
      SendEventToModule Preserve Remove Install Restore
   else # {
      if [[ $G_MODE = "uninstall" ]] ; then # {

	  # Stop SRSS services
	  srss_shutdown="$UT_ETC_DIR/basedir/lib/utshutdown-srss"
	  
	  if [ -x $srss_shutdown ] ; then    
	      $srss_shutdown 
	  else
	      $G_MEDIA_DIR/utshutdown-srss
	  fi
	  
	 # Check if interface is configured
	 if [[ $CONFIGURED = "true" ]]; then
            print "Unconfiguring Sun Ray Interconnect...\n"
	    #Check if LAN is configured 
            if grep "^[ 	]*allowLANConnections" $UT_ETC_DIR/auth.props \
	    | grep true 1>/dev/null 2>&1; then 
               $UT_ETC_DIR/basedir/sbin/utadm -L off >/dev/null 2>&1 
            fi 
            # Unconfigure Sun Ray Interconnect
            $UT_ETC_DIR/basedir/sbin/utadm -r 
	 fi
         # Check if Configured
	 if [[ -f  $UTADMIN_CONF ]] ; then
            # Check if Replication is enabled
            if grep '^pu.._replica' $UT_ETC_DIR/srds/current/utdsd.conf \
	    >/dev/null 2>&1 ; then
		print "Unconfiguring Sun Ray Replication...\n"
		$UT_ETC_DIR/basedir/sbin/utreplica -u
            fi
            print "Unconfiguring Sun Ray Data Store...\n"
            $UT_ETC_DIR/basedir/sbin/utconfig -fu
	 fi
      fi
      # }
      SendEventToModule Remove 
   fi
   # }

   PrintFinalMessage

   CleanupAndExit 0
}

function GetG_PRODUCT_DIR {
  (
    cd $(dirname $1)
    print "$(pwd)/.."
  )
}

# function main {

# global variables definition
#

export G_OS="$(uname -s)"

case "$(uname -m)" in
  sun4*)      export G_ISA="sparc";;
  i?86*|i86*) export G_ISA="i386";;
esac  

export G_OS_REL="$(uname -r)"

# location of the products
#
export G_PRODUCT_DIR="$(GetG_PRODUCT_DIR $0)"

# location of the support_lib and modules directories
export G_MEDIA_DIR="$G_PRODUCT_DIR/lib"

# options to be used for utdialog
export G_UTDIALOG_OPTS="-c SRSS"
export G_UTDIALOG_A=
export G_UTDIALOG_D=
export G_UTDIALOG_N=

# Perform sanity checks on directories
CheckDirectories

#
# include libraries and definitions
#

. ${G_MEDIA_DIR}/support_lib/gl_defs
. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/master_lib
. ${G_MEDIA_DIR}/support_lib/util_lib

#
# local variables definition
#
UT_ETC_DIR="/etc/opt/SUNWut"
UTADMIN_CONF="$UT_ETC_DIR/utadmin.conf"
CONFIGURED=""

typeset -r TMP_ADMIN_FILE="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.admin_default"

typeset -r OPTSTR="a:Acd:fj:nuv"

G_ADMIN_FILE=""

while getopts $OPTSTR OPT; do
  case "$OPT" in
    A) G_UTDIALOG_OPTS="$G_UTDIALOG_OPTS -a"; G_UTDIALOG_A="-a";;
    a) G_ADMIN_FILE="$OPTARG";;
    c) G_UTDIALOG_OPTS="$G_UTDIALOG_OPTS -d"; G_UTDIALOG_D="-d";;
    d) G_PRODUCT_DIR="$OPTARG";;
    j) G_JRE="$OPTARG";;
    f) G_QUICK_INSTALL="yes";;
    n) G_UTDIALOG_OPTS="$G_UTDIALOG_OPTS -n"; G_UTDIALOG_N="-n";;
    u) G_MODE="uninstall";;
    v) G_DEBUG="yes";;
    *) Usage;;
  esac
done
shift $(($OPTIND - 1))

if (( $# != 0 )); then
  Usage
fi

if [[ -z "$G_ADMIN_FILE" ]]; then
  G_ADMIN_FILE=$G_PRODUCT_DIR/admin_default
  #
  # must be assigned after cmdline opts are parsed, G_PRODUCT_DIR change
else
  case "$G_OS" in
    Linux) Fatal "option '-a' can not be used on Linux";;
  esac
fi

if [[ ! -f $G_ADMIN_FILE ]]; then
   G_ADMIN_FILE="/opt/SUNWut/etc/admin_default"
fi

#
# sanity checks
#
CheckUidIsZero

CheckMediaDir

# change into root directory to avoid the problem of 
# being invoked in one of the SunRay directories.
#
cd /

case "$G_OS" in
SunOS)
  CheckAdminFile

  cp "$G_ADMIN_FILE" "$TMP_ADMIN_FILE"
  G_ADMIN_FILE="$TMP_ADMIN_FILE"
  ;;
esac

# if uninstalling the software, copy support_lib and modules directory
# in a temporary location. Otherwise, they will be removed with the rest of
# the software
#
if [[ $G_MODE = "uninstall" ]]; then
  CONFIGURED=$(/etc/opt/SUNWut/dhcp/utdhcpservice | grep configured \
  | awk -F= '{print $2}')
  if [[ -f $UTADMIN_CONF ]] \
  || [[ $CONFIGURED = "true" ]] ; then
    print "\nWARNING: Sun Ray Server configuration detected.  Uninstalling"
    print "         the product will remove the existing configuration.\n"
  fi

  rm -rf ${G_TMP_DIR}/support_lib >/dev/null 2>&1
  rm -rf ${G_TMP_DIR}/iu_modules >/dev/null 2>&1
  rm -rf ${G_TMP_DIR}/utprodinfo >/dev/null 2>&1
  rm -rf ${G_TMP_DIR}/utshutdown-srss >/dev/null 2>&1
  rm -rf ${G_TMP_DIR}/utdialog >/dev/null 2>&1
  rm -rf ${G_TMP_DIR}/utdialog.d >/dev/null 2>&1

  cp -pr ${G_MEDIA_DIR}/support_lib ${G_TMP_DIR}/support_lib >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy support library into a temporary directory"
  fi

  cp -pr ${G_MEDIA_DIR}/iu_modules ${G_TMP_DIR}/iu_modules >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy modules into a temporary directory"
  fi

  cp -pr ${G_MEDIA_DIR}/utprodinfo ${G_TMP_DIR}/utprodinfo >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy utprodinfo into a temporary directory"
  fi

  cp -pr ${G_MEDIA_DIR}/utshutdown-srss ${G_TMP_DIR}/utshutdown-srss >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy utshutdown-srss into a temporary directory"
  fi

  cp -pr ${G_MEDIA_DIR}/utdialog ${G_TMP_DIR}/utdialog >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy utdialog into a temporary directory"
  fi

  cp -pr ${G_MEDIA_DIR}/utdialog.d ${G_TMP_DIR}/utdialog.d >/dev/null 2>&1
  if [[ $? != 0 ]]; then
    Fatal "cannot copy utdialog.d into a temporary directory"
  fi

  G_MEDIA_DIR=${G_TMP_DIR}
fi

print "# $G_PROGRAM_ID   Version: 4.3     $(date)\n" 

StartInstall 

# NOTREACHED - Since StartInstall calls CleanUpAndExit

# }
