#!/bin/ksh -p
#
# ident "@(#)M60SVT.ksh	1.1 09/08/27 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: UtSVT
# AUTHOR     : Bob Doolittle
# DESCRIPTION: Used for installing / uninstalling Sun Ray module for Service Tags
#
# 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
#

#
# function SVT_Check_Installed
#
# Description:
#   check whether Sservice Tags is installed
#
# Parameters:
#   (none)
#
#
function SVT_Check_Installed {

  if AnyPackageInstalled $SVT_PKG_RLIST; then
     _SW_INSTALLED="yes"
     _SW_COMPATIBLE="yes"

     if $G_MEDIA_DIR/utprodinfo -t installed SUNWutsvt; then
             SVT_INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p \
		     SUNWutsvt VERSION | cut -d_ -f1)
     fi
  fi
}

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

function Module_Init {

  _SW_INSTALLED="no"

  SVT_Check_Installed

  if IsInstallRequired ; then
       _DO_INSTALL="yes"
       if [[ $_SW_INSTALLED = "yes" ]]; then
           _DO_REMOVE="yes"
	   AddPostInitMessage \
	   "Upgrade\t [ $SVT_PROD_NAME $SVT_INSTALLED_VERSION to $SVT_VERSION ]"
       else
	   AddPostInitMessage "Install\t [ $SVT_PROD_NAME $SVT_VERSION ]"
       fi
  elif IsUninstallRequired && [[ ${_SW_INSTALLED} = "yes" ]]; then
       _DO_REMOVE="yes"
       AddPostInitMessage \
	"Remove\t [ $SVT_PROD_NAME $SVT_INSTALLED_VERSION ]"
  fi

   return 0

}

#
# function Module_Preserve
#
# Description:
#   If installed build is earlier than 1.3_10.b do not preserve data.
#   The config file is not necessary since libutsmon will use
#   a dynamic version if a real one does not exist.
#
# Parameters:
#   (none)
#
# Globals used:
#

function Module_Preserve {

  return 0

}

#
# function Module_Remove
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE
#

function Module_Remove {

   RemoveMsg "$SVT_PROD_NAME" "$SVT_INSTALLED_VERSION"

   RemoveProduct $G_ADMIN_FILE $SVT_PKG_RLIST

#
# 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:
#
# 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 "$SVT_PROD_NAME" "$SVT_VERSION"

  if ! InstallProduct $G_ADMIN_FILE $SVT_DIR $SVT_PKG_LIST; then
     print -u2 \
             "$SVT_PROD_NAME 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"

typeset -r SVT_PROD_NAME="Service Tags"
typeset -r SVT_DIR=$(GetPackagePath Service_Tags_1.1)
typeset -r SVT_VERSION="1.1"

#
# BEGIN: Developers module variables definition here
#

if [[ "$G_OS" = "Linux" ]]; then
	typeset -r SVT_PKG_LIST="SUNWutsvt-1.1-?*.i386.rpm"
	typeset -r SVT_PKG_RLIST="SUNWutsvt"
else
	typeset -r SVT_PKG_LIST="SUNWutsvt"
	typeset -r SVT_PKG_RLIST=$SVT_PKG_LIST
fi

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
