#!/bin/ksh -p
#
# ident "@(#)M09SunRayCore.ksh	1.70	11/01/26 Oracle"
#
# Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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.
#

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

function 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
}

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

function 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
#

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

function Module_Init {

  typeset exit_no="0"
  _SW_INSTALLED="no"

  if AnyPackageInstalled $UT_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
}

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

function Module_Preserve {

   SaveCurrentSRVersion

   return 0
}

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

function Module_Remove {

  typeset status=0

  RemoveMsg "$G_UT_PROD_NAME" "$INSTALLED_VERSION"

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

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

  return $status
}

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

  typeset vflag=""
  typeset I_PKGS=""

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

  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION"

  # cleanup old instances
  #
  if ! RemovePatch $UT_PATCHES; then
    Error "failed to remove all instances of old $G_UT_PROD_NAME patches"
  fi

  if ! RemoveProduct $G_ADMIN_FILE $UT_PKG_LIST; then
    Error "failed to completely remove $G_UT_PROD_NAME"
  fi

  case "$G_OS" in
    SunOS) I_PKGS="$UT_PKG_LIST";;
    Linux) I_PKGS="$UT_RPM_LIST";;
  esac

  if ! InstallProduct $G_ADMIN_FILE ${UT_DIR} $I_PKGS; 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
}

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

function Module_Restore {

   return 0
}

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

function Module_Abort {

   return 0
}

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

function 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=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})

DeclareModuleVar UT_PKG_LIST=""

# The ordering of SUNWuto and SUNWutr is critical for uninstall
# Same applies for SUNWutsto and SUNWutstr
#
PKGS1=""
PKGS1="$PKGS1 SUNWuti"
PKGS1="$PKGS1 SUNWutid"
PKGS1="$PKGS1 SUNWuto"
PKGS1="$PKGS1 SUNWutfw"
PKGS1="$PKGS1 SUNWutr"
PKGS1="$PKGS1 SUNWutu"
PKGS1="$PKGS1 SUNWuta"
PKGS1="$PKGS1 SUNWutsto"
PKGS1="$PKGS1 SUNWutstr"
PKGS1="$PKGS1 SUNWutgsm"
PKGS1="$PKGS1 SUNWutps"
PKGS1="$PKGS1 SUNWutref"
PKGS1="$PKGS1 SUNWlibusbut"

case "$G_OS" in
SunOS)
  PKGS2=""
  PKGS2="$PKGS2 SUNWutk"
  PKGS2="$PKGS2 SUNWutstk"
  PKGS2="$PKGS2 SUNWutesc"
  PKGS2="$PKGS2 SUNWutscr"
  if [ -x /usr/openwin/bin/Xsun ]; then
    PKGS2="$PKGS2 SUNWutxsun"
  fi
  ;;

Linux)
  PKGS2=""
  PKGS2="$PKGS2 SUNWutkau"
  PKGS2="$PKGS2 SUNWutio"
  PKGS2="$PKGS2 SUNWutdsk"
  ;;
esac

RPMS=""
RPMS="$RPMS SUNWuti-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutid-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWuto-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutfw-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutr-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutu-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWuta-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutsto-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutstr-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutps-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutkau-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutio-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutdsk-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutgsm-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWutref-4.3-?*.i386.rpm"
RPMS="$RPMS SUNWlibusbut-4.3-?*.i386.rpm"

typeset UT_PKG_LIST="$PKGS1 $PKGS2"	# used by query, remove and 'pkgadd' ops
typeset UT_RPM_LIST="$RPMS"		# used by 'rpm -i' ops

# List obsoleted packages to be removed when upgrading from pre-3.0 SRSS.
OBSOLETED_PACKAGES=""
OBSOLETED_PACKAGES="$OBSOLETED_PACKAGES SUNWutj"
OBSOLETED_PACKAGES="$OBSOLETED_PACKAGES SUNWutkx"
OBSOLETED_PACKAGES="$OBSOLETED_PACKAGES 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
