#!/bin/ksh -p
#
# ident "@(#)M11SRDS.ksh	1.21 04/09/01 SMI"
#
# Copyright 2001-2002,2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# 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:
  #    G_MEDIA_DIR

  SRDS_INSTALLED_VER=$($G_MEDIA_DIR/utprodinfo -p SUNWutdso 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

  if CompareVersion "$SRDS_INSTALLED_VER" $1; then
     # matches
     return 0
  fi
  return 1
}


# 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
}


#
# 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"
      # need to check for compatibility
      if [[ $_SW_COMPATIBLE = "no" ]] ||
      	 ProductPartiallyInstalled $SRDS_PKG_LIST; then
         _DO_REMOVE="yes"
	 _SW_COMPATIBLE="no"
         print "installed - upgrade required"
      else
	 print "installed"
      fi

   else
      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
      else
	 AddPostInitMessage \
      	   "Upgrade\t [ $SRDS_PROD_NAME $SRDS_VERSION ]"
      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" ]]; then
      _DO_REMOVE="yes"
      AddPostInitMessage "Remove\t [ $SRDS_PROD_NAME $SRDS_INSTALLED_VER ]"
   else 
      Note "$SRDS_PROD_NAME is not installed."
      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() {

   typeset exit_no=0
   if ProductInstalled $SRDS_PKG_LIST && SRDSVersion $SRDS_VERSION; 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_INSTALLED_VER"
   RemoveProduct $G_ADMIN_FILE $SRDS_PKG_LIST

   return $?
}

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

   typeset PKGS=""

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

   case "$G_OS" in
      SunOS) PKGS="$SRDS_PKG_LIST";;
      Linux) PKGS="$SRDS_RPM_LIST";;
    esac

   InstallMsg "$SRDS_PROD_NAME" "$SRDS_VERSION"

   if ! InstallProduct $G_ADMIN_FILE $SRDS_DIR $PKGS; then
      print -u2 "$SRDS_PROD_NAME not successfully installed"
      return 1
   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 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
. ${G_MEDIA_DIR}/support_lib/util_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="2.1"

export SRDS_DIR=$(GetPackagePath Sun_Ray_Data_Store_${SRDS_VERSION})

export SRDS_PKG_LIST="SUNWutdso SUNWutdsr"
EXT="2.1-?*.i386.rpm"
export SRDS_RPM_LIST="SUNWutdso-$EXT SUNWutdsr-$EXT"

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 \
                          "
#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
