#!/bin/ksh
#
# ident "@(#)adminGUI.ksh	1.3 02/08/20 SMI"
#
# Copyright (c) 2002 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# The common routines for the M31AdminGUI<locale> modules.
# This script should be sourced by the M31AdminGUI* moduels only.
#

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

#
# AdminGUI_Preinstall()
#
# Description:
#    Pre-install procedure
#
# Parameters:
#    (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_UT_VERSION

AdminGUI_Preinstall() {

   _DO_INSTALL="no"

# check to see if the localized Admin GUI package is available on the CD.
   if [[ -d ${UT_L10N_DIR}/${L10N_GUI} ]]; then
	_INSTALL_GUI="yes"
   fi

# check to see if the localized doc package is available on the CD.
   if [[ -d ${UT_DOC_DIR}/${SW_LOCALE_SYM}/${L10N_DOC} ]]; then
	_INSTALL_DOC="yes"
   fi

#
# if both L10N doc and GUI are not available, there is nothing to install.
# Just remove the old packages.
#
   if [[ ${_INSTALL_GUI} = "no" && ${_INSTALL_DOC} = "no" ]]; then
	if [[ $_SW_INSTALLED = "yes" || $_DOC_INSTALLED = "yes" ]]; then
	    AddPostInitMessage \
		"Remove\t [ $G_UT_PROD_NAME ${G_SR_CURRENT_VERSION} $SW_LOCALE Admin GUI and Documentation ]"
	    _DO_REMOVE="yes"
	    fmt <<-!

		An installation of the $G_UT_PROD_NAME $G_SR_CURRENT_VERSION
		$SW_LOCALE Admin GUI support and/or documentation has been
		detetected on this server.  Since the $G_SR_CURRENT_VERSION
		$SW_LOCALE Admin GUI and documentation are not currently
		available, the $G_SR_CURRENT_VERSION $SW_LOCALE Admin GUI and
		documentation will be removed for now.  You can reinstall them
		in the future when they become available.

		!
	fi
	AddPostInitMessage \
	    "Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE Admin GUI and Documentation are not available ]"
	return 0
    fi


#
# got this far because L10N GUI and/or docs are available on the CD.
#
# NOTE: we overloaded the _SW_INSTALLED variable.  When set to "yes", it can
# mean either the SRSS is installed (for 1.x cases) or the locale package 
# is installed (for 2.x cases).  When set to "no", it means the locale package
# is not installed.
#
   if [[ $_SW_INSTALLED = "no" ]]; then
      if [[ -z $G_SR_CURRENT_VERSION ]]; then
	#
	# this is a new install, we will only prompt the user.
	#
	if [[ $_INSTALL_GUI = "yes" ]]; then
	    print ""
	    if YesOrNo \
	    "Do you want to install $G_UT_PROD_NAME $SW_LOCALE Admin GUI" \
	    "N"; then
	        _DO_INSTALL="yes"
                AddPostInitMessage \
                "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE Admin GUI ]"
	        if [[ $_INSTALL_DOC = "yes" ]]; then
            	    AddPostInitMessage \
            	    "Install\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE documentation ]"
	        else
            	    AddPostInitMessage \
            	    "Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE documentation not available ]"
	        fi
	    fi
	fi
      else
	#
	# this is not a new install. This is an upgrade without the Admin GUI
	# support package installed.  We'll just remove the doc if it's
	# currently installed.
	# This can only happen when someone installed the docs manually in
	# 2.x.
	#
	if [[ $_DOC_INSTALLED = "yes" && $_DO_INSTALL = "no" ]]; then
	    _DO_REMOVE="yes"
	    AddPostInitMessage \
		"Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION $SW_LOCALE documentation ]"
	    fmt <<-!

		The $G_UT_PROD_NAME $G_SR_CURRENT_VERSION $SW_LOCALE
		documentation is currently installed on this server.  This
		document is now installed automatically when you install
		the $SW_LOCALE Admin GUI.  Since the $G_SR_CURRENT_VERSION
		$SW_LOCALE Admin GUI support is not currently installed on
		this system, the $G_UT_VERSION $SW_LOCALE Admin GUI will not
		be installed on this system and the $G_SR_CURRENT_VERSION
		$SW_LOCALE will be removed from this system.  If you wish to
		install the documentation only, please refer to the
		Installation documentation.

		!
	fi
      fi
   else

#
# if SRSS software is installed (meaning upgrade), we'll do the following:
#   - upgrading from 1.x (_SW_COMPATIBLE=="no"), install GUI and doc.
#   - upgrading from 2.0 (_SW_COMPATIBLE=="yes"), install GUI and doc.
#
	_DO_REMOVE="yes"

	if [[ $_INSTALL_GUI = "yes" ]]; then
	    _DO_INSTALL="yes"
	    AddPostInitMessage \
                "Upgrade\t [ $G_UT_PROD_NAME $SW_LOCALE Admin GUI $G_SR_CURRENT_VERSION to ${G_UT_VERSION} ]"
	else
	    AddPostInitMessage \
		"Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE Admin GUI support not available ]"
	fi

	if [[ $_INSTALL_DOC = "yes" ]]; then
	    if [[ $_DO_INSTALL = "yes" ]]; then
            	AddPostInitMessage \
            	    "Upgrade\t [ $G_UT_PROD_NAME $SW_LOCALE documentation $G_SR_CURRENT_VERSION to $G_UT_VERSION ]"
	    else
            	AddPostInitMessage \
		    "Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION $SW_LOCALE documentation ]"
	    	fmt <<-!

		The $G_UT_PROD_NAME $G_SR_CURRENT_VERSION $SW_LOCALE
		documentation is currently installed on this server.  This
		document is now installed automatically when you install
		the $SW_LOCALE Admin GUI.  Since the $SW_LOCALE Admin GUI
		support is not currently available, the $G_UT_VERSION
		$SW_LOCALE Admin GUI will not be installed on this system and
		the $G_SR_CURRENT_VERSION $SW_LOCALE documentation will be
		removed from this system.  If you wish to install the
		documentation only, please refer to the Installation 
		documentation.

		!
	    fi
	else
            AddPostInitMessage \
            	"Skip\t [ $G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE documentation not available ]"
	fi
   fi

   return 0
}

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

AdminGUI_Preremove() {

   if AnyPackageInstalled $UT_ADMINGUI_LIST $UT_DEPRECATED_ADMGUI_PKG; then
      _DO_REMOVE="yes"
      AddPostInitMessage \
             "Remove\t [ $G_UT_PROD_NAME $G_SR_CURRENT_VERSION $SW_LOCALE Admin GUI and documentation ]"
   fi
}

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

Module_Init() {

   return_code=0
   _DO_INSTALL="no"
   _DO_REMOVE="no"

   if [[ -n $G_SR_CURRENT_VERSION ]] && [[ $G_SR_CURRENT_VERSION < "2.0" ]]; then
#
# special handling for 1.x upgrade.  We mark the package as installed but
# incompatible.
#
      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="no"

   elif AnyPackageInstalled $L10N_GUI || \
	ProductPartiallyInstalled $L10N_GUI; then
#
# Check if the localized Admin GUI is already installed.
#
      _SW_COMPATIBLE="yes"
      _SW_INSTALLED="yes"

   elif AnyPackageInstalled $UT_DEPRECATED_ADMGUI_PKG || \
	ProductPartiallyInstalled $UT_DEPRECATED_ADMGUI_PKG; then
#
# Check if the deprecated localized Admin GUI packages is already installed.
#
      _SW_COMPATIBLE="yes"
      _SW_INSTALLED="yes"
      _SW_DEPRECATED_PKG="yes"
   fi

   if AnyPackageInstalled $L10N_DOC || \
	ProductPartiallyInstalled $L10N_DOC; then

      _DOC_INSTALLED="yes"
   fi

   if IsInstallRequired; then
	AdminGUI_Preinstall
	return_code=$?
   elif IsUninstallRequired; then
	AdminGUI_Preremove
	return_code=$?
   fi
   return return_code
}

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

Module_Preserve() {

   return 0

}

#
# Module_Remove()
#
# Description:
#    Remove Japanese 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 $SW_LOCALE Admin GUI and 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
   Module_Specific_Remove
   if [[ $_SW_DEPRECATED_PKG = "yes" ]]; then
        #
        # this is needed for upgrades between 2.0 builds
        #
        RemoveProduct $G_ADMIN_FILE $UT_DEPRECATED_ADMINGUI_PKG $L10N_DOC
   else  
	RemoveProduct $G_ADMIN_FILE $UT_ADMINGUI_LIST
   fi


#
# 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 Japanese docs
#
# Parameters:
#   (none)
#
# Globals used:
#    G_DEBUG
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE

Module_Install() {

  vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
     vflag="-v"
  fi
  typeset INSTALL_PKG=""
  typeset MESSAGE=""
  if [[ $_INSTALL_DOC = "no" ]]; then
     # this means that we install Admin GUI only
     MESSAGE="Admin GUI"
     INSTALL_PKG=$L10N_GUI
  elif [[ $_INSTALL_GUI = "no" ]]; then
     # this means that we install the documentation only
     MESSAGE="Documentation"
     INSTALL_PKG=$L10N_DOC
  else
     # this means that we install both the Admin GUI and the doc
     MESSAGE="Admin GUI and Documentation"
     INSTALL_PKG=$UT_ADMINGUI_LIST
  fi
  InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION $SW_LOCALE $MESSAGE"

  # cleanup old instances
  if [[ $_SW_DEPRECATED_PKG = "yes" ]]; then
        #
        # this is needed for upgrades between 2.0 builds
        #
        RemoveProduct $G_ADMIN_FILE $UT_DEPRECATED_ADMINGUI_PKG $L10N_DOC
  else  
	RemoveProduct $G_ADMIN_FILE $UT_ADMINGUI_LIST
  fi

  if [[ $? != 0 ]]; then
	Error "failed to completely remove $G_UT_PROD_NAME $SW_LOCALE Admin GUI and Documentation"
  fi

  Module_Specific_Install
  if [[ $_INSTALL_DOC = "yes" ]]; then
     pkgadd $vflag -a $G_ADMIN_FILE -d ${UT_DOC_DIR}/$SW_LOCALE_SYM $L10N_DOC
  fi
  if [[ $_INSTALL_GUI = "yes" ]]; then
     pkgadd $vflag -a $G_ADMIN_FILE -d ${UT_L10N_DIR} $L10N_GUI
  fi

#
# check that packages have been installed correctly
#
  if ! ProductInstalled $INSTALL_PKG || \
       ProductPartiallyInstalled $INSTALL_PKG; then
     print -u2 \
             "$G_UT_PROD_NAME $G_UT_VERSION $SW_LOCALE $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

}
