#!/bin/ksh
#
# ident "@(#)module_lib.ksh	1.18 01/05/28 SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# module_lib.ksh
#
# functions to be used by the modules
#

#
# DeclareModuleVariable()
#
# Description:
#    To declare a "static" variable inside the module (i.e. the value of the
#    variable is preserved whenever the module is called)
#
# Parameters:
#    $1 - variable name and, optionally, the value
#
#         example:  VAR1
#                   VAR2=value
#
# Module globals used:
#    _VAR_LIST
#
# Globals used:
#    (none)

DeclareModuleVar() {

   typeset var_name=""

   export "${1}"

#
#  _VARS_LIST contains the list of variable in the form:
#
#       echo <varname>=$<varname>; ...
#
# (see SaveModuleVars for more details...)

   var_name=`echo ${1} | cut -f1 -d=`
   _VARS_LIST="${_VARS_LIST}echo ${var_name}=\'\${${var_name}-}\';"

}

#
# SaveModuleVars()
#
# Description:
#    To save the state of all "static" variables
#
# Parameters:
#    (none)
#
# Module globals used:
#    _EVENT
#    _VARS_LIST
#    _VAR_STORAGE_FILE
#    _MODULE_NAME
#
# Globals used:
#    G_TMP_DIR

SaveModuleVars() {
   rm -f $_VAR_STORAGE_FILE > /dev/null 2>&1

   if [[ $_EVENT = "Abort" || $_EVENT = "Exit" ]]; then
      return 0
   fi

#
# save the variables content into a storage file
# The way it is carried out is:
#    _VARS_LIST contains the list of variable in the form:
#       echo <varname>=$<varname>; ...
#
# Using eval, $<varname> is expanded with the content of the variable. 
# the output of echo commands is redirected to the variable storage file.
#

   eval $_VARS_LIST > $_VAR_STORAGE_FILE

   return 0
}

#
# RestoreModuleVars()
#
# Description:
#    To restore the value of all "static" variables
#
# Parameters:
#    (none)
#
# Module globals used:
#    _VAR_STORAGE_LIST
#
# Globals used:
#    (none)

RestoreModuleVars() {

   if [[ -f $_VAR_STORAGE_FILE ]]; then
      . $_VAR_STORAGE_FILE
   fi

}

#
# IsInstallRequired
#
# Description:
#    To check if installation is required
#
# Parameters:
#    (none)
#
# Module globals used:
#    (none)
#
# Globals used:
#    G_MODE
#
IsInstallRequired() {

    if [[ $G_MODE = "install" ]]; then
       return 0
    fi

    return 1
}

#
# IsUninstallRequired
#
# Description:
#    To check if removal of the software is required
#
# Parameters:
#    (none)
#
# Module globals used:
#    (none)
#
# Globals used:
#    G_MODE
#
IsUninstallRequired() {

    if [[ $G_MODE = "uninstall" ]]; then
       return 0
    fi

    return 1
}

#
# IsPreserveRequired
#
# Description:
#    To check if preservation of configuration data is requested
#
# Parameters:
#    (none)
#
# Module globals used:
#    (none)
#
# Globals used:
#    G_MODE
#
IsPreserveRequired() {

    if [[ $G_MODE = "preserve" ]]; then
       return 0
    fi

    return 1
}

#
# AddPostInitMessage()
#
# Description:
#    To add a message which will be displayed in the master task as a post-init
#    message before starting the install/upgrade procedure
#
# Parameters:
#    $1 - message
#
# Module globals used:
#    (none)
#
# Globals used:
#    G_MESSAGE_FILE
#
AddPostInitMessage() {

    echo $1 >> ${G_MESSAGE_FILE} 2> /dev/null
    return 0
}

# function Error()
#
# Description:
#    to print an error message and exit
#
# Parameters:
#    $* : message string
#
# Module globals used:
#    _MODULE_NAME

Error() {
  print -u2 "${_MODULE_NAME#[A-Z][0-9][0-9]}: error, $*"
  exit 2
}

#
# function Note()
#
# Description:
#    to print a note
#
# Parameters:
#    $* : message string
#
# Module globals used:
#    _MODULE_NAME

Note() {
  print -u2 "${_MODULE_NAME#[A-Z][0-9][0-9]}: note, $*"
  return 0
}

