#!/bin/ksh -p
#
# ident "@(#)utxsun.sh	1.15 08/02/05 SMI"
#
# Copyright 2008 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
AWK=/bin/awk
XMOREOPTS=
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

# This is temporary for Beta. We need to figure out what the
# fontpath is for a particular system. We'll grep through the
# XF86Config file. For FCS we'll do this at install time and
# keep the path in a file.
if [[ -f /etc/X11/XF86Config ]] ; then
	# match lines with FontPath that are not commented out,
	# accumulate the path, but remove double quotes before appending:
	FONTPATH=$($AWK '$1 == "FontPath" { fp = fp "," substr($2,2,length($2)-2) } \
                        END { print substr(fp,2) } \
                        ' /etc/X11/XF86Config)
	if [ x$FONTPATH != x ] ; then
		XMOREOPTS="-fp $FONTPATH"
	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
	XMOREOPTS="$XMOREOPTS +bs";;
# Xsun)	;;
esac

exec $XSERVER "$@" $XMOREOPTS $XOPTS
