#!/bin/ksh -p
#
# ident "@(#)M39KioskL10N.ksh	1.2 08/08/08 SMI"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: KioskL10N
# AUTHOR     :
# DESCRIPTION: To install localized kiosk 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_ADMIN_FILE    : pathname of the admin file used by pkgadd. default provided.
# G_DEBUG         : debug on/off. Possible values "yes", "no"
# G_TMP_DIR       : pathname of the temporary directory 
# G_LOGFILE       : pathname of the log file.
# G_PID           : PID of the master task
#

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

    for pkg in $KIOSK_L10N_INSTLIST; do
	if [ -e ${KIOSK_L10N_DIR}/${pkg} ] ||
	   [ -L ${KIOSK_L10N_DIR}/${pkg} ]; then
	    _SW_PKG_LIST="${_SW_PKG_LIST} ${pkg}"
	fi
    done
}


#
# function Check_Kiosk_Installed 
#
# Check if kiosk is currently installed and determine the 
# installed version. Results are stored into module variables.
#
# Parameters:
#	(none)
#
# Returns:
#	If kiosk is found KIOSK_INSTALLED is set to "yes"
#	and KIOSK_INSTALLED_VERSION is set to the version identified.
#
function Check_Kiosk_Installed {

  if AnyPackageInstalled $KIOSK_PKG; then
     KIOSK_INSTALLED="yes"

     KIOSK_INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p \
				$KIOSK_PKG VERSION | cut -d_ -f1)
  fi
}

#
# function L10N_Preinstall
#
# Description:
#    Pre-install procedure
#
# Parameters:
#    (none)
#

function L10N_Preinstall {
  _DO_INSTALL="no"    


  if isTrustedSolaris ; then
    # Kiosk won't install, so we won't either
    # We rely on the kiosk module to explain, so no messages about this here.
    if [[ $_SW_INSTALLED = "yes" ]]; then
      # This should not really happen, but if it does we report what we do
      _DO_REMOVE="yes"
      AddPostInitMessage \
	"Remove\t [ $KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION $LUF ]"
    fi
    return 0
  fi

  if [[ $_SW_INSTALLED = "yes" ]]; then
    _DO_REMOVE="yes"

    if [[ -n $_SW_PKG_LIST ]]; then
      typeset KIOVTV="$KIOSK_INSTALLED_VERSION to $KIOSK_VERSION"
      AddPostInitMessage "Upgrade\t [ $KIOSK_PROD_NAME $LUF $KIOVTV ]"
      _DO_INSTALL="yes"
    else
      AddPostInitMessage \
        "Skip\t [ $KIOSK_PROD_NAME $KIOSK_VERSION $LUP not available ]"
    fi
  else
    if [[ -n $_SW_PKG_LIST ]]; then
      AddPostInitMessage "Install\t [ $KIOSK_PROD_NAME $KIOSK_VERSION $LUF ]"
      _DO_INSTALL="yes"
    else
      AddPostInitMessage "Skip\t [ $KIOSK_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
#

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

  # Find out about existing kiosk install
  Check_Kiosk_Installed

  if IsInstallRequired; then
    GetAvailablePackages
    L10N_Preinstall
    return_code=$?
  elif IsUninstallRequired && [[ $_SW_INSTALLED = "yes" ]]; then
    _DO_REMOVE="yes"
    AddPostInitMessage \
      "Remove\t [ $KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION $LUF ]"
  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_ADMIN_FILE

function Module_Remove {

   RemoveMsg "$KIOSK_PROD_NAME $LUF" "$KIOSK_INSTALLED_VERSION"

   RemoveProduct $G_ADMIN_FILE $KIOSK_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_ADMIN_FILE

function Module_Install {

  vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
      vflag="-v"
  fi
  InstallMsg "$KIOSK_PROD_NAME" "$KIOSK_VERSION $LUF"

  # cleanup old instances
  RemoveProduct $G_ADMIN_FILE $KIOSK_L10N_NAMELIST

  if [[ $? != 0 ]]; then
    Error "failed to completely remove $KIOSK_PROD_NAME $LUF"
  fi

  if ! InstallProduct $G_ADMIN_FILE ${KIOSK_L10N_DIR} $_SW_PKG_LIST; then
     # packages not installed correctly
     print -u2 \
             "$KIOSK_PROD_NAME $LUF 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"
DeclareModuleVar _SW_PKG_LIST=""

DeclareModuleVar KIOSK_INSTALLED="no"
DeclareModuleVar KIOSK_INSTALLED_VERSION=""
typeset -r KIOSK_VERSION="4.3"
typeset -r KIOSK_PKG="SUNWkio"
typeset -r KIOSK_PROD_NAME="Kiosk Mode"

# Recurring texts
typeset -r LUF="localized files"
typeset -r LUP="localized packages"

#
# BEGIN: Developers module variables definition here
#

export KIOSK_L10N_DIR=$(GetPackagePath Kiosk_Mode_4.3)

KIOSK_L10N_NAMELIST=""
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWckio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWdkio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWekio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWfkio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWhkio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWikio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWjkio"
KIOSK_L10N_NAMELIST="$KIOSK_L10N_NAMELIST SUNWkkio"

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

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
