#!/bin/ksh -p
#
# ident "@(#)M35L10N.ksh	1.17	12/12/10 Oracle"
#
# Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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
#

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

function L10N_Preinstall {
  _DO_INSTALL="no"    
  if [[ $_SW_INSTALLED == "yes" ]]; then
    _DO_REMOVE="yes"
    LUF="Localized user files"
    LUP="Localized user packages"

    GSRUTV="$G_SR_CURRENT_VERSION to $G_UT_VERSION"
    AddPostInitMessage "Upgrade\t [ $G_UT_PROD_NAME $LUF $GSRUTV ]"
    _DO_INSTALL="yes"
  else
    AddPostInitMessage "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION $LUF ]"
    _DO_INSTALL="yes"
  fi
}


#
# 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 {
  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_NAMELIST || \
    ProductPartiallyInstalled $UT_L10N_NAMELIST ; then
    #
    # Check if the localized User files are already installed.
    #
    _SW_COMPATIBLE="yes"
    _SW_INSTALLED="yes"
  fi

  if AnyPackageInstalled $UT_SUNMC_NAMELIST || \
    ProductPartiallyInstalled $UT_SUNMC_NAMELIST ; then
    # Remove any obsolete SunMC L10N packages
    AddPostInitMessage "Remove\t [ $G_UT_PROD_NAME obsolete Localized SunMC files ]"
  fi
    

  if IsInstallRequired; then
    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
}

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

function Module_Preserve {

   return 0

}

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

function Module_Remove {

  if AnyPackageInstalled $UT_SUNMC_NAMELIST || \
    ProductPartiallyInstalled $UT_SUNMC_NAMELIST ; then
    # Remove any obsolete SunMC L10N packages
    RemoveMsg "$G_UT_PROD_NAME obsolete Localized SunMC files" any
    RemoveProduct $G_ADMIN_FILE $UT_SUNMC_NAMELIST
  fi

  RemoveMsg "$G_UT_PROD_NAME Localized files" "$G_SR_CURRENT_VERSION"

  RemoveProduct $G_ADMIN_FILE $UT_L10N_NAMELIST

  #
  # 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:
#    To install English docs
#
# 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 Localized files"

  # cleanup old instances
  RemoveProduct $G_ADMIN_FILE $UT_L10N_NAMELIST

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

  if ! InstallProduct $G_ADMIN_FILE ${UT_L10N_DIR} $UT_L10N_INSTLIST; then
     # packages not installed correctly
     print -u2 \
             "$G_UT_PROD_NAME Localized files 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

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
#

export UT_L10N_DIR=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})

# This list of languages must match the one in "src/release/image/builder.ksh".

UT_L10N_NAMELIST=""
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWcuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWduto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWeuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWfuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWhuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWiuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWjuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWkuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWnuto"
UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWputo"

# Note order in this list is critical: utesc must come first so it is
# removed last.
UT_SUNMC_NAMELIST=
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWutesc"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWcutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWdutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWeutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWfutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWhutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWiutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWjutes"
UT_SUNMC_NAMELIST="$UT_SUNMC_NAMELIST SUNWkutes"


case "$G_OS" in
SunOS)
	# For Solaris, the package name is the same as the name
	UT_L10N_INSTLIST="$UT_L10N_NAMELIST"
	;;
Linux)
	# For Linux, the package name includes an extension
	typeset LINUX_PKG_EXT="-4.5-?*.i?86.rpm"
	typeset PkgName
	UT_L10N_INSTLIST=""
	for PkgName in $UT_L10N_NAMELIST
	do
		UT_L10N_INSTLIST="$UT_L10N_INSTLIST ${PkgName}${LINUX_PKG_EXT}"
	done
	;;
esac
#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
