#!/bin/ksh -p
#
# ident "@(#)M50EnterpriseManager.ksh	1.27	13/01/04 Oracle"
#
# Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
#

#
# MODULE NAME: UtEnterpriseManager
# AUTHOR     : Ronald Hixon
# DESCRIPTION: Used for installing / uninstalling Sun Ray module for EM
#	       Also used for Preserving/Restoring EM data
#
# 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.
# G_PID           : PID of the master task
#

#
# 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:
#    G_MEDIA_DIR
#    G_UT_PROD_NAME
#    G_SR_CURRENT_VERSION
#

function Module_Init {

  _SW_INSTALLED="no"

   if AnyPackageInstalled $UT_PKG_RLIST || \
	ProductPartiallyInstalled $UT_PKG_RLIST ; then
     _SW_INSTALLED="yes"
     CompareVersion $G_SR_CURRENT_VERSION "4.5"
     compare_status=$?
  fi

  if IsInstallRequired ; then
       _DO_INSTALL="yes"
       if [[ $_SW_INSTALLED = "yes" ]]; then
           _DO_REMOVE="yes"
	   _DO_RESTORE="yes"
	   if [[ $compare_status -eq 2 ]]; then
	       AddPostInitMessage \
	       "Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION modules for SunMC ]"
	       AddPostInitMessage \
	       "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION modules for Oracle Enterprise Manager ]"
	   else
	       AddPostInitMessage \
	       "Upgrade\t [ $G_UT_PROD_NAME Oracle Enterprise Manager modules $G_SR_CURRENT_VERSION to $G_UT_VERSION ]"
	   fi
       else
	   AddPostInitMessage "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION modules for Oracle Enterprise Manager ]"
       fi
  elif IsUninstallRequired && [[ ${_SW_INSTALLED} = "yes" ]]; then
       _DO_REMOVE="yes"
       if [[ $compare_status -eq 2 ]]; then
	   AddPostInitMessage \
	   "Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION modules for SunMC ]"
       else
	   AddPostInitMessage \
	   "Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION modules for Oracle Enterprise Manager ]"
       fi
  fi

   return 0

}

#
# function Module_Preserve
#
# Description:
#   If installed build is earlier than 1.3_10.b do not preserve data.
#   The config file is not necessary since libutsmon will use
#   a dynamic version if a real one does not exist.
#
# Parameters:
#   (none)
#
# Globals used:
#

function Module_Preserve {

  return 0

}

#
# function Module_Remove
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_SR_CURRENT_VERSION
#    G_UT_VERSION
#    G_ADMIN_FILE
#

function Module_Remove {

   CompareVersion $G_SR_CURRENT_VERSION "4.5"
   compare_status=$?
   if [[ $compare_status -eq 2 ]]; then
	RemoveMsg "$G_UT_PROD_NAME" "$G_SR_CURRENT_VERSION modules for SunMC"
   else
	RemoveMsg "$G_UT_PROD_NAME" "$G_SR_CURRENT_VERSION module for Oracle Enterprise Manager"
   fi

   RemoveProduct $G_ADMIN_FILE $UT_PKG_RLIST

#
# if the removal didn't succeed, we exit with 1 so to keep the procedure
# going...
#
   if [[ $? != 0 ]]; then
      return 1
   fi

  return 0

}

#
# function Module_Install
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#    G_DEBUG
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE
#

function Module_Install {

  vflag=""

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

  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION module for Oracle Enterprise Manager"

  if ! InstallProduct $G_ADMIN_FILE ${UT_DIR} $UT_PKG_LIST; then
     print -u2 \
             "$G_UT_PROD_NAME module for Oracle Enterprise Manager not successfully installed"

     return 2
  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
. ${G_MEDIA_DIR}/support_lib/util_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"
DeclareModuleVar _DO_RESTORE="no"	# this is to force the framework to
					# invoke the Module_Restore

#
# BEGIN: Developers module variables definition here
#

export UT_ETC_DIR="/etc/opt/SUNWut"
export UT_VAR_DIR="/var/opt/SUNWut"

typeset -r UT_DIR=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})

if [[ "$G_OS" == "Linux" ]]; then
    typeset -r UT_PKG_LIST="SUNWutesa-?*.rpm"
    typeset -r UT_PKG_RLIST="SUNWutesa"
else
    typeset -r UT_PKG_LIST="SUNWutesa"
    typeset -r UT_PKG_RLIST=$UT_PKG_LIST
fi

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
