#!/bin/ksh -p
#
# ident "@(#)utxsun.sh	1.20	11/10/11 Oracle"
#
# Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

#exec 2> $(mktemp /tmp/utxsun_debug.$$.XXXXXXXX)       # Debug
#set -x

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

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)

TMPFILE=$(mktemp "$TMPLATE")
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 -f "$TMPFILE"
	exit 2
elif [ -s "${TMPFILE}" ]; then
	# warning message
	logger -p user.notice -t "utxsun" -f "${TMPFILE}"
fi
/bin/rm -f "$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
