#!/bin/ksh -p
#
# ident "@(#)M11SRDS.ksh	1.10 02/10/10 SMI"
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# MODULE NAME: SRDS
# AUTHOR     :
# DESCRIPTION: To install Sun Ray Data Store
#
# 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
#

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

SRDSVersion() {
  SRDS_INSTALLED_VER="$(pkgparam SUNWutdso SUNW_PRODVERS 2>&-)"
  #
  # only release number portion of VERSION is used,
  # i.e. "1.0" of "1.0_04,REV=2002.09.25.12.40"
  #
  # the var is used throughout this module, not local to this function

  case "$SRDS_INSTALLED_VER" in
    $1) return 0;;
    *)  return 1;;
  esac
}


# PreserveSunRayDSData
#
# Description:
#    Check to see if we need to preserver/restore the SunDS data.
#    This is done by checking the existance of the following files:
#	/etc/opt/SUNWut/utadmin.conf file and
#	/etc/opt/SUNWut/srds/current/utdsd.run file
#
PreserveSunRayDSData() {

   if [[ -f ${UT_ETC_DIR}/utadmin.conf ]] && \
	[[ -f ${SRDS_ETC_DIR}/current/utdsd.run ]]; then
        return 0
   fi
   return 1
}


#
# InstallSRDS()
#
# Description:
#    Installation of SRDS product
#
# Parameters:
#    (none)
#
# Globals used:
#    G_DEBUG
#    G_PROGRAM_ID
#    G_ADMIN_FILE

InstallSRDS() {
   typeset vflag=""

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

   InstallMsg "$SRDS_PROD_NAME" "$SRDS_VERSION"

   pkgadd $vflag -a $G_ADMIN_FILE -d ${SRDS_DIR}/Packages $SRDS_PKG_LIST

   return $?
}


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

SRDS_Preinstall() {

   _DO_REMOVE="no"
   if [[ $_SW_INSTALLED = "yes" ]]; then
      ProductMsg -n "$SRDS_PROD_NAME $SRDS_INSTALLED_VER"
      if [[ $_SW_COMPATIBLE = "no" ]]; then
         _DO_REMOVE="yes"
         print "installed - upgrade required"
      else
	 print "installed"
      fi

   elif [[ $_SW_INSTALLED = "no" ]]; then
      ProductMsg -n "$SRDS_PROD_NAME $SRDS_VERSION"
      print "not installed"
   fi
   _DO_INSTALL="yes"

   if IsDataPreserved || [[ $_SW_INSTALLED = "yes" ]]; then
      if PreserveSunRayDSData; then
	 if [[ -z $SRDS_INSTALLED_VER ]]; then
	    AddPostInitMessage \
      		"Upgrade\t [ $SRDS_PROD_NAME $SRDS_VERSION  ]"
	 elif [[ $SRDS_INSTALLED_VER = $SRDS_VERSION ]]; then
	    AddPostInitMessage \
      		"Upgrade\t [ data for $SRDS_PROD_NAME $SRDS_VERSION ]"
	 else
	    AddPostInitMessage \
	    "Migrate\t [ $SRDS_PROD_NAME $SRDS_INSTALLED_VER to $SRDS_VERSION ]"
	 fi
      fi
   else
      AddPostInitMessage "Install\t [ $SRDS_PROD_NAME $SRDS_VERSION ]"
   fi

   return 0
}

#
# SRDS_Preremove()
#
# Description:
#    Pre-remove procedure: ask whether Sun Directory Services has to be removed
#
# Parameters:
#    (none)
#
# Globals used:
#    (none)

SRDS_Preremove() {

   _DO_REMOVE="no"

   if [[ $_SW_INSTALLED = "yes" && $_SW_COMPATIBLE = "yes" ]]; then
      _DO_REMOVE="yes"
      AddPostInitMessage "Remove\t [ $SRDS_PROD_NAME $SRDS_VERSION ]"
   else 
      Note "$SRDS_PROD_NAME is not installed, hence no need to remove it."
      return 1
   fi

   return 0
}

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

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

Module_Init() {

   exit_no=0

#
# Check if SRDS is already installed. If so, check if that version is 
# compatible with Sunray
#
   if ProductInstalled $SRDS_PKG_LIST && SRDSVersion $SRDS_VERSION && \
      ! ProductPartiallyInstalled $SRDS_PKG_LIST; then

      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="yes"

   elif ProductInstalled $SRDS_PKG_LIST && ! SRDSVersion $SRDS_VERSION; then

      _SW_INSTALLED="yes"
      _SW_COMPATIBLE="no"

   fi

   if IsInstallRequired; then

      SRDS_Preinstall
      exit_no=$?

   elif IsUninstallRequired; then

      SRDS_Preremove
      exit_no=$?

   elif IsPreserveRequired; then
      if [[ ${_SW_INSTALLED} = "yes" ]]; then
         AddPostInitMessage "Preserve [ data for Sun Ray Data Store ]"
      fi

   fi

   return $exit_no
}

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

Module_Preserve() {

#
# check if SRDS is configured
#
   if ! PreserveSunRayDSData; then
      return 0
   fi

#
# Save files
#
  SaveFiles $SRDS_PRESERVE_FILES
  if [[ $? != "0" ]]; then
     return 2
  fi

  SaveDirectory SRDS_DIRS $SRDS_PRESERVE_DIRS
  if [[ $? != "0" ]]; then
     return 2
  fi
 
   return 0

}

#
# Module_Remove()
#
# Description:
#    Remove SRDS
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Remove() {

   RemoveMsg "$SRDS_PROD_NAME" "$SRDS_VERSION"
   RemoveProduct $G_ADMIN_FILE $SRDS_PKG_LIST

   return $?
}

#
# Module_Install()
#
# Description:
#    To install Sun Directory Service
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Install() {

   if [[ $_SW_COMPATIBLE = "yes" ]]; then
      # no need to install if the software is compatible
      return 0
   fi

   InstallSRDS

   if ! ProductInstalled $SRDS_PKG_LIST || \
           ProductPartiallyInstalled $SRDS_PKG_LIST; then
      Error "$SRDS_PROD_NAME not successfully installed"
   fi

   return 0
}

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

Module_Restore() {

# restore the SRDS_DIRS
   echo "Restoring SRDS configuration/data."
   RestoreDirectory SRDS_DIRS
   if [[ $? != 0 ]]; then
      return 2
   fi

   # Restore preserved files (Overwriting existing files).

   for file in $SRDS_PRESERVE_FILES; do
      if GetPreservedFilePath "$file"; then
	preservedfile=${_RETURN_VAL}
	cp -p "$preservedfile" "$file"
      fi
   done

# Perform datastore migration using the old `utconfig -s`
   ${UT_DIR}/sbin/utconfig -s silent > $UTCONFIG_LOG 2>&1
   if [[ $? != "0" ]]; then
      return 2
   fi

   echo "Restoration of SRDS data complete"
   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="no"

#
# BEGIN: Developers module variables definition here
#

DeclareModuleVar SRDS_INSTALLED_VER

export SRDS_PROD_NAME="Sun Ray Data Store"
export SRDS_VERSION="1.0"
export SRDS_DIR="${G_PRODUCT_DIR}/Sun_Ray_Data_Store_1.0/Solaris_8+"
export SRDS_PKG_LIST="SUNWutdso SUNWutdsr"

export UT_DIR="/opt/SUNWut"
export UT_VAR_DIR="/var/opt/SUNWut"
export UT_ETC_DIR="/etc/opt/SUNWut"
export SRDS_ETC_DIR="/etc/opt/SUNWut/srds"
export SRDS_VAR_DIR="/var/opt/SUNWut/srds"

export SRDS_PRESERVE_DIRS="${SRDS_ETC_DIR}/current \
                          ${SRDS_VAR_DIR}/dbm.ut \
                          ${SRDS_VAR_DIR}/replog/utpushd.replog \
                          ${SRDS_VAR_DIR}/replog/utpulld.replog \
                          ${SRDS_VAR_DIR}/replog/utpushd.status \
                          ${SRDS_VAR_DIR}/replog/utpulld.status \
                          ${SRDS_VAR_DIR}/replog/*.rej \
                          ${SRDS_VAR_DIR}/log/utdsd.replog \
                         "

export SRDS_PRESERVE_FILES="/etc/opt/SUNWut/utadmin.pw \
                           ${UT_ETC_DIR}/utadmin.conf \
                           ${UT_VAR_DIR}/log/admin_log \
                          "
#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
