#!/bin/ksh -p
#
# ident "@(#)M25SunRayCore.ksh	1.37 02/09/18 SMI"
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# MODULE NAME: SunRayCore
# AUTHOR     :
# DESCRIPTION: To install SunRay core packages 
#              (Uninstallation of the software is provided by 08SunRayCore 
#              module)
#              
#
# The following exported variables (initialized by the master task) are 
# available (read-only) for the module:
#
# G_PROGRAM_ID    : program name
# G_MEDIA_DIR     : pathname of the install directory on the media (CD,...)
# G_PRODUCT_DIR   : pathname of the bundles directory on the media (CD,...)
# G_ADMIN_FILE    : pathname of the admin file used by pkgadd. default provided.
# G_DEBUG         : debug on/off. Possible values "yes", "no"
# G_QUICK_INSTALL : used to force a quick install (no user inputs).
#                   possible values "yes", "no"
# G_TMP_DIR       : pathname of the temporary directory 
# G_UT_PROD_NAME  : product name
# G_UT_VERSION    : product version
# G_DAEMON_LOC    : location of OS daemon scripts
# G_LOGFILE       : pathname of the log file.
#

#
# UT_Preinstall()
#
# Description:
#    Pre-install procedure: ask whether Sun Ray software is to be installed
#
# Parameters:
#    (none)
#
# Globals used:
#    G_SR_CURRENT_VERSION

UT_Preinstall(){

   if [[ $_SW_INSTALLED = "yes" ]]; then
      ProductMsg -n "$G_UT_PROD_NAME $INSTALLED_VERSION"
      print installed
#
# remove old instance of Sun Ray
#
      _DO_REMOVE="yes"
   else
      ProductMsg -n "$G_UT_PROD_NAME $G_UT_VERSION"
      print "not installed"
   fi

   _DO_INSTALL="yes"

   if [[ $_SW_INSTALLED = "yes" ]]; then
      AddPostInitMessage \
            "Upgrade\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION to $G_UT_VERSION  ]"
   else
      AddPostInitMessage "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION ]"
   fi

   return 0
}

#
# UT_Preremove()
#
# Description:
#    Pre-install procedure: ask whether Sun Ray software has to be removed
#
# Parameters:
#    (none)
#
# Globals used:
#    (none)

UT_Preremove(){ 
     print "\nRemoval of $G_UT_PROD_NAME Software\n"
 
     if [[ $_SW_INSTALLED = "yes" && $_SW_COMPATIBLE = "yes" ]]; then
         _DO_REMOVE="yes"
         AddPostInitMessage "Remove\t [ $G_UT_PROD_NAME $INSTALLED_VERSION ]"
         return 0
     fi
     Error "No Sun Ray software packages installed." 
}

#
# Module developers to provide the following functions:
# Module_Init, Module_Preserve, Module_Remove, Module_Install, Module_Restore
# Module_Abort, Module_Exit
#

#
# Module_Init()
#
# Description:
#   initialization of the module for installation, uninstallation
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Init() {

  typeset exit_no="0"
  _SW_INSTALLED="no"

  UT_PKG_LIST="${UT_PKG_LIST_ALLOS}"

  if AnyPackageInstalled $UT_PKG_LIST $UT_X_PKG_LIST; then
     _SW_INSTALLED="yes"
     _SW_COMPATIBLE="yes"

     GetCurrentSRVersion
     INSTALLED_VERSION=$_RETURN_VAL
  fi
 
  if IsInstallRequired; then
     UT_Preinstall
     exit_no=$?

  elif IsUninstallRequired; then
     UT_Preremove
     exit_no=$?

  elif IsPreserveRequired; then
     if [[ $_SW_INSTALLED != "yes" ]]; then
        Error "No Sun Ray software packages installed." 
     fi
  fi

  return $exit_no
}

#
# Module_Preserve()
#
# Description:
#   
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Preserve() {

   SaveCurrentSRVersion

   return 0
}

#
# Module_Remove()
#
# Description:
#    Remove SunRay Core Packages
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Remove() {

  typeset status=0

  RemoveMsg "$G_UT_PROD_NAME" "$INSTALLED_VERSION"

  RemovePatch $UT_PATCHES || status=1
  RemoveProduct $G_ADMIN_FILE $UT_PKG_LIST $UT_X_PKG_LIST || status=1

  if [[ $status != 0 ]]; then
     Note "$G_UT_PROD_NAME not successfully removed"
  fi

  return $status
}

#
# Module_Install()
#
# Description:
#    To install SunRay Core Packages
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Install() {

  typeset vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
     vflag="-v"
  fi

  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION"

  # cleanup old instances
  RemovePatch $UT_PATCHES || \
    Error "failed to remove all instances of old $G_UT_PROD_NAME patches"
  RemoveProduct $G_ADMIN_FILE $UT_PKG_LIST $UT_X_PKG_LIST || \
    Error "failed to completely remove $G_UT_PROD_NAME"

  if ! SunOSRelease 5.6 && ProductInstalled $OS_64BIT_PKG; then
    pkgadd $vflag -a $G_ADMIN_FILE -d ${UT_DIR}/Packages $UT_X_PKG_LIST
  fi
  pkgadd $vflag -a $G_ADMIN_FILE -d ${UT_DIR}/Packages $UT_PKG_LIST

  InstallPatches ${UT_DIR}/Patches

  if ! ProductInstalled $UT_PKG_LIST || \
       ProductPartiallyInstalled $UT_PKG_LIST $UT_X_PKG_LIST; then
     if IsDataPreserved; then
        fmt <<-!

	$G_UT_PROD_NAME has not installed correctly. All data saved during the
	upgrade "Save & Restore" has been retained at the following location:
	${G_UPGRADE_DIR}

	!
      fi 
      Error "$G_UT_PROD_NAME not successfully installed"
   fi

   return 0
}

#
# Module_Restore()
#
# Description:
#    Restore saved configuration data
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Restore() {

   return 0
}

#
# Module_Abort()
#
# Description:
#    Abort procedure
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Abort() {

   return 0
}

#
# Module_Exit()
#
# Description:
#    Exit procedure (normal termination)
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Exit() {

   return 0

}

#
# END
#

#
# MAIN STARTS HERE
#

trap "exit 2" HUP INT QUIT TERM

#
# include libraries
#

. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/module_lib
. ${G_MEDIA_DIR}/support_lib/upgrade_lib

export _EVENT=$1
export _MODULE_NAME=$(basename $0)
export _VARS_LIST=""
export _EXIT_CODE=0
export _RETURN_VAL=0
export _VAR_STORAGE_FILE="${G_TMP_DIR}/.${G_PROGRAM_ID}.${_MODULE_NAME#???}"

DeclareModuleVar _DO_INSTALL="no"
DeclareModuleVar _DO_REMOVE="no"
DeclareModuleVar _SW_INSTALLED="no"
DeclareModuleVar _SW_COMPATIBLE="yes"

#
# BEGIN: Developers module variables definition here
#

typeset -r UT_DIR="${G_PRODUCT_DIR}/${G_SUNRAY_SERVER_DIR}/Solaris_8+"

DeclareModuleVar UT_PKG_LIST=""

# The ordering of 'SUNWuto SUNWutr' is critical for uninstall
typeset -r UT_PKG_LIST_ALLOS='SUNWuta SUNWutj SUNWuto SUNWutr SUNWutk SUNWutu
                              SUNWutesc SUNWutps SUNWutscr'

typeset -r UT_X_PKG_LIST="SUNWutkx SUNWutux"

typeset -r UT_PATCHES="108303 109127"

typeset -r OS_64BIT_PKG="SUNWcsxu"

DeclareModuleVar INSTALLED_VERSION=""

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
