#!/bin/ksh -p
#
# ident "@(#)M00SunRay_first.ksh	1.52 05/05/26 SMI"
#
# Copyright 2001-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: Sunray_first
# AUTHOR     :
# DESCRIPTION: To provide general checking and preinstall/preremove procedure
#              for the Sunray product. it must be executed first
#
# 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
# G_SR_CURRENT_VERSION: the currently installed SunRay version
# G_MODE          : invocation mode (install, uninstall, preserve)
#

#
# StopDaemons()
#
# Description:
#   Stop Sunray daemon before installation/removal procedure
#
# Parameters:
#   (none)
#
# Globals used:
#   G_UT_PROD_NAME
#   G_DAEMON_LOC
#   G_SR_CURRENT_VERSION
#

StopDaemons() {
  typeset v=${G_SR_CURRENT_VERSION%%.*}
  typeset -r XMGR=/etc/opt/SUNWut/xmgr

  if $_DAEMONS_STOPPED; then
	# daemons already stopped
	return
  fi
  if $G_MEDIA_DIR/utprodinfo -t installed SUNWutr; then
    print "\nStopping Sun Ray services, please wait ..."
    [[ -x "${G_DAEMON_LOC}/utsvc" ]] && ${G_DAEMON_LOC}/utsvc stop \
      1>/dev/null 2>&1

    [[ -x "${G_DAEMON_LOC}/utstorage" ]] && ${G_DAEMON_LOC}/utstorage stop \
      1>/dev/null 2>&1

    # dhcp server daemon
    case "$G_OS" in
      SunOS) DHCPD=dhcp;;
      Linux) DHCPD=dhcpd;;
    esac
    [[ -x "${G_DAEMON_LOC}/${DHCPD}" ]] && ${G_DAEMON_LOC}/${DHCPD} stop \
	1>/dev/null 2>&1

    if [[ ${v} = "1" ]]; then
	# old SunDS daemon (SRSS 1.x)
	if grep -s "o=utdata" /etc/opt/SUNWconn/ldap/current/dsserv.conf > /dev/null; then
	    # only stop the sunds server if it's serving SunRay Data.
	    [[ -x "${G_DAEMON_LOC}/dsserv" ]] && ${G_DAEMON_LOC}/dsserv stop \
		1>/dev/null 2>&1
	fi
    else
	# stop SR datastore daemon (SRSS 2.x or later)
	[[ -x "${G_DAEMON_LOC}/utds" ]] && ${G_DAEMON_LOC}/utds stop \
	    1>/dev/null 2>&1
    fi

    [[ -x "${G_DAEMON_LOC}/utacleanup" ]] && ${G_DAEMON_LOC}/utacleanup \
      1>/dev/null 2>&1
    if [[ -x $XMGR/notify ]]; then
    	$XMGR/notify
    elif [[ -f /var/dt/Xpid ]]; then
    	kill -HUP "$(/bin/cat /var/dt/Xpid 2>&-)"
    fi
  fi
  _DAEMONS_STOPPED="true"
}


#
# RemoveOtherFiles()
#
# Description:
#   Checks to see if there is any other files/directories under the
#   SUNWut.upgrade directory.  If yes, then it will ask the user if
#   it can cleanup these files.  If the answer is no, the program
#   will return error status of 1.
#
# Parameters:
#   $1  the tarfile name to be excluded from the listing of other files.
#	if not passed, list all files under SUNWut.upgrade directory.
#
# Globals used:
#   G_OLD_PRESERVED_FILE
#   G_UPGRADE_DIR
#   G_PROGRAM_ID
#
RemoveOtherFiles() {

    typeset FILE="xxdummyfilexx"
    typeset -i other_files
    typeset -i match=0
    if [[ $# -eq 1 ]]; then
	FILE=$1
	match=1
    fi
    other_files=$(find ${G_UPGRADE_DIR}/* \
	! \( -name ${fname} -or -name ${G_OLD_PRESERVED_FILE} \) \
	-print -prune 2> /dev/null | wc -l)

    if [[ $other_files -gt $match ]]; then
	# if the backup directory contains other files/directories
	# other than the tarfile itself.  Abort the program.
	if [[ $match -ne 0 ]]; then
	    fmt <<- !

		In addition to the tarfile ${FILE}, we also found
		the following files/directories under the ${G_UPGRADE_DIR}
		directory.

		!
	else
	    fmt <<- !

		Found the following files/directories under the
		${G_UPGRADE_DIR} directory.

		!
	fi
	typeset -r fname=$(basename  ${FILE})
	find ${G_UPGRADE_DIR}/* ! -name ${fname} -print -prune
	print ""
	if YesOrNo "Do you want to remove these files/directories" "Y"
	then
	    find ${G_UPGRADE_DIR}/* ! -name ${fname} -print -prune | \
		xargs rm -rf
	else
	    fmt <<-!

		Please cleanup the directory ${G_UPGRADE_DIR} before
		rerunning $G_PROGRAM_ID.

		!
	    return 1
	fi
    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:
  #   Generic checking
  #
  # Parameters:
  #   (none)
  #
  # Globals used:
  #   G_MODE
  #   G_OLD_PRESERVED_FILE
  #

  if ! IsPreserveRequired; then
    if AnyPackageInstalled $CB_PACKAGE_LIST; then
      Error "Controlled Browser is installed on this server.\n" \
       "Execute '/opt/SUNWut/sbin/cbinstall -u' to remove the\n" \
       "Controlled Browser first.\n"
    fi
  fi  	# not preserve mode

  if IsDataPreserved; then
     #
     # IsDataPreserved only checks the existance of the tarfile.   It can
     # be either the compressed or uncompressed tar file under the
     #    /var/tmp/SUNWut.upgrade
     # directory.  Before we proceed, we need to check to make sure that
     # there isn't a preserve # directory under SUNWut.upgrade.  If yes,
     # we abort the program and tell the user to cleanup the directory.
     #
     GetPreservedTarVersion
     typeset -r PRESERVE_VERSION=${_RETURN_VAL}
     typeset -r PRESERVE_FILENAMES=${_RETURN_FILENAMES}

     if IsInstallRequired; then
	fmt <<-!

	Found ${PRESERVE_VERSION} preserved file "${PRESERVE_FILENAMES}"
	directory.

	!
	if YesOrNo "Do you want to restore from this file" "Y"
	then
            AddPostInitMessage \
	    "Restoring data from ${PRESERVE_VERSION} preserved files.\n" 
	    if ! RemoveOtherFiles ${PRESERVE_FILENAMES}; then
		return 2
	    fi
	    touch ${G_OLD_PRESERVED_FILE}
	    #
	    # now we need to check to see if the preserved version match the currently
	    # installed SunRay software.  If not, warn the user.
	    #
	    GetCurrentSRVersion
	    typeset -r curr=$_RETURN_VAL
	    if [[ -n "$curr" ]] && [[ "$curr" != "$PRESERVE_VERSION" ]]; then
		fmt <<- !

		WARNING: SunRay $curr is currently installed on this system.
		You are restoring from a preserved tarfile generated from a $PRESERVE_VERSION
		system.

		!
		if ! YesOrNo "Do you want to continue" "Y"
		then
		    return 2
		fi
	    fi
	else
	    fmt <<-!

		Please remove the existing preserved file
		${PRESERVE_FILENAMES} before rerunning $G_PROGRAM_ID.

		!
	    return 2
	fi
     elif IsPreserveRequired; then
	fmt <<-!

	Found ${PRESERVE_VERSION} preserved file "${PRESERVE_FILENAMES}"

	!
	if YesOrNo "Do you want to remove this existing preserved file" "Y"
	then
	    rm -r -f ${PRESERVE_FILENAMES}

	else
	    fmt <<-!

		Please move the existing preserved file under the directory
		${PRESERVE_FILENAMES} to other location before rerunning
		$G_PROGRAM_ID.

		!
	    return 2
	fi
	
     fi
  else
     #
     # no data preserved, but we need to make sure that there is no preserve directory
     # left over from the previous install.
     #
     if ! RemoveOtherFiles; then
	return 2
     fi
  fi


#
# we are actually not doing anything here but these variables must be set
# in order to carry out pre-install, pre-preserve stuff...
#
  if ProductInstalled SUNWuto; then
  	_SW_INSTALLED="yes"
  	_DO_REMOVE="yes"
  else
	 _DO_REMOVE="no"
  fi
  _SW_COMPATIBLE="yes"
  _DO_INSTALL="yes"

   return 0
}

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

Module_Preserve() {


   if [[ ${G_OS} = "Linux" ]] && ! ${G_MEDIA_DIR}/utprodinfo -p pax  >/dev/null 2>&1; then
      print "Error: Please install the pax rpm before upgrade"
      return 2
   fi

   if IsPreserveRequired; then
      StopDaemons
   elif ! OldPreservedExist && ! IsUninstallRequired && \
	[[ $_SW_INSTALLED = "yes" ]]; then
      # stop all daemons before preserving the data files
      StopDaemons
      echo "\nThe following files will be preserved:"
   fi
   return 0
}

#
# Module_Remove()
#
# Description:
#    Actual pre-remove stuff (stop Sunray daemons)
#
# Parameters:
#   (none)
#
# Globals used:
#   G_MEDIA_DIR

Module_Remove() {

   StopDaemons
   CompareVersion $G_SR_CURRENT_VERSION "3.1"
   if [ $? -ne 2 ]; then
	# 3.1 or later, we need to use utctl to disable the features before
	# uninstalling.  We will use the installed utctl rather than the one
	# from the CD.
	typeset BASE=`$G_MEDIA_DIR/utprodinfo -r SUNWuto`
	UTCTL=$BASE/SUNWut/lib/utctl
	if [ -x $UTCTL ]; then
   	    $UTCTL disable
	fi
   fi

   return 0
}

#
# Module_Install()
#
# Description:
#    Actual pre-install stuff (stop Sunray daemons)
#
# Parameters:
#   (none)
#
# Globals used:
#   (none)

Module_Install() {

  StopDaemons

  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
. ${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 _RETURN_FILENAMES=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 _DAEMONS_STOPPED="false"

#
# BEGIN: Developers module variables definition here
#

export CB_PACKAGE_LIST="SUNWbbnav SUNWutbb SUNWbbprt"

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
