#!/bin/ksh 
#
# ident "@(#)M45UtSettings.ksh	1.22 04/04/15 SMI"
#
# Copyright 2001-2002,2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: UtSettings.ksh 
# AUTHOR     : 
# DESCRIPTION: Used for Preserving/Restoring utsettings 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
#

#
# Module_Init()
#
# Description:
#   initialization of the module for installation, uninstallation
#
# Parameters:
#   (none)
#
# Globals used:
#   G_MEDIA_DIR
#

Module_Init() {

  if IsUninstallRequired; then

     return 0

  fi

  INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p SUNWutr PRODVERS 2>&-)

  if [[ -z "$INSTALLED_VERSION" ]] ; then
    _SW_INSTALLED="no"
  else
    _SW_INSTALLED="yes"
    CompareVersion $INSTALLED_VERSION $VALID_UPTO_VERSION
    if [[ $? -ne 1 ]] ; then
	# if the current installed SunRay version is not greater than
	# the specified VALID_UPTO_VERSION, set compatibility to false
	# so it prints the upgrade message
	_SW_COMPATIBLE="no"
    fi
  fi

  if IsInstallRequired ; then
       _DO_INSTALL="yes"
       if IsDataPreserved || [[ ${_SW_COMPATIBLE} = "no" ]]; then
          AddPostInitMessage "Upgrade\t [ data for utsettings ]"
       fi
  elif IsPreserveRequired ; then
       if [[ ${_SW_INSTALLED} = "yes" ]]; then
          AddPostInitMessage "Preserve [ data for utsettings ]"
       fi
  fi

  return 0
}

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

Module_Preserve() {

   if [[ ${_SW_COMPATIBLE} = "yes" ]] ; then
      # no need to preserve utsettings files
      return 0
   fi
   SaveFiles $UTSETTINGS_PRESERVE_FILES
   if [[ $? != 0 ]]; then
      return 2
   fi
   return 0

}

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

Module_Remove() {

   return 0

}

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

Module_Install() {

  return 0

}

#
# Module_Restore()
#
# Description:
#    Restore saved configuration data
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Restore() {
 typeset path
 typeset file

 PRESERVED_DIR=${G_UPGRADE_DIR}/preserve/M46Utslaunch

 if GetPreservedFilePath $UTSETTINGS_DEFAULT_FILE; then
    path=${_RETURN_VAL}
    if [ -f $PRESERVED_DIR/$UTSLAUNCH_DEFAULT_FILE ]; then      
        UpdateFields "$path" $PRESERVED_DIR/$UTSLAUNCH_DEFAULT_FILE
    else
        cp -p "$path" $PRESERVED_DIR/$UTSLAUNCH_DEFAULT_FILE
    fi
 fi   
 if GetPreservedFilePath $UTSETTINGS_MANDATORY_FILE; then
    path=${_RETURN_VAL}
    cp -p "$path" $PRESERVED_DIR/$UTSLAUNCH_MANDATORY_FILE
 fi   

 echo "Restoration of Utsettings data complete"

 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/upgrade_lib
. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/module_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"

#
# BEGIN: Developers module variables definition here
#

export VALID_UPTO_VERSION=1.3
export UT_ETC_DIR="/etc/opt/SUNWut"

export UTSETTINGS_DEFAULT_FILE="${UT_ETC_DIR}/utsettings_defaults.properties"
export UTSETTINGS_MANDATORY_FILE="${UT_ETC_DIR}/utsettings_mandatory.properties"
export UTSLAUNCH_DEFAULT_FILE="${UT_ETC_DIR}/utslaunch_defaults.properties"
export UTSLAUNCH_MANDATORY_FILE="${UT_ETC_DIR}/utslaunch_mandatory.properties"

export UTSETTINGS_PRESERVE_FILES="
                             ${UTSETTINGS_DEFAULT_FILE} \
                             ${UTSETTINGS_MANDATORY_FILE} "

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
