#!/bin/ksh
#
# ident "@(#)M35L10N.ksh	1.10 05/08/08 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# 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_INSTLIST.
#
# 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_INSTLIST; do
	if [ -e ${UT_L10N_DIR}/${pkg} ] ||
	   [ -L ${UT_L10N_DIR}/${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"
    LUF="Localized user files"
    LUP="Localized user packages"

    if [[ -n $_SW_PKG_LIST ]]; then
      GSRUTV="$G_SR_CURRENT_VERSION to $G_UT_VERSION"
      AddPostInitMessage "Upgrade\t [ $G_UT_PROD_NAME $LUF $GSRUTV ]"
      _DO_INSTALL="yes"
    else
      AddPostInitMessage \
        "Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION $LUP not available ]"
    fi
  else
    if [[ -n $_SW_PKG_LIST ]]; then
      AddPostInitMessage "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION $LUF ]"
      _DO_INSTALL="yes"
    else
      AddPostInitMessage "Skip\t [ $G_UT_PROD_NAME $LUP 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_NAMELIST || \
    ProductPartiallyInstalled $UT_L10N_NAMELIST ; then
    #
    # Check if the localized User files are already installed.
    #
    _SW_COMPATIBLE="yes"
    _SW_INSTALLED="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"

   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
}

#
# 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
  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} $_SW_PKG_LIST; then
     # packages not installed correctly
     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=""

#
# BEGIN: Developers module variables definition here
#

export UT_L10N_DIR=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})

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 SUNWsuto"

if [ "$G_OS" = "SunOS" ]; then
   # SunMC packages are available on Solaris only
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWcutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWdutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWeutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWfutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWhutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWiutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWjutes"
   UT_L10N_NAMELIST="$UT_L10N_NAMELIST SUNWkutes"
fi

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="-3.1-?*.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
