#!/bin/ksh -p
#
# ident "@(#)M80SRVC.ksh	1.2 11/02/08 Oracle"
#
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
#

#
# MODULE NAME: SRVC
# AUTHOR     : Shefali Bhargava
# DESCRIPTION: Used for installing / uninstalling Sun Ray module for SRVC
#
# 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 SRVC_Check_Installed
#
# Description:
#   check whether SRVC is installed
#
# Parameters:
#   (none)
#
#
function SRVC_Check_Installed {

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

     if $G_MEDIA_DIR/utprodinfo -t installed SUNWkio-vdm; then
             SRVC_INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p \
		     SUNWkio-vdm 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"

  if [[ "$G_OS" = "Linux" ]]; then
      return 0
  fi

  SRVC_Check_Installed

  if IsInstallRequired ; then

      if isTrustedSolaris ; then
	  AddPostInitMessage \
	      "Skip\t [ $SRVC_PROD_NAME $SRVC_VERSION is not supported with Solaris Trusted Extensions ]"
	  return 0
      fi

       _DO_INSTALL="yes"
       if [[ $_SW_INSTALLED = "yes" ]]; then
           _DO_REMOVE="yes"
	   AddPostInitMessage \
	   "Upgrade\t [ $SRVC_PROD_NAME $SRVC_INSTALLED_VERSION to $SRVC_VERSION ]"
       else
	   AddPostInitMessage "Install\t [ $SRVC_PROD_NAME $SRVC_VERSION ]"
       fi
  elif IsUninstallRequired && [[ ${_SW_INSTALLED} = "yes" ]]; then
       _DO_REMOVE="yes"
       AddPostInitMessage \
	"Remove\t [ $SRVC_PROD_NAME $SRVC_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 {

  if [[ "$G_OS" = "Linux" ]]; then
      return 0
  fi

   RemoveMsg "$SRVC_PROD_NAME" "$SRVC_INSTALLED_VERSION"

   RemoveProduct $G_ADMIN_FILE $SRVC_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 {

  if [[ "$G_OS" = "Linux" ]]; then
      return 0
  fi

  vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
      vflag="-v"
  fi

  InstallMsg "$SRVC_PROD_NAME" "$SRVC_VERSION"

  if ! InstallProduct $G_ADMIN_FILE $SRVC_DIR $SRVC_PKG_LIST; then
     print -u2 \
             "$SRVC_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 SRVC_PROD_NAME="VMware View Connector"
typeset -r SRVC_DIR=$(GetPackagePath VMware_View_Connector_1.3)
typeset -r SRVC_VERSION="1.3"

#
# BEGIN: Developers module variables definition here
#

typeset -r SRVC_PKG_LIST="SUNWkio-vdm"
typeset -r SRVC_PKG_RLIST=$SRVC_PKG_LIST


#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
