

#	Copyright (c) 1990 OPUS Systems
#	  All Rights Reserved

#ident	"@(#)Xsetupmgmt	1.1"

#menu#  choose the X server, window manager, and font defaults
#help#
#help#	This subcommand creates links to the default X server /usr/bin/X11/X,
#help#	to the default window manager /usr/bin/X11/Xwm, and allows you to pick
#help#  default fonts in /usr/lib/X11/fonts/misc/fonts.alias  

#	/usr/admin/menu/packagemgmt/Xsetupmgmt
#
#	get default server /usr/bin/X11/X
#	get default window manager to /usr/bin/X11/Xwm
#	pick default fonts in /usr/lib/X11/fonts/misc/fonts.alias
#

CWD=/usr/admin/menu/packagemgmt/xmgmt
XNET="-local"
XBIN=/usr/bin/X11
MBIN=/usr/bin/MOTIF
XLIB=/usr/lib/X11
XFONT=$XLIB/fonts/misc
XALIAS=$XFONT/fonts.alias

# Don't do this for ev-235
# tput clear	# clear screen

if [ ! -d $XBIN ]; then
	echo
	echo "X bin directory $XBIN not found."
	exit 1
fi


#	LINK DEFAULT SERVER TO /usr/bin/X11/X

XFLAG=BAD
while [ $XFLAG != OK ]; do

	if [ -x $XBIN/X ]; then
		set -- `ls -i $XBIN/X`
		iX=$1
	else
		iX=NONE

	fi
	XLIST=" "
	for S in mfb cfb4 cfb8 age; do
		if [ -x $XBIN/Xopus$S ]; then
			set -- `ls -i $XBIN/Xopus$S`
			eval iX$S=$1
			XLIST="$XLIST$S "
		else
			eval iX$S=""
		fi
	done

	SERVERmfb="\ \ \ \ \ mfb\ \ -- monochrome frame buffer"
	SERVERcfb4="\ \ \ \ \ cfb4 -- color frame buffer"
	SERVERage="\ \ \ \ \ AGE\ \ -- high resolution, TMS340 graphics processors\n"

	if [ "$XLIST" = " " ]; then
		echo "No servers available; install X first."
		exit 1
	else
		echo "\nX servers available:"
		for S in $XLIST; do
			eval eval echo "\$SERVER$S"
		done
		echo
	fi

	XFLAG=OK
	case $iX in
		$iXmfb)
			echo "Current default X server: mfb";;
		$iXcfb4)
			echo "Current default X server: cfb4";;
		$iXcfb8)
			echo "Current default X server: cfb8";;
		$iXage)
			echo "Current default X server: age"
			XFONT=$XLIB/fonts/age
			XALIAS=$XFONT/fonts.alias
			;;
		*)
			XFLAG=BAD; echo "No default X server";;
	esac

	if [ $XFLAG = OK ]; then
		echo "\nChoose new default X server [${XLIST}CR q ]: \c"
	else
		echo "   Choose default X server [${XLIST}q ]: \c"
	fi
	read ANS
	case "$ANS" in
		"")	if [ $XFLAG != OK ]; then
				echo Must choose a default X server
			fi;;

		q|Q)	exit 0;;

		mfb | cfb4 | cfb8)
			if [ -x $XBIN/Xopus$ANS ]; then
				rm -f $XBIN/X
				ln $XBIN/Xopus$ANS $XBIN/X
				XFLAG=OK
				XFONT=$XLIB/fonts/misc
				XALIAS=$XFONT/fonts.alias
			else
				echo X $ANS server not available
				XFLAG=BAD
			fi;;

		age)
			if [ -x $XBIN/Xopus$ANS ]; then
				rm -f $XBIN/X
				ln $XBIN/Xopus$ANS $XBIN/X
				XFLAG=OK
				XFONT=$XLIB/fonts/age
				XALIAS=$XFONT/fonts.alias
			else
				echo X $ANS server not available
				XFLAG=BAD
			fi;;

		*)
			echo "Must choose from [$XLIST]"
			XFLAG=BAD;;
	esac
done

#	LINK DEFAULT WINDOW MANAGER
#
echo
XFLAG=BAD
while [ $XFLAG != OK ]; do

	if [ -x $XBIN/Xwm ]; then
		set -- `ls -i $XBIN/Xwm`;	iXwm=$1
	else
		iXwm=NONE

	fi
	XLIST=" "
	for WM in uwm twm mwm ; do
		if [ -x $XBIN/$WM ]; then
			set -- `ls -i $XBIN/$WM`
			eval i$WM=$1
			XLIST="$XLIST$WM "
		elif [ -x $MBIN/$WM ]; then
			set -- `ls -i $MBIN/$WM`
			eval i$WM=$1
			XLIST="$XLIST$WM "
		else
			eval i$WM=""
		fi
	done

	if [ "$XLIST" = " " ]; then
		echo "No window managers available; install X first."
		exit 1
	fi

	XFLAG=OK
	case $iXwm in
		$iuwm)
			echo "Current default X window manager: uwm";;
		$imwm)
			echo "Current default X window manager: mwm";;
		$itwm)
			echo "Current default X window manager: twm";;
		NONE)
			XFLAG=BAD; echo "No default X window manager";;
		*)
			echo "Current default X window manager is unknown";;
	esac

	if [ $XFLAG = OK ]; then
		echo "\nChoose new default window manager [${XLIST}CR q ]: \c"
	else
		echo "   Choose default window manager [${XLIST}q ]: \c"
	fi
	read ANS
	case "$ANS" in
		"")	if [ $XFLAG != OK ]; then
				echo Must choose a default window manager
			fi;;

		q|Q)	exit 0;;

		uwm | mwm | twm)
			if [ -x $XBIN/$ANS ]; then
				rm -f $XBIN/Xwm
				ln $XBIN/$ANS $XBIN/Xwm
				XFLAG=OK
			elif [ -x $MBIN/$ANS ]; then
				rm -f $XBIN/Xwm
				ln $MBIN/$ANS $XBIN/Xwm
				XFLAG=OK
			else
				echo $ANS not available
				XFLAG=BAD
			fi;;

		*)
			echo "Must choose from [$XLIST]"
			XFLAG=BAD;;
	esac
done


#	PICK DEFAULT FIXED & VARIABLE FONTS
#
if [ ! -d $XFONT ]; then
	echo
	echo "X font directory $XFONT not found."
	exit 1
fi

XFLAG=BAD
while [ $XFLAG != OK ]; do
echo
if [ -s $XALIAS ]; then
	echo Current default fonts:
	XFIXED=`grep fixed $XALIAS`
	XVARIABLE=`grep variable $XALIAS`
	if [ "$XFIXED" ]; then
		echo "     $XFIXED"
	fi
	if [ "$XVARIABLE" ]; then
		echo "     $XVARIABLE"
	fi
else
	echo No default fonts.
	XFIXED=""
	XVARIABLE=""
	> $XALIAS
fi

echo "\n     Choose font size [ 6x10 6x12 6x13 8x13 8x13bold 9x15 ]"
echo "                      [ 8 10 12 14 18 24 CR q ]: \c"
read ANS

XFLAG=OK
case "$ANS" in
	"")	if [ ! "$XFIXED" -o ! "XVARIABLE" ]; then
			echo Must select a default font size.
			XFLAG=BAD
		fi;;
	q|Q)	exit 0;;
	8)	XFIXED="fixed    *-courier-medium-r-normal--8-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--8-*";;
	10)	XFIXED="fixed    *-courier-medium-r-normal--10-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--10-*";;
	12)	XFIXED="fixed    *-courier-medium-r-normal--12-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--12-*";;
	14)	XFIXED="fixed    *-courier-medium-r-normal--14-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--14-*";;
	18)	XFIXED="fixed    *-courier-medium-r-normal--18-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--18-*";;
	24)	XFIXED="fixed    *-courier-medium-r-normal--24-*"
		XVARIABLE="variable *-helvetica-medium-r-normal--24-*";;
	6x10)	XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-medium-r-normal--10-*";;
	6x12)	XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-medium-r-normal--12-*";;
	6x13)	XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-medium-r-normal--12-*";;
	8x13)	XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-medium-r-normal--12-*";;
	8x13bold) XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-bold-r-normal--12-*";;
	9x15)	XFIXED="fixed    $ANS"
		XVARIABLE="variable *-helvetica-medium-r-normal--14-*";;
	*)	XFLAG=BAD;; 
esac
done
echo

grep -v fixed    $XALIAS > /tmp/fonts.alias
grep -v variable /tmp/fonts.alias > $XALIAS
rm -f /tmp/fonts.alias
echo "$XFIXED"    >> $XALIAS
echo "$XVARIABLE" >> $XALIAS

OPUS_CFG=/tmp/opuscfg.$$

HERCULES=hercules
VGA1="vga monochrome"
VGA4="vga color"
EV235="ev235, high resolution monochrome"
AGE=AGE

error_cleanup()
{
	trap "" 1 2 3 15
	rm -f $OPUS_CFG
	sync
	exit 1
}

trap "error_cleanup" 1 2 3 15

page()
{
	echo "\n------------------------------------------------------------------------------"
}

opuscfg()
{
	MONO=""
	COLOR=""
	while :; do
		page #1
		echo "\nChoose the X device driver:\n"
		echo "1) $HERCULES"
		echo "2) $VGA1"
		echo "3) $VGA4"
		echo "4) $VGA1 and $VGA4"
		echo "5) $EV235"
		echo "6) $AGE"
		echo "\nEnter [1-6]: \c"
		read ANS
		case $ANS in
			1)
				MONITOR=hercules
				break
				;;
			2)
				MONITOR=vga1
				break
				;;
			3)
				MONITOR=vga4
				break
				;;
			4)
				MONO=YES
				COLOR=YES
				break
				;;
			5)
				MONITOR=ev235
				break
				;;
			6)
				MONITOR=age
				break
				;;
			"")
				echo "Enter [1-6]: \c"
				;;
			*)
				echo "\n\007Invalid response."
				sleep 1
				;;
		esac
	done

	page #2
	while :; do
		TTY_VALID=NO
		echo "\nEnter the mouse serial port number [tty0]: tty\c"
		read ANS
		case $ANS in
			"")
				TTY=""
				echo Default serial line for the mouse is tty0.
				break
				;;
			[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
				MOUSE=`grep "^<tty.*" $OPUS_CFG | grep -v mega | sed 's/^<tty/=/' | cut -d= -f2"`
				for i in $MOUSE; do
					if [ $ANS = $i ]; then
						TTY_VALID=YES
					fi
				done
				if [ $TTY_VALID = YES ]; then
					if [ $ANS -eq 0 ]; then
						TTY=""
					else
						TTY=",tty=$ANS"
					fi
					break
				else
					echo "\n\007Invalid response. Valid tty ports are:\n"
					for port in $MOUSE; do
						echo "     tty$port\c"
					done
					echo
				fi
				;;
			*)
				echo "\n\007Invalid response."
				;;
		esac
	done
	
	if [ "$MONO" -a "$COLOR" ]; then
		X11="<X=X11:0(display=vga1,pointer=logitech$TTY)>
<X=X11:1(display=vga4,pointer=logitech$TTY)>"
	else
		X11="<X=X11:0(display=$MONITOR,pointer=logitech$TTY)>"
	fi

	/opus/bin/ducp -a /opus/opus.cfg | sed 's/^<X/\.X/
	/mega/s/^</\./
	/^<end>/d' > $OPUS_CFG
	echo "$X11" >> $OPUS_CFG
	echo "<end>" >> $OPUS_CFG
	/opus/bin/udcp -a $OPUS_CFG /opus/opus.cfg

	while :; do
		page #3
		echo "\nThe OPUS configuration file '\\opus\\opus.cfg' has been modified."
		if [ "$XCFG_INVLD" ]; then
			echo "\nThe old X configuration:"
			echo "\n$XCFG_INVLD"
			echo "\nhas been modified to:"
		else
			echo "\nYour new X configuration reads:"
		fi
		echo "\n$X11"	
		echo "\nWould you like to modify it further? [y/n] \c"
		read ANS
		case $ANS in
			y|Y)
				XCFG_INVLD="$X11"
				opuscfg
				break
				;;
			n|N)
				page #end
				echo "\n\007Reboot your workstation and boot UNIX using the modified '\\opus\\opus.cfg'."
				break
				;;
			*) 
				echo "\n\007Invalid response."
				;;
		esac
	done
	rm -f $OPUS_CFG
}

#		MODIFY OPUS.CFG
#

while :; do
	echo "\nWill the X Window System be used on the system console? [y/n] \c"
	read ANS
	case $ANS in
		y|Y)
			break
			;;
		n|N)
			exit 0
			;;
		"")
			;;
		*)
			echo "\n\007Invalid response."
			;;
	esac
done

# Modify opus.cfg

CONFIG=`/opus/bin/opdos wildcard /opus/opus.cfg`
if [ "$CONFIG" ]; then
	/opus/bin/ducp -a /opus/opus.cfg > $OPUS_CFG 
else
	echo "\n\007\\opus\\opus.cfg not found in DOS.\n"
	echo "\nCannot proceed with the XCORE preinstallation."
	exit 1
fi

EQNX=`grep "^<.*mega" $OPUS_CFG`
XEQNX=`grep "^\[xeqnx\]" $OPUS_CFG`

if [ "$EQNX" -a ! "$XEQNX" ]; then
	echo "\n\007This system is currently configured for the Equinox MEGAPORT Adapter card."
	echo "X cannot co-exist with MEGAPORT on the system console."
	echo "\nChoose:\n"
	echo "1) disable MEGAPORT serial ports"
	echo "2) abort X installation"
	while :; do
		echo "\nEnter [1-2]: \c"
		read ANS
		case $ANS in
			1)
				break
				;;
			2)
				error_cleanup
				;;
			"")
				;;
			*)
				echo "\n\007Invalid response."
				;;
		esac
	done
fi

XCFG=`grep -c "^<X=X11" $OPUS_CFG`
X11=`grep "^<X=X11" $OPUS_CFG`
if [ $XCFG -ge 2 ]; then
	DISPLAY0=`grep -c "^<X=X11:0" $OPUS_CFG`
	DISPLAY1=`grep -c "^<X=X11:1" $OPUS_CFG`
	if [ $XCFG -eq 2 -a $DISPLAY0 -eq 1 -a $DISPLAY1 -eq 1 ]; then
		:
	else
		page #0
		XCFG_INVLD="$X11"
		echo "\n\007sysadm: configuration error: duplicate X devices defined"
		echo "\nThe opus configuration file must be modified."
		opuscfg
		exit 0
	fi
fi

if [ "$X11" ]; then
	while :; do
		echo "\nThe X device driver in the OPUS configuration file reads:\n"
		echo "$X11"
		echo "\nWould you like to modify it? [y/n] \c"
		read ANS
			case $ANS in
				y|Y)
					opuscfg
					break
					;;
				n|N)
					rm -f $OPUS_CFG
					break
					;;
				"")
					;;
				*) 
					echo "\n\007 Invalid response."
					;;
		esac
	done
else
	opuscfg
fi
