#!/bin/ksh
#
# ident "@(#)M35L10N.ksh	1.6 02/09/03 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# MODULE NAME: L10N
# AUTHOR     :
# DESCRIPTION: To install localized files
#
# 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
# G_SR_CURRENT_VERSION: currently installed SR version
#

#
# GetAvailablePackages()
#
# Get a list of available packages currently available on the cdrom
# based on the list specified in UT_L10N_LIST.
#
# Parameters:
#	$*	: list of packages names
#
# Returns:
#	Module specific variable _SW_PKG_LIST gets the list of packages
#	currently available on the cdrom.
#
GetAvailablePackages() {
    typeset pkg=""
    _SW_PKG_LIST=""

    for pkg in $UT_L10N_LIST; do
	if [[ -d ${UT_L10N_DIR}/Packages/${pkg} ]]; then
	    _SW_PKG_LIST="${_SW_PKG_LIST} ${pkg}"
	fi
    done
}


#
# L10N_Preinstall()
#
# Description:
#    Pre-install procedure
#
# Parameters:
#    (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_SR_CURRENT_VERSION

L10N_Preinstall() {

    _DO_INSTALL="no"    
    if [[ $_SW_INSTALLED = "yes" ]]; then

        _DO_REMOVE="yes"
	if [[ -n $_SW_PKG_LIST ]]; then
	  AddPostInitMessage \
	  "Upgrade\t [ $G_UT_PROD_NAME Localized user files $G_SR_CURRENT_VERSION to $G_UT_VERSION ]"
	  _DO_INSTALL="yes"

        else
	  AddPostInitMessage \
	  "Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION Localized user packages not available ]"
	fi

    else
        if [[ -n $_SW_PKG_LIST ]]; then
	  AddPostInitMessage \
	  "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION Localized user files ]"
	  _DO_INSTALL="yes"

      	else
	  AddPostInitMessage \
	  "Skip\t [ $G_UT_PROD_NAME Localized User packages not available ]"
	fi

    fi
}


#
# 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() {

   return_code=0
   _SW_INSTALLED="no"
   _DO_REMOVE="no"
   _DO_INSTALL="no"

   if [[ -n $G_SR_CURRENT_VERSION ]] && [[ $G_SR_CURRENT_VERSION < "2.0" ]]; then
#
# special handling for 1.x upgrade.  We mark the package as installed but
# incompatible.
#
      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="no"

   elif AnyPackageInstalled $UT_L10N_LIST || \
	ProductPartiallyInstalled $UT_L10N_LIST ; then
#
# Check if the localized User files are already installed.
#
      _SW_COMPATIBLE="yes"
      _SW_INSTALLED="yes"

   elif AnyPackageInstalled $UT_DEPRECATED_L10N_LIST || \
	ProductPartiallyInstalled $UT_DEPRECATED_L10N_LIST; then
#
# Check if the deprecated localized User packages are already installed.
#
      _SW_COMPATIBLE="yes"
      _SW_INSTALLED="yes"
      _SW_DEPRECATED_PKG="yes"
   fi

   if IsInstallRequired; then
      GetAvailablePackages
      L10N_Preinstall
      return_code=$?

   elif IsUninstallRequired && [[ $_SW_INSTALLED = "yes" ]]; then

      _DO_REMOVE="yes"
      AddPostInitMessage \
	"Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION Localized user files ]"
   fi

   return return_code
}

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

Module_Preserve() {

   return 0

}

#
# Module_Remove()
#
# Description:
#    Remove L10N files
#
# Parameters:
#   (none)
#
# Globals used:
#   G_UT_PROD_NAME
#   G_ADMIN_FILE

Module_Remove() {

   RemoveMsg "$G_UT_PROD_NAME Localized files" "$G_SR_CURRENT_VERSION"

   if [[ $_SW_DEPRECATED_PKG = "yes" ]]; then
	#
	# this is needed for upgrades between 2.0 builds
	#
	RemoveProduct $G_ADMIN_FILE $UT_DEPRECATED_L10N_LIST
   else
	RemoveProduct $G_ADMIN_FILE $UT_L10N_LIST
   fi

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

   return 0
}

#
# Module_Install()
#
# Description:
#    To install English docs
#
# Parameters:
#   (none)
#
# Globals used:
#    G_DEBUG
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE

Module_Install() {

  vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
      vflag="-v"
  fi
  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION Localized files"

  # cleanup old instances
  if [[ $_SW_DEPRECATED_PKG = "yes" ]]; then
	#
	# this is needed for upgrades between 2.0 builds
	#
	RemoveProduct $G_ADMIN_FILE $UT_DEPRECATED_L10N_LIST
  else
	RemoveProduct $G_ADMIN_FILE $UT_L10N_LIST
  fi

  if [[ $? != 0 ]]; then
    Error "failed to completely remove $G_UT_PROD_NAME Localized files"
  fi

  pkgadd $vflag -a $G_ADMIN_FILE -d ${UT_L10N_DIR}/Packages $_SW_PKG_LIST

#
# check that packages have been installed correctly
#
  if ! ProductInstalled $_SW_PKG_LIST
       ProductPartiallyInstalled $_SW_PKG_LIST; then
     print -u2 \
             "$G_UT_PROD_NAME Localized files not successfully installed"

     return 2
  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"
DeclareModuleVar _SW_PKG_LIST=""
DeclareModuleVar _SW_DEPRECATED_PKG="no"

#
# BEGIN: Developers module variables definition here
#

export UT_L10N_DIR="${G_PRODUCT_DIR}/${G_SUNRAY_SERVER_DIR}/Solaris_8+"

export UT_L10N_LIST="SUNWcuto SUNWduto SUNWeuto \
	SUNWfuto SUNWhuto SUNWiuto SUNWjuto SUNWkuto SUNWsuto \
	SUNWcutes SUNWdutes SUNWeutes \
	SUNWfutes SUNWhutes SUNWiutes SUNWjutes SUNWkutes"
#
# need to define the deprecated package list so that we can remove them
# when upgrading between 2.0 builds
#
export UT_DEPRECATED_L10N_LIST="SUNWcutlu SUNWdutlu SUNWeutlu \
	SUNWfutlu SUNWhutlu SUNWiutlu SUNWjutlu SUNWkutlu SUNWsutlu"



#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
