#!/bin/ksh -p
#
# ident "@(#)M08GDM.ksh	1.11 12/05/01 SMI"
#
# Copyright (c) 2004, 2012, 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 GetRPMVersion {
    rpm -q --qf '%{VERSION}' $1
}

function Module_Init {

	typeset exit_no=0 Locale_File="/etc/X11/gdm/locale.alias"

	if [[ $G_OS == "SunOS" ]]; then
		return $exit_no
	fi

	# Check to see if a gdm of appropriate version is installed on the system
	typeset Obsolete_GDM_Installed=false GDM_OK=false
	typeset Installed_GDM_Version Action
	
	if Installed_GDM_Version="$(GetRPMVersion ${GDM_NAME_OL6})" ||
	    Installed_GDM_Version="$(GetRPMVersion ${GDM_NAME})"; then
	    CompareVersion ${GDM_REQ_VERSION} ${Installed_GDM_Version}
	    if (( $? == 1 )); then
		Obsolete_GDM_Installed=true
	    else
		GDM_OK=true
	    fi
	fi

	if ! ${GDM_OK}; then
	    if ${Obsolete_GDM_Installed}; then
		Action=upgrade
	    else
		Action=install
	    fi
	    print "Error: Please $Action $GDM_NAME or $GDM_NAME_OL6 before installing Sun Ray Software"
	    if ${Obsolete_GDM_Installed} ; 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

	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_NAME_OL6=gdm-multiseat
export GDM_REQ_VERSION=2.12

export GDM_NOT_INSTALLED=1



#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
