#!/bin/ksh -p
#
# ident "@(#)gdm-remove-dpy.sh	1.11	12/06/05 Oracle"
#
# Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
#
# utility script to remove X server configuration from
# system configuration files for a token 
#
# The database of available display numbers is taken as 
# Xservers. If a display is mentioned as
#
#	sysname:num OR :num
#
# at the beginning of a line or the beginning of a comment, it is considered
# unavailable for use.
#
# E.g. a line '# :3 RESERVED' will prevent display 3 from being assigned
#

verbose=0
umask 0022
unset LD_LIBRARY_PATH
export PATH=/usr/bin:/bin:/usr/sbin:/sbin

function getdispkeyval {
	awk -F= "{
		if (\$1 == \"$1\") {print \$2} 
	}" /tmp/SUNWut/config/ctokens/$token
}

BASE=/etc/opt/SUNWut/basedir
SUNWUT=$BASE
SUNWUTLIB=$SUNWUT/lib

dir=$1		# configuration directory location
token=$2	# token name
rmdisplay=$3	# rm display command line

xserv=$dir/Xservers     # DMNAME's Xservers file
xconf=$dir/Xconfig      # DMNAME's Xconfig file
xconfiglock=$dir/config.lock    # X-Windows config lock file

GDM_DIR=$($SUNWUTLIB/utgdmconfigpath)
PRESESSION_DIR=${GDM_DIR}PreSession
POSTSESSION_DIR=${GDM_DIR}PostSession
INIT_DIR=${GDM_DIR}Init

DPY=$(getdispkeyval DISPLAY)
TYPE=$(getdispkeyval SESSION_TYPE)

rm -f "$PRESESSION_DIR/:$DPY"
rm -f "$POSTSESSION_DIR/:$DPY"
rm -f "$INIT_DIR/:$DPY"

#
# Remove the display file on success -> this must be atomic
# because the display number may be reassigned immediately
#
gdmglue=
if [[ "$TYPE" == "default" ]]; then
	gdmglue="; /opt/SUNWut/lib/gdm/utgdmdynamic -b -d "'$UT_DPY'
fi

post="$rmdisplay""$gdmglue"

$SUNWUTLIB/utdtutil -l $xconfiglock -D "_TOKEN=$token\>" -r "$post" \
		$xserv $xconf >/dev/null

