#!/bin/ksh -p
#
# ident "@(#)utgdmconfigctl.ksh	1.10	08/09/08 SMI"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u
########################################################################
#
# Configures gdm custom.conf to set the following properties for
# Sun Ray
# VTAllocation=false and
# DynamicXServers=true 
# Further creates 0010.SUNWut.xdmEnv.sh and 0100.SUNWut.sh links
# if any other gdm than our own is installed
#
########################################################################

# updateGdmCustom
function updateGdmCustom {
	${AWK} -F= -v GDMGREETER=$GDMGREETER -v BASEXSESSION=$BASEXSESSION \
		-v SUNWUTBegin="$SUNWUTBegin" -v SUNWUTEnd="$SUNWUTEnd" \
		-v SRCRASHSCRIPT=$SRCRASHSCRIPT '
		BEGIN {
			FoundVT=0;
			FoundDy=0;
			FoundGt=0;
			FoundReboot=0;
			FoundHalt=0;
			FoundCrash=0;
			if (BASEXSESSION == "")
				FoundBx=1;
			else
				FoundBx=0;
			InDaemonSection=0;
		}
		/\[daemon\]/ {
			InDaemonSection=1;
			print;
			next;
		}
		$1 == "VTAllocation" {
			FoundVT=1;
			if ($2 !~ "false.*") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "VTAllocation=false";
				print SUNWUTEnd;
				next;
			}
		}
		$1 == "DynamicXServers" {
			FoundDy=1;
			if ($2 !~ "true.*") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "DynamicXServers=true";
				print SUNWUTEnd;
				next;
			}
		}
		$1 == "Greeter" {
			FoundGt=1;
			if ($2 !~ ".*/gdmgreeter") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "Greeter=" GDMGREETER;
				print SUNWUTEnd;
				next;
			}
		}
		$1 == "RebootCommand" {
			FoundReboot=1;
			if ($2 != "") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "RebootCommand=";
				print SUNWUTEnd;
				next;
			}
		}
		$1 == "HaltCommand" {
			FoundHalt=1;
			if ($2 != "") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "HaltCommand=";
				print SUNWUTEnd;
				next;
			}
		}
		$1 == "XKeepsCrashing" {
			FoundCrash=1;
			if ($2 != "") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "XKeepsCrashing=" SRCRASHSCRIPT;
				print SUNWUTEnd;
				next;
			}
		}
		(BASEXSESSION != "") && ($1 == "BaseXsession") {
			FoundBx=1;
			if ($2 !~ BASEXSESSION ".*") {
				print "# SUNRAY DEL " $0;
				print SUNWUTBegin;
				print "BaseXsession=" BASEXSESSION;
				print SUNWUTEnd;
				next;
			}
		}
		InDaemonSection && /\[.*\]/ {
			# we have hit the end of the [daemon] block
			if ( ! FoundVT \
			     || ! FoundDy \
			     || ! FoundGt \
			     || ! FoundBx \
			     || ! FoundReboot \
			     || ! FoundHalt \
			     || ! FoundCrash) {
				print SUNWUTBegin;
				if (! FoundVT) {
					print "VTAllocation=false";
					FoundVT=1
				}
				if (! FoundDy) {
					print "DynamicXServers=true";
					FoundDy=1;
				}
				if (! FoundGt) {
					print "Greeter=" GDMGREETER;
					FoundGt=1;
				}
				if (! FoundBx) {
					print "BaseXsession=" BASEXSESSION;
					FoundBx=1;
				}
				if (! FoundReboot) {
					print "RebootCommand=";
					FoundReboot=1;
				}
				if (! FoundHalt) {
					print "HaltCommand=";
					FoundHalt=1;
				}
				if (! FoundCrash) {
					print "XKeepsCrashing=" SRCRASHSCRIPT;
					FoundCrash=1;
				}
				print SUNWUTEnd;
			}
			InDaemonSection=0;
		}
		{
			print $0;
		}
		END { 
			# Similar to "end of the [daemon] block" case above, but
			# no block follows - daemon was last, or missing
			if ( ! FoundVT \
			     || ! FoundDy \
			     || ! FoundGt \
			     || ! FoundBx \
			     || ! FoundReboot \
			     || ! FoundHalt) {
				print SUNWUTBegin;
				if (!InDaemonSection) {
					# daemon block was actually missing entirely
					print "[daemon]";
				}
				if (! FoundVT) {
					print "VTAllocation=false";
				}
				if (! FoundDy) {
					print "DynamicXServers=true";
				}
				if (! FoundGt) {
					print "Greeter=" GDMGREETER;
				}
				if (! FoundBx) {
					print "BaseXsession=" BASEXSESSION;
				}
				if (! FoundReboot) {
					print "RebootCommand=";
				}
				if (! FoundHalt) {
					print "HaltCommand=";
				}
				if (! FoundCrash) {
					print "XKeepsCrashing=" SRCRASHSCRIPT;
				}
				print SUNWUTEnd;
			}
		}
	' $1 

}

# restoreGdmCustom
#
#
function restoreGdmCustom {
	if ! ${AWK} -v SUNWUTBegin="$SUNWUTBegin" -v SUNWUTEnd="$SUNWUTEnd" '
		BEGIN {
			InSUNWUTBlock=0;
		}
		
		$0 == SUNWUTBegin {
			InSUNWUTBlock=1;
			next;
		}

		$0 == SUNWUTEnd {
			InSUNWUTBlock=0;
			next;
		}

		InSUNWUTBlock {
			next;
		}
		
		/^# SUNRAY DEL / {
		  sub("# SUNRAY DEL *", "", $0)
		  print $0
		  next;
		}

		{
			print $0;
		}

		END {
			if (InSUNWUTBlock)
				exit 1
			else
				exit 0
		}
	' $1
	then
		logit "Error: [${PROG}] malformed $1"
	fi
}

function rmLink  {
	typeset LINK=$1
	if [ -L $LINK ] ; then
		rm -rf $LINK
		if $VERB; then
			print "$LINK is removed"
		fi
	fi
}

function mkLink  {

	typeset LINK_TO=$1
	typeset LINK=$2

	rm -rf $LINK
	ln -fs $LINK_TO $LINK
	if [ $? -ne 0 ]; then
		logit "Error: [${PROG}] Failed to create $LINK "
		return 1
	elif $VERB; then
		print "$LINK to $LINK_TO link made"
	fi
	return 0
}

function isSLES10 {
	if [ "$OS" != "SunOS" ] \
		&& [ -f /etc/SuSE-release ] \
		&& [ "`sed -n 's/^ *VERSION *= *\([0-9]*\).*/\1/p' /etc/SuSE-release`" = 10 ]; then
		return 0
	else
		return 1
	fi
}

########################################
# This function should return 0 iff this is a platform that execs the
# scripts in xinitrc.d, rather than sourcing them
########################################
function isPlatformExecing {
	isSLES10
}

function isGDMEnabled {
	[ "$OS" != "SunOS" ] \
		|| [ "`/bin/svcs -H -o STATE gdm 2>/dev/null`" = "online" ]
}

########################################
# Since the activation framework is typically invoked from an /etc/rc
# script, stdout/stderr may not be captured, so syslog it also.
########################################
function logit {
	print -u2 "$*"
	logger -p user.err -t $0 "$*"
}

########################################################################
#
# Operation entry points, one for each keyword.	 This is where the
# specific work for this feature happens.
#
########################################################################


#
# disable()
#
function disable {

	typeset status=0
	
	restoreGdmCustom $SYSTEM_CUSTOMCONF >> $TMPFILE 
	if [ $? != 0 ]; then
		logit "Error: GDM custom.conf cannot be restored correctly"
		logit "\tPlease inspect $SYSTEM_CUSTOMCONF manually"
		status=1
		rm $TMPFILE
	else
		mv $TMPFILE $SYSTEM_CUSTOMCONF
		# GDM needs to be restarted but system reboot is
		# required after uninstall so this will occur
	fi

	if $XINIT_SOURCES; then
		rmLink $SYSTEM_XINITRCD/0010.SUNWut.xdmEnv.sh 
		rmLink $SYSTEM_XINITRCD/0100.SUNWut.sh
	fi

	rm -f $SUNRAY_ETCDIR/xmgr

	return $status

}

#
# enable ()
#
function enable {

	updateGdmCustom $SYSTEM_CUSTOMCONF >> $TMPFILE 
	mv $TMPFILE $SYSTEM_CUSTOMCONF
	
	# Restart GDM
	if isGDMEnabled; then
		`/usr/sbin/gdm-restart`
		if [ $? != 0 ]; then
			logit "Error: GDM not restarted successfully"
		fi
	fi

	if $XINIT_SOURCES && [ -d $SYSTEM_XINITRCD ]; then
		mkLink $SUNRAY_XINITRCD/0010.SUNWut.xdmEnv $SYSTEM_XINITRCD/0010.SUNWut.xdmEnv.sh
		mkLink $SUNRAY_XINITRCD/0100.SUNWut $SYSTEM_XINITRCD/0100.SUNWut.sh
	fi

	return 0
}

###################################
#
# resolvePath - find an executable in a path and return absolute pathname
#
# usage: resolvePath executable [ dir1 ] [ dir2 ] ...
#
###################################
resolvePath() {
	file=$1
	shift
	for dir; do
		fpath=$dir/$file
		if [ -x $fpath -a ! -d $fpath ]; then
			echo $fpath
			return
		fi
	done
}

#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {
	OS=`uname -s`
	SUNRAY_ETCDIR="/etc/opt/SUNWut"
	SYSTEM_XINITRCD="/etc/X11/xinit/xinitrc.d"
	SUNRAY_XINITRCD="$SUNRAY_ETCDIR/xinitrc.d"
	SUNWUTBegin="# SUNWut BEGIN - do not edit"
	SUNWUTEnd="# SUNWut END"

	# determine whether xinitrc.d scripts are sourced or exec'd
	if isPlatformExecing; then
		XINIT_SOURCES=false
		BASEXSESSION=$SUNRAY_ETCDIR/gdm/Xsession.source_wrapper
	else
		XINIT_SOURCES=true
		BASEXSESSION=
	fi
		
	GDM_DIR=`/etc/opt/SUNWut/basedir/lib/utgdmconfigpath`
	SUNRAY_GDM_DIR=${SUNRAY_ETCDIR}/gdm

	SYSTEM_CUSTOMCONF="${GDM_DIR}custom.conf"
	SRCRASHSCRIPT="${SUNRAY_GDM_DIR}/XKeepsCrashing.sunray"
	GDM_PACKAGE_NAME="gdm"
	TMPFILE="/var/opt/SUNWut/tmp/custom.conf.tmp.$$"
	GDMGREETER=$(resolvePath gdmgreeter /usr/lib /usr/libexec)
	if [ $OS = "SunOS" ]; then
		AWK=nawk
	else
		AWK=gawk
	fi
}

########################################################################
#
# Configurable parameter
#
########################################################################
# NONE

#########################################
#
# variables used in the shared script utctl-shlib
#
#########################################
DESCR="sets up gdm configuration"

########################################################################
#
# Execution starts here
#

PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
