#!/bin/ksh -p
#
# ident "@(#)utxsun.sh	1.19 09/09/14 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#exec 2>/var/tmp/utxsun.$$       # Debug
#set -x

INFODIR=/var/opt/SUNWut
RESDIR=$INFODIR/dispinfo
SUNWUTBIN=/opt/SUNWut/bin
UTXCONFIG=$SUNWUTBIN/utxconfig
FONTPATH=""
XMOREOPTS="-br"
AWK=/bin/awk
TMPFILE=/tmp/utxsun_err.$$

if [ ! -x $AWK ]
then
    AWK=/usr/bin/awk
    if [ ! -x $AWK ]
    then
	print "No awk found" 1>&2
	exit 2
    fi
fi

dpy=${1#:}
geom=$($AWK -F= '/GROUP_PRIVATE_DATA/ {print $3}' \
	$RESDIR/$dpy 2> /dev/null)

/bin/rm -rf $TMPFILE
XSERVER_CMD=$(${UTXCONFIG} -d $1 -L ${geom:-1x1} 2>${TMPFILE})
if [ $? != 0 ]; then
	# error message
	logger -p user.error -t "utxsun" -f ${TMPFILE}
	/bin/rm -rf $TMPFILE
	exit 2
elif [ -s ${TMPFILE} ]; then
	# warning message
	logger -p user.notice -t "utxsun" -f ${TMPFILE}
fi
/bin/rm -rf $TMPFILE

XSERVER=${XSERVER_CMD%% *}
XOPTS=${XSERVER_CMD#${XSERVER}}

case $(basename $XSERVER) in
Xnewt*)	# turn on the backingstore for the static Xnewt server
	# and force Xnewt to terminate on a reset
	XMOREOPTS="$XMOREOPTS +bs -terminate"

	# The fontpath file contains the list of font pathnames which
	# are used to override the internal default fontpath for Xnewt.
	# This is only used on Linux distros to workaround different
	# locations where the font files are stored.  This file is created
	# by the X11complinks activation script on Linux.
	if [ -f /etc/opt/SUNWut/X11/fontpath ] ; then
		# The following awk script accumulates the fontpaths to be used
		# with the -fp option to Xnewt.  Comment and blank lines are
		# ignored and imbedded comments are not allowed.
		FONTPATH=$($AWK '\
			BEGIN	{ sep=""; fp=""; } \
			/^#/ || /^[ 	]*$/	{ next } \
				{ fp = fp sep $1; \
				  sep="," } \
			END	{ print fp } \
				' /etc/opt/SUNWut/X11/fontpath )
	fi

	if [ x$FONTPATH != x ] ; then
		XMOREOPTS="$XMOREOPTS -fp $FONTPATH"
	fi
	;;
# Xsun)	;;
esac

exec $XSERVER "$@" $XMOREOPTS $XOPTS
