#!/bin/ksh -p
#
# ident "@(#)M30EngDocs.ksh	1.32 05/08/10 SMI"
#
# Copyright 2001-2002,2004,2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: EngDocs
# AUTHOR     :
# DESCRIPTION: To install Sunray English documentation
#
# The following exported variables (initialized by the master task) are
# available (read-only) for the module:
#
# G_PROGRAM_ID    : program name
# G_MEDIA_DIR     : path of the install directory on the media (CD,...)
# G_PRODUCT_DIR   : path of the bundles directory on the media (CD,...)
# G_ADMIN_FILE    : path 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
# G_SR_CURRENT_VERSION: the currently installed SR version
#

#
# Module developers to provide the following functions:
# Module_Init, Module_Preserve, Module_Remove, Module_Install, Module_Restore
# Module_Abort, Module_Exit
#

#
# EngDocsVersion()
#
# Description:
#    Check if version number of the Eng.docs currently installed matches the
#    version number required.
#
# Parameters:
#    $1 - version number
#
# Globals used:
#    (none)

EngDocsVersion() {

#
# take only the version number (i.e. 1.2 or 1.3) without the build number
# (usually, the format is 1.2_16.a,REV=xxxxx).
#
   case "$G_SR_CURRENT_VERSION" in
      $1) return 0;;
      *)  return 1;;
   esac
}

#
# EngDoc_Preinstall()
#
# Description:
#    Pre-install procedure
#
# Parameters:
#    (none)
#

EngDoc_Preinstall() {
   typeset O
   typeset E
   typeset NVED="$G_UT_PROD_NAME $G_UT_VERSION English documentation"
   typeset OVED="$G_UT_PROD_NAME $G_SR_CURRENT_VERSION English documentation"

   case "$G_INSTALL_OS" in
   SunOS)
     O="-d"
     E=""
     ;;

   Linux)
     O="-r"
     E="-$G_UT_VERSION-?*.i386.rpm"
     ;;
   esac

   # check to see if the english doc package is available on the CD.
   #
   if test $O $UT_DOC_DIR/SUNWeutdo$E; then
      _DO_INSTALL="yes"
      _INSTALL_DOC="yes"
   fi

   # check to see if the manpage package is available on the CD.
   #
   if test $OPT $UT_DOC_DIR/SUNWutm$EXT; then
      _DO_INSTALL="yes"
      _INSTALL_MANPAGE="yes"
   fi

   # starting in 2.0, the doc for the locale that the Admin GUI supports is
   # automatically installed.  Since we will always install the english version
   # of the Admin GUI, the english doc will always be installed as well.
   #
   if [[ $_SW_INSTALLED = "no" ]]; then
      if [[ $_INSTALL_DOC = "no" ]]; then
      	 AddPostInitMessage "Skip\t [ $NVED not available ]"
      else 
         AddPostInitMessage "Install\t [ $NVED ]"
      fi
   else
      _DO_REMOVE="yes"
      if [[ $_INSTALL_DOC = "no" ]]; then
      	 AddPostInitMessage "Remove\t [ $OVED ]"
      	 AddPostInitMessage "Skip\t [ $NVED ]"
      else
         AddPostInitMessage "Upgrade\t [ $OVED to $G_UT_VERSION ]"
      fi
   fi

   return 0
}

#
# EngDoc_Preremove()
#
# Description:
#    Pre-remove procedure
#
# Parameters:
#    (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_UT_VERSION

EngDoc_Preremove() {
   typeset PNCV="$G_UT_PROD_NAME $G_SR_CURRENT_VERSION"

   if AnyPackageInstalled $UT_PKG_LIST ; then
      _DO_REMOVE="yes"
      AddPostInitMessage "Remove\t [ $PNCV English documentation ]"
   fi
}

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

Module_Init() {

   return_code=0

#
# Check if the English documentation is already installed (and, of course,
# which version of it)
#
   if ProductInstalled $UT_PKG_LIST && EngDocsVersion $G_UT_VERSION && \
      ! ProductPartiallyInstalled $UT_PKG_LIST; then

      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="yes"

   elif ProductInstalled $UT_PKG_LIST && ! EngDocsVersion $G_UT_VERSION; then

      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="no"

   fi

   if IsInstallRequired; then

      EngDoc_Preinstall
      return_code=$?

   elif IsUninstallRequired; then

      EngDoc_Preremove
      return_code=$?

   fi

   return $return_code
}

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

Module_Preserve() {

   return 0

}

#
# Module_Remove()
#
# Description:
#    Remove English documentation
#    Note that this function can be used to uninstall older version of
#    the product (up to now the package list is the same).
#
# Parameters:
#   (none)
#
# Globals used:
#   G_UT_PROD_NAME
#   G_ADMIN_FILE

Module_Remove() {
   RemoveMsg "$G_UT_PROD_NAME" "${G_SR_CURRENT_VERSION} English Documentation"

   # remove the docdir links.  This is to support the older SR releases.
   # In 2.0, the docdir link is installed as part of the package.  So, no
   # need to removing it manually in the future.
   #
   if [[ -L ${UT_LIB_LOCALE_DIR}/docdir ]]; then
      rm ${UT_LIB_LOCALE_DIR}/docdir
   fi

   RemoveProduct $G_ADMIN_FILE $UT_PKG_LIST

   # if the removal didn't succeed, we exit with 1 so to keep the procedure
   # going...
   #
   if [[ $? != 0 ]]; then
      return 1
   fi

   return 0
}

#
# Module_Install()
#
# Description:
#    To install English docs
#
# Parameters:
#   (none)
#
# Globals used:
#    G_DEBUG
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE

Module_Install() {
  typeset vflag=""
  typeset PKGS=""
  typeset MESSAGE=""

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

  if [[ $_INSTALL_DOC = "no" ]]; then
     # this means that we install manpage only
     MESSAGE="manpages"
     PKGS="$MAN_PKG"
  elif [[ $_INSTALL_MANPAGE = "no" ]]; then
     # this means that we install the doc only
     MESSAGE="English Documentation"
     PKGS="$DOC_PKG"
  else
     # this means that we install both the manpage and the doc
     MESSAGE="English manpage and Documentation"
     PKGS="$DOC_PKG $MAN_PKG"
  fi
  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION $MESSAGE"

  # cleanup old instances
  #
  if ! RemoveProduct $G_ADMIN_FILE $UT_PKG_LIST; then
    Error "failed to completely remove $G_UT_PROD_NAME English Documentation"
  fi

  if ! InstallProduct $G_ADMIN_FILE $UT_DOC_DIR $PKGS; then
     print -u2 "$G_UT_PROD_NAME $MESSAGE not successfully installed"
     return 2
  fi

  return 0
}

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

Module_Restore() {

   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/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 _INSTALL_DOC="no"
DeclareModuleVar _INSTALL_MANPAGE="no"

#
# BEGIN: Developers module variables definition here
#

case "$G_OS" in
SunOS)
  case "$G_ISA" in
    sparc) UT_DOC_DIR=$G_PRODUCT_DIR/Docs/Solaris_8+/$G_ISA/Packages;;
    i386)  UT_DOC_DIR=$G_PRODUCT_DIR/Docs/Solaris_10+/$G_ISA/Packages;;
  esac
  ;;
Linux)
  UT_DOC_DIR=$(GetPackagePath "Docs")
  ;;
esac
export UT_DOC_DIR

RPM_EXT="-3.1-?*.i386.rpm"

DOC_PKG="SUNWeutdo"
MAN_PKG="SUNWutm"

UT_PKG_LIST="$DOC_PKG $MAN_PKG"

case "$G_OS" in
Linux)
  DOC_PKG="$DOC_PKG$RPM_EXT"
  MAN_PKG="$MAN_PKG$RPM_EXT"
  ;;
esac

export UT_LIB_LOCALE_DIR="/opt/SUNWut/lib/locale"

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
