:
#	@(#) n9gxi.sh 1.6 92/07/24 
clear
init_range="C0000 D0000 E0000 F0000"
echo "\n\tNumber 9 GXi Grafinfo File Configuration"
echo "
Number 9 GXi video cards can be used two different ways by the X server.
The first method involves communicating with the card via its I/O ports.
The second involves both the I/O ports and a 64K memory window mapped
into physical PC address space.

This script expects you to know which I/O ports address range the card
is currently set to use, and if you want to also use the memory mapped
windows, which 64K address range is available on your machine.

Supported memory mapped window address are $init_range.
Please check that no other installed hardware conflicts with the address
you select.  The address of the I/O ports is hardware configurable via a
DIP switch block on the card (see the card installation document).  The
memory mapped window is software configured by the X server at runtime.

This script will remove and/or replace all files named n9gxi*.xgi in
the directory /usr/lib/grafinfo/numbernine.  If you have made any
modifications to these files you should exit this script now and backup
those modifications.
"

echo -n "Do you want to configure your video cards I/O ports and/or memory mapping [y]:"
read sure
if [ "$sure" = "" ]
then
	sure="y"
fi
if [ "$sure" != "y" ]
then
	# If n9gxi.xgi exists, a card hasn't been configured yet.
	if [ -f /usr/lib/grafinfo/numbernine/n9gxi.xgi ]
	then
		echo "\nERROR: At least one Number 9 GXi video card must be configured."
		echo "\nExiting script."
		exit 1
	fi
	echo "Exiting script."
	exit 0
fi

count=0
sure=n
while [ "$count" -le 0 -a "$sure" = "n" ]
do
	echo -n "How many Number 9 GXi cards are to be installed [1]: "
	read count
	if [ "$count" = "" ]
	then
		count=1
	elif [ "$count" -lt 0 ]
	then
		echo -n "Please input a positive number, or interrupt script"
		echo " to exit."
	elif [ "$count" -eq 0 ]
	then
		echo "Aborting script."
		exit 1
	fi
	if [ "$count" -ge 8 ]
	then
		echo -n "Are you sure [n]: "
		read sure
		if [ "$sure" = "" ]
		then
			sure="n"
		fi
		if [ "$sure" = "n" ]
		then
			count=0
		fi
	fi
done

rm -f /usr/lib/grafinfo/numbernine/n9gxi*.xgi

i=0
while [ $i -lt $count ]
do
	i=`expr $i + 1`
	echo "\nConfiguring card $i:"
	echo "\tWill this card use;"
	echo "\t\t1. A memory mapped window and I/O ports"
	echo "\t\t2. Only I/O ports"
	com=0
	while [ "$com" != "1" -a "$com" != "2" ]
	do
		echo -n "\tInput 1 or 2 [2]: "
		read com
		if [ "$com" = "" ]
		then
			com=2
		fi
		if [ "$com" != "1" -a "$com" != "2" ]
		then
			echo "Invalid selection, try again."
		fi
	done
	if [ "$com" = "1" ]
	then
		def_range="E0000"
		range="$init_range"
		found=0
		while [ "$found" -eq 0 ]
		do
			echo "\tSelect a base address for the 64K memory window"
			echo -n "\tfrom the range $range [$def_range]: "
			read sel_range
			if [ "$sel_range" = "" ]
			then
				sel_range="$def_range"
			fi
			found=0
			for j in $range
			do
				if [ "$j" = "$sel_range" ]
				then
					found=1
				fi
			done
			if [ "$found" -eq 0 ]
			then
				echo "Invalid memory window address selected."
			fi
		done
	fi
	def_range="2A0"
	range="280 290 2A0 2B0 2C0 2D0 2E0 2F0"
	found=0
	while [ "$found" -eq 0 ]
	do
		echo "\tSelect the base I/O address from the "
		echo -n "\trange $range [$def_range]: "
		read io_range
		if [ "$io_range" = "" ]
		then
			io_range="$def_range"
		fi
		found=0
		for j in $range
		do
			if [ "$j" = "$io_range" ]
			then
				found=1
			fi
		done
		if [ "$found" -eq 0 ]
		then
			echo "Invalid base I/O address selected."
		fi
	done
	io_base=`echo $io_range | sed -e 's/.$//'`
	io_desc="0x"$io_base"0"
	instance=$i
	sed_output="/usr/lib/grafinfo/numbernine/n9gxi"$i".xgi"
	if [ "$com" = 1 ]
	then
		mem_base=$sel_range
		mem_base_k=`echo $sel_range | sed -e 's/....$//'`
		class_desc="I/O=$io_desc, memory=$mem_base"
		sed_input=/usr/lib/grafinfo/numbernine/n9gxi.tms3
	else
		mem_base=""
		mem_base_k=""
		class_desc="I/O=$io_desc"
		sed_input=/usr/lib/grafinfo/numbernine/n9gxi.tms0
	fi
	sed	-e "s/@INSTANCE@/$instance/g" \
		-e "s/@IO_BASE@/$io_base/g" \
		-e "s/@MEM_BASE@/$mem_base/g" \
		-e "s/@MEM_BASE_K@/$mem_base_k/g" \
		-e "s+@CLASS_DESC@+$class_desc+g" \
		< $sed_input > $sed_output
	chmod 444 $sed_output
	chown bin $sed_output
	chgrp bin $sed_output
done
echo "Script complete."
exit 0
