#!/bin/ksh
#
# ident "@(#)M08GDM.ksh	1.6 04/10/26 SMI"
#
# Copyright 2001-2002,2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# MODULE NAME: GDM
# AUTHOR     :
# DESCRIPTION: To install the patched version of gdm 2.4.4.7 that is needed
#              for Sun Ray to work on Linux systems. There is simply 1 RPM
#              that must be installed to replace the one currently on the
#              system.
#
# 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
#

#
# 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 [[ $G_OS == "SunOS" ]]; then
		return $exit_no
	fi

	export OUR_GDM_NOT_INSTALLED OLD_GDM_INSTALLED INSTALLED_GDM
	OLD_GDM_INSTALLED=0
	INSTALLED_GDM=""

	# Check to see if our version of gdm is already installed
	rpm --quiet -q ${GDM_NAME}-${GDM_VERSION}-${GDM_RELEASE} > /dev/null 2>&1
	OUR_GDM_NOT_INSTALLED=$?

	if [[ ${OUR_GDM_NOT_INSTALLED} == 1 ]]; then
		# Check to see if some gdm or gdm2 is installed on the system
		OLD_GDM_NOT_INSTALLED=0
		OLD_GDM2_NOT_INSTALLED=0

		INSTALLED_GDM="$(rpm -q ${GDM_NAME} 2>&-)"
		OLD_GDM_NOT_INSTALLED=$?

		if [[ ${OLD_GDM_NOT_INSTALLED} == 1 ]]; then
			INSTALLED_GDM="$(rpm -q ${GDM_NAME}2 2>&-)"
			OLD_GDM2_NOT_INSTALLED=$?
		fi

		if [[ ${OLD_GDM_NOT_INSTALLED} == 0 ]] || \
			[[ ${OLD_GDM2_NOT_INSTALLED} == 0 ]]; then
			OLD_GDM_INSTALLED=1
		fi
	fi

	if IsInstallRequired; then
		ProductMsg -n "${GDM_NOMINAL} $GDM_VERSION"
		if [[ ${OUR_GDM_NOT_INSTALLED} == 1 ]]; then
			print -n "not "
		fi
		print "installed"

		# If an old gdm is present, ask user if ok to delete
		if [[ ${OUR_GDM_NOT_INSTALLED} == 1 ]] &&  \
			[[ ${OLD_GDM_INSTALLED} == 1 ]]; then
			print ""
			print "Your system currently has $INSTALLED_GDM installed."
			print "This rpm needs to be removed in order to install a new version of gdm."
			print "Before removing, make sure gdm is not managing any displays and is stopped."
			print ""

			if ! YesOrNo "Remove ${INSTALLED_GDM}?" "Y"; then
				# apparently 2 is the magic number to abort installation
				return 2
			else
				# We will backup a copy of locale.alias to use
				# instead of the one included with our RPM.
				LOCFILE=/etc/X11/gdm/locale.alias
				if [[ -f ${LOCFILE} ]]; then
					mv ${LOCFILE} ${LOCFILE}.sysorig
				fi

				print ""
				print "+++ ${INSTALLED_GDM}"
				rpm -e ${INSTALLED_GDM}
				print ""
			fi
		fi

		if [[ ${OUR_GDM_NOT_INSTALLED} == 1 ]]; then
			_DO_INSTALL="yes"
			AddPostInitMessage "Install\t [ ${GDM_NOMINAL} ${GDM_VERSION} ]"
		fi
	elif IsUninstallRequired; then
		if [[ ${OUR_GDM_NOT_INSTALLED} == 0 ]]; then
			print "You can leave ${GDM_NAME}-${GDM_VERSION}-${GDM_RELEASE} on your system"
			print "to manage the console or other displays if needed:"
			print ""
			if YesOrNo "Remove ${GDM_NAME}-${GDM_VERSION}-${GDM_RELEASE}?" "N"; then
				_DO_REMOVE="yes"
				AddPostInitMessage "Remove\t [ $GDM_NOMINAL ${GDM_VERSION} ]"
			fi
		fi
	fi
	return $exit_no
}

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

Module_Preserve() {
   return 0
}

#
# Module_Remove()
#
# Description:
#    To remove packages
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Remove() {

	RemoveMsg "${GDM_NOMINAL}" "$GDM_VERSION"
	RemoveProduct $G_ADMIN_FILE $GDM_PKG_LIST
	return $?
}

#
# Module_Install()
#
# Description:
#    To install the gdm rpm on linux
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Install() {

	if [[ $G_OS == "SunOS" ]]; then
		print "Not installing ${GDM_NOMINAL} on Solaris"
		return 0
	fi

	InstallMsg "${GDM_NOMINAL}" "$GDM_VERSION"

	if ! InstallProduct $G_ADMIN_FILE ${GDM_DIR} $GDM_RPM_LIST; then
		Error "${GDM_NOMINAL} not successfully installed"
	else
		# make a copy of our locale.alias and use orig if possible
		LOCFILE=/etc/X11/gdm/locale.alias
		if [[ -f ${LOCFILE}.sysorig ]]; then
			cp ${LOCFILE} ${LOCFILE}.sunray
			rm -f ${LOCFILE}
			mv ${LOCFILE}.sysorig ${LOCFILE}
		fi
	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 _SW_DATA_SAVED="no"

#
# BEGIN: Developers module variables definition here
#

export GDM_NOMINAL=GDM
export GDM_NAME=gdm
export GDM_VERSION=2.4.4.7.1
export GDM_RELEASE=3

export GDM_NOT_INSTALLED=1

typeset -r GDM_DIR=$(GetPackagePath ${GDM_NOMINAL}_${GDM_VERSION})

DeclareModuleVar GDM_PKG_LIST=""

PKGS1=""

case "$G_OS" in
SunOS)
  PKGS2=""

  ;;

Linux)
  PKGS2=""
  PKGS2="$PKGS2 ${GDM_NAME}-${GDM_VERSION}-${GDM_RELEASE}"
  ;;
esac

RPMS=""
RPMS="$RPMS ${GDM_NAME}-${GDM_VERSION}-${GDM_RELEASE}.i386.rpm"

typeset GDM_PKG_LIST="$PKGS1 $PKGS2"     # used by query, remove and 'pkgadd' ops
typeset GDM_RPM_LIST="$RPMS"             # used by 'rpm -i' ops

#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
