#!/bin/ksh -p
#
# ident "@(#)M08GDM.ksh	1.10 10/12/29 SMI"
#
# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#

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

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

function 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 NO_GDM_INSTALLED
	OLD_GDM_INSTALLED=0
	INSTALLED_GDM=""
	LOCALE_FILE="/etc/X11/gdm/locale.alias"

	# Check to see if our version of gdm is already installed
	rpm --quiet -q ${GDM_PACKAGE_NAME} > /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
		INSTALLED_GDM_VERSION="${INSTALLED_GDM#*\-}"
		INSTALLED_GDM_VERSION_NUMBER="${INSTALLED_GDM_VERSION%\-*}"
		CompareVersion ${GDM_REQ_VERSION} ${INSTALLED_GDM_VERSION_NUMBER}
		if [ $? = 1 ]; then
		    OLD_GDM_INSTALLED=1
		fi
	    fi

	    if [[ ${OLD_GDM_NOT_INSTALLED} == 1 ]] && \
			[[ ${OLD_GDM2_NOT_INSTALLED} == 1 ]]; then
		NO_GDM_INSTALLED=1
	    fi

	    if [[ ${OLD_GDM_INSTALLED} == 1 ]] || [[ ${NO_GDM_INSTALLED} == 1 ]] ; then
		print "Error: Please install $GDM_PACKAGE_NAME before installing Sun Ray Software"
		if [[ ${OLD_GDM_INSTALLED} == 1 ]] ; then
		    print "Please be sure to back up any customized files such as\n$LOCALE_FILE before un-installing the old version."
		fi
                # Apparently 2 is the magic number to abort installation.
		return 2
	    fi
	fi

	return $exit_no
}

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

function Module_Preserve {
   return 0
}

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

function Module_Remove {

    return 0
}

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

function Module_Install {

    return 0
}

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

function Module_Restore {
   return 0

}

#
# function Module_Abort
#
# Description:
#    Abort procedure
#
# Parameters:
#   (none)
#
# Globals used:
#

function Module_Abort {
   return 0
}

#
# function Module_Exit
#
# Description:
#    Exit procedure (normal termination)
#
# Parameters:
#   (none)
#
# Globals used:
#

function 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="yes"
DeclareModuleVar _SW_DATA_SAVED="no"

#
# BEGIN: Developers module variables definition here
#


export GDM_NAME=gdm
export GDM_REQ_VERSION=2.12
export GDM_OUR_VERSION=2.16.7
export GDM_PACKAGE_NAME=${GDM_NAME}-${GDM_OUR_VERSION}

export GDM_NOT_INSTALLED=1



#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
