#!/bin/ksh -p
#
# ident "@(#)utxinit.sh	1.8 02/10/29 SMI"
#
# Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
#
#
# Xinit replacement that works
#
SUNWUTLIB=$(pkginfo -r SUNWuto 2>/dev/null)/SUNWut/lib

#
# This part parses the client and server commands (separated by "--")
#
cprog=""
sprog=""

client=t

sep=""

for i
do
	if [ "$i" = "--" ]
	then
		client=f
		sep=""
	elif [ "$client" = t ]
	then
		cprog="$cprog$sep$i"
		sep=" "
	else
		sprog="$sprog$sep$i"

		case "$i" in
		:[0-9]*)
			display="$i"
			;;
		esac
		sep=" "
	fi
done

#
# Set up defaults
#
if [ "$display" = "" ]
then
	display=":0"
fi

if [ "$sprog" = "$display" ]
then
	sprog="/usr/openwin/bin/Xsun $display"
fi

if [ "$cprog" = "" ]
then
	cprog="/usr/openwin/bin/xterm -geometry  +1+1  -n  login"
fi

#
# Now we know what to run; do the work
#

#
# Setup the session_prof file
# 
DPY=${display#*:}
DPY=${DPY%.*}
SESSION_PROC=/tmp/SUNWut/session_proc/$DPY

#
# cleanup function -- temporary only
# XXXX: this should be removed once the official fix is integrated
#
function cleanup {
	/bin/rm -f $SESSION_PROC 2> /dev/null
}


#
# Get ready to catch SIGUSR1
#
ready=0
trap "ready=1" USR1

#
# To be considered a "smart" parent we must pass SIGIGN on SIGUSR1
#
( trap "" USR1 ; exec $sprog ) &

function openControl {
	exec 8<> /dev/tcp/$1/$2
}

DPY=${display#*:}
DPY=${DPY%.*}

SID=$(sed -n 's/SESSION=\(.*\)/\1/p' /var/opt/SUNWut/displays/$DPY)

function disconnect {
	CONTROL_IPA=0.0.0.0
	CONTROL_PORT=7010

	openControl $CONTROL_IPA $CONTROL_PORT

	print -u8 "control $SID
request disconnect
end
"

	cat < /dev/fd/8 | while read line
	do
		case $line in
		(ok*|end*)
			print -u8 quit 2>/dev/null
			;;
		(error*)
			print -u2 -- $line
			;;
		(quit*)
			;;
		*)
			;;
		esac
	done
	exec 8<> /dev/null
}

#
# Pid is the X server's pid; on exit kill it
#
pid=$!
trap "kill -9 $pid; cleanup; exit" 0 1 2 15

#
# Get ready
#
DISPLAY=$display
export DISPLAY

#
# Wait for the callback or time-out
#
incr=0
while [ $ready -eq 0 -a $incr -lt 15 ]
do
	sleep 1
	incr=$(($incr + 1))
done

#
# Run the client program while keeping the display open via utxexec
#
$SUNWUTLIB/utxexec $cprog &
apppid=$!

# Generate the session_proc file
#
echo XID=$DPY > $SESSION_PROC
echo pid=$apppid >> $SESSION_PROC
echo program=$SUNWUTLIB/utxexec >> $SESSION_PROC
echo server_pid=$pid >> $SESSION_PROC
echo server_prog=$(echo $sprog | cut -d' ' -f1) >> $SESSION_PROC
/bin/chmod 644 $SESSION_PROC

# 
# Wait for app to exit
#
wait $apppid
status=$?

# 1. If Xserver has exited, then disconnect
# 2. If Xserver is still there (status != 3), that means 
# app exited first, then kill the Xserver and exit
#

if [ $status -eq 3 ]
then
  #
  # X Server exited, This could happen if a
  # "Ctrl-alt-bksp-bksp" sequence was used.
  # do a disconnect
  #
  disconnect 
else
  #
  # App exited first, This is the normal case
  # Kill the X server and exit
  #
  kill -9 $pid
  cleanup
fi
trap "" 0 1 2 15

