:
#	@(#) mfgitx.sh 1.5 92/07/24 
clear
init_range="A0000 B0000 C0000 D0000 E0000 F0000"
echo "\n\tMicrofield I8 Grafinfo File Configuration"
echo "
The I8 video card is accessed by the X server via two base I/O ports and a
64KB memory window mapped into physical PC address space.

This script expects you to know which I/O port address range the card is
currently set to use, and 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. Please note that 8-bit peripherals can interfere with 16-bit
bus accesses.

The address of the I/O ports is hardware configurable via a set of rotary
switches 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 mfgi8*.xgi in
the directory /usr/lib/grafinfo/microfield.  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 memory mapping [y]: "
read sure
if [ "$sure" = "" ]
then
	sure="y"
fi
if [ "$sure" != "y" ]
then
	# If mfgi8.xgi exists, a card hasn't been configured yet.
	if [ -f /usr/lib/grafinfo/microfield/mfgi8.xgi ]
	then
		echo "\nERROR: At least one Microfield I8 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 I8 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/microfield/mfgi8*.xgi
i=0
while [ $i -lt $count ]
do
	i=`expr $i + 1`
	echo "\nConfiguring card $i:"
	com=1
	if [ "$com" = "1" ]
	then
		def_range="D0000"
		range="$init_range"
		found=0
		while [ "$found" -eq 0 ]
		do
			echo "\tSelect a base address for the 64KB 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="2B0"
	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/microfield/mfgi8"$i".xgi"
	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/microfield/mfgi8.tms3
	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
