#!/bin/ksh -p
#
# ident "@(#)gdm-notify.sh	1.7 08/07/23"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Script to notify gdm that the Xservers file and  Xconfig file have changed
#
export PATH=/usr/bin:/usr/sbin:/opt/gnome/bin:/bin:/sbin

GDM_DIR="`/opt/SUNWut/lib/utgdmconfigpath`"
SUNRAY_GDM_DIR=/etc/opt/SUNWut/gdm

PRESESSION_DIR=${GDM_DIR}PreSession
POSTSESSION_DIR=${GDM_DIR}PostSession
POSTLOGIN_DIR=${GDM_DIR}PostLogin
INIT_DIR=${GDM_DIR}Init

SUNRAY_PRESESSION_DIR=$SUNRAY_GDM_DIR/SunRayPreSession
SUNRAY_POSTSESSION_DIR=$SUNRAY_GDM_DIR/SunRayPostSession
SUNRAY_POSTLOGIN_DIR=$SUNRAY_GDM_DIR/SunRayPostLogin
SUNRAY_INIT_DIR=$SUNRAY_GDM_DIR/SunRayInit

verbose=0
umask 0022
DMNAME=gdm-binary
which_gdm=$(whence gdm-binary)
if [ -z $which_gdm ]; then
	which_gdm=$(whence gdm)
	if [ -z $which_gdm ]; then
		print -u2 "gdm/gdm-binary not found"
		exit 1
	fi
	DMNAME=gdm
fi
unset LD_LIBRARY_PATH
BASE=/etc/opt/SUNWut/basedir
SUNWUTLIB=$BASE/lib
XSUN=$SUNWUTLIB/utxsun

GDMDYN=gdmdynamic

if [ $# -eq  1 ]
then
	dpy=$1
else
	exit
fi

#
# since SRSS is started in rc 5 scripts and
# gdm is started out of inittab, we cannot
# be certain that gdm is running
#

count=1000

while [[ $count -gt 0 ]]
do
	count=$(($count-1))
	pgrep -P 1 -u 0 $DMNAME >/dev/null
	if [[ $? -ne 0 ]]
	then
		sleep 15
	else
		count=0
	fi
done

#Cleanup if old links are lying around
if [ -L  "$PRESESSION_DIR/:$dpy" ]; then
	rm  "$PRESESSION_DIR/:$dpy"
fi
if [ -L  "$POSTSESSION_DIR/:$dpy" ]; then
	rm  "$POSTSESSION_DIR/:$dpy"
fi
if [ -L  "$POSTLOGIN_DIR/:$dpy" ]; then
	rm  "$POSTLOGIN_DIR/:$dpy"
fi
if [ -L  "$INIT_DIR/:$dpy" ]; then
	rm  "$INIT_DIR/:$dpy"
fi

# Creating display links for Sun Rays
ln -s "$SUNRAY_PRESESSION_DIR/Default" "$PRESESSION_DIR/:$dpy"
ln -s "$SUNRAY_POSTSESSION_DIR/Default" "$POSTSESSION_DIR/:$dpy"
ln -s "$SUNRAY_POSTLOGIN_DIR/Default" "$POSTLOGIN_DIR/:$dpy"
ln -s "$SUNRAY_INIT_DIR/Default" "$INIT_DIR/:$dpy"

cmd="$dpy""=""$XSUN"
($GDMDYN -a $cmd; $GDMDYN -r $dpy)&

