#!/bin/sh

# Program:  digisetup
# Author:   Troy De Jongh     troyd@digibd.com
# Date:     Nov, 1994
# Purpose:  To provide a halfway-decent interface for configuring DigiBoards
# Version:  1.0

OPTFILE=/tmp/digiopt.$$
SOURCEFILE=/usr/src/linux/drivers/char/pcxxconfig.h
DIGIMAJOR=30
DIGICUMAJOR=31

which dialog 2>&1 > /dev/null
if [ $? -ne 0 ]
then
	echo "This shell script needs the 'dialog' program."
	exit 1
fi

show() {
	if [ $NUMCARDS -eq 0 ]
	then
		dialog --msgbox "There are no boards installed." 5 40
		return
	fi
	SCRATCH1=/tmp/digi1.scr.$$
	echo "Board     Status    Altpin    Ports     I/O       RAM Addr" > $SCRATCH1
	cat $INSTALLFILE >> $SCRATCH1

	dialog --title "Press E or ESC to exit" --textbox $SCRATCH1 10 70
	rm -f $SCRATCH1
}

makeinstallfile() {
	SCRATCH=/tmp/digi.scr.$$

	INSTALLFILE=/tmp/digi2.scr.$$
	if [ $NUMCARDS -eq 0 ]
	then
		return
	fi

	grep "ENABLED" $SOURCEFILE | sed 's/,//g' > $SCRATCH
	cat $SCRATCH | awk '{printf("%-10s%-10s%-10s%-10s%-10s%-10s\n",NR,$1,$3,$4,$5,$6)}'\
				>> $INSTALLFILE
	rm -f $SCRATCH
}

removecard() {
	dialog --title "Remove Board" --menu \
		"Use SHOW to determine which board number you wish to remove" 20 75 8 \
		"1"		"Board 1" \
		"2"		"Board 2" \
		"3"		"Board 3" \
		"4"		"Board 4" \
		"5"		"Board 5" \
		"6"		"Board 6" \
		"7"		"Board 7" \
		"8"		"Abort removal" 2> $OPTFILE

		if [ $? -ne 0 ]
		then
			dialog --msgbox "Board $selection not removed." 10 40
			return
		fi

		selection=`cat $OPTFILE`

		if [ $selection -eq 8 ]
		then
			dialog --msgbox "Board $selection not removed." 10 40
			return
		fi

		if [ $selection -gt $NUMCARDS ]
		then
			dialog --msgbox "No such board exists!" 10 40
			return
		fi

		TMPFILE=/tmp/digi4.scr.$$
		cat $INSTALLFILE | sed ${selection}d > $TMPFILE
		cat $TMPFILE | awk '{printf("%-10s%-10s%-10s%-10s%-10s%-10s\n",NR,$2,$3,$4,$5,$6)}'\
> $INSTALLFILE

		NUMCARDS=`expr $NUMCARDS - 1`
		CHANGESMADE=1

		dialog --msgbox "Board $selection successfully removed." 10 40
		
}
			
writeoutchanges() {

	if [ $ORIGNUMCARDS -lt $NUMCARDS ]
	then
		dialog --yesno "Create device nodes for newly installed board(s)?" 8 40
		if [ $? -eq 0 ]
		then
			#Create devices for new cards here
			boardnum=$ORIGNUMCARDS
			while [ $boardnum -lt $NUMCARDS ]
			do
				c=1
				while [ $c -le 16 ]
				do
					name=`expr $boardnum \* 16 + $c`
					echo "Creating /dev/ttyd$name..."
					mknod /dev/ttyd$name c $DIGIMAJOR `expr $name - 1`
					mknod /dev/ttyD$name c $DIGICUMAJOR `expr $name - 1`
					c=`expr $c + 1`
				done
				boardnum=`expr $boardnum + 1`
			done
		fi
	elif [ $ORIGNUMCARDS -gt $NUMCARDS ]
	then
		dialog --yesno "Delete the device nodes for the removed board(s)?" 8 40
		if [ $? -eq 0 ]
		then
			#Remove extra devices here
			boardnum=$NUMCARDS
			while [ $boardnum -lt $ORIGNUMCARDS ]
			do
				c=1
				while [ $c -le 16 ]
				do
					name=`expr $boardnum \* 16 + $c`
					echo "Removing /dev/ttyd$name"
					rm -f /dev/tty[dD]$name
					c=`expr $c + 1`
				done
				boardnum=`expr $boardnum + 1`
			done
		fi
	fi
		
	echo "#define NUMCARDS $NUMCARDS" > $SOURCEFILE
	echo >> $SOURCEFILE
	echo "struct board_info boards[NUMCARDS]={" >> $SOURCEFILE

	awk '{printf("	%s, 0, %s, %s, %s, %s,\n", $2, $3, $4, $5, $6)}' < $INSTALLFILE >> $SOURCEFILE
	echo "};" >> $SOURCEFILE
	echo >> $SOURCEFILE
	echo '/* DO NOT HAND EDIT THIS FILE! */' >> $SOURCEFILE

	dialog --msgbox "Changes saved.  You must recompile the kernel for \
the changes to take effect." 10 60

}

installnew() {
	if [ $NUMCARDS -eq 7 ]
	then
		dialog -msgbox "You already have the maximum number (7) of \
boards installed!" 10 60
		return
	fi

	dialog --title "Install Board" --menu \
		"Select the board you wish to install" 15 50 7 \
		"1"		"PC/2e" \
		"2"		"PC/4e" \
		"3"		"PC/8e" \
		"4"		"PC/16e" \
		"5"		"PC/8i" \
		"6"		"PC/16i" \
		"7"		"Abort installation"   2> $OPTFILE

		selection=`cat $OPTFILE`

		if [ $? -ne 0 ]
		then
			return
		fi

		case $selection in
			1)
				PORTS=2
				BOARD="PC/2e"
				EIGHTK=8;;
			2)
				PORTS=4
				BOARD="PC/4e"
				EIGHTK=2;;   # 2 means maybe
			3)
				PORTS=8
				BOARD="PC/8e"
				EIGHTK=2;;
			4)
				PORTS=16
				BOARD="PC/16e"
				EIGHTK=64;;
			5)
				PORTS=8
				BOARD="PC/8i"
				EIGHTK=64;;
			6)
				PORTS=16
				BOARD="PC/16i"
				EIGHTK=64;;
			7)
				return;;
		esac

		if [ $EIGHTK -eq 2 ]
		then
			dialog --title "Board Model" --menu \
					 "The board you selected may be an older model \
with a 64K window or a newer model with an 8K window.  If you have an \
8K board, you simply have more selections to choose from in terms of RAM \
addresses.  A 64K board limits you to installing the board on only RAM \
addresses on 64K boundaries.  Please select one of the following options:" \
				20 70 4 \
				"1"		"This board is an 8K model" \
				"2"		"This board is an 64K model" \
				"3"		"I do not know the window size of this board" \
				"4"		"Abort installation"   2> $OPTFILE

			if [ $? -ne 0 ]
			then
				return
			fi

			selection=`cat $OPTFILE`
			case $selection in
				1)
					EIGHTK=8;;
				2) 
					EIGHTK=64;;
				3) 
					EIGHTK=64;;
				4)
					return;;
			esac
		fi

		if [ $EIGHTK -eq 64 ]
		then
			dialog --title "Installed RAM Address" --menu \
					 "Select the desired RAM address at which you wish \
to install your board.  Be sure the desired address is not already in \
use by another non-DigiBoard device.  DigiBoard devices can share RAM \
addresses (i.e. more than one board can be installed at 0xd0000)." \
				20 70 5 \
				"1"		"0xC0000" \
				"2"		"0xD0000" \
				"3"		"0xE0000" \
				"4"		"0xD00000" \
				"5"		"Abort installation"   2> $OPTFILE

			if [ $? -ne 0 ]
			then
				return
			fi

			selection=`cat $OPTFILE`
			case $selection in
				1)
					MEM=0xc0000;;
				2) 
					MEM=0xd0000;;
				3) 
					MEM=0xe0000;;
				4)
					MEM=0xd00000;;
				5)
					return;;
			esac
		else
			MEMSELECTED=0
			while [ $MEMSELECTED -eq 0 ]
			do
				dialog --title "Installed RAM Address" --menu \
					 	"Select the desired RAM address at which you wish \
to install your board.  Be sure the desired address is not already in \
use by another non-DigiBoard device.  DigiBoard devices can share RAM \
addresses (i.e. more than one board can be installed at 0xd0000)." \
					20 70 9 \
					"1"		"0xC0000" \
					"2"		"0xC2000" \
					"3"		"0xC4000" \
					"4"		"0xC6000" \
					"5"		"0xC8000" \
					"6"		"0xCA000" \
					"7"		"0xCC000" \
					"8"		"0xCE000" \
					"V"		"View more addresses"  2> $OPTFILE

				if [ $? -ne 0 ]
				then
					return
				fi

				selection=`cat $OPTFILE`
				MEMSELECTED=1
				case $selection in
					1)
						MEM=0xc0000;;
					2) 
						MEM=0xc2000;;
					3) 
						MEM=0xc4000;;
					4) 
						MEM=0xc6000;;
					5) 
						MEM=0xc8000;;
					6) 
						MEM=0xca000;;
					7) 
						MEM=0xcc000;;
					8)
						MEM=0xce0000;;
					V)
						MEMSELECTED=0;;
				esac

				if [ $MEMSELECTED -eq 1 ]
				then
					break
				fi

				dialog --title "Installed RAM Address" --menu \
					 	"Select the desired RAM address at which you wish \
to install your board.  Be sure the desired address is not already in \
use by another non-DigiBoard device.  DigiBoard devices can share RAM \
addresses (i.e. more than one board can be installed at 0xd0000)." \
					21 75 11 \
					"9"		"0xD0000" \
					"10"		"0xD2000" \
					"11"		"0xD4000" \
					"12"		"0xD6000" \
					"13"		"0xD8000" \
					"14"		"0xDA000" \
					"15"		"0xDC000" \
					"16"		"0xDE000" \
					"17"		"0xE0000" \
					"18"		"0xF00000" \
					"19"		"View previous addresses" 2> $OPTFILE

				if [ $? -ne 0 ]
				then
					return
				fi
	
				selection=`cat $OPTFILE`
				MEMSELECTED=1
				case $selection in
					9) 
						MEM=0xd0000;;
					10) 
						MEM=0xd2000;;
					11) 
						MEM=0xd4000;;
					12) 
						MEM=0xd6000;;
					13) 
						MEM=0xd8000;;
					14) 
						MEM=0xda000;;
					15) 
						MEM=0xdc000;;
					16) 
						MEM=0xde000;;
					17) 
						MEM=0xe0000;;
					18) 
						MEM=0xf00000;;
					19)
						MEMSELECTED=0;;
				esac
			done
		fi

		dialog --msgbox "Memory address $MEM selected" 5 40

		dialog --title "I/O Port" --menu \
					 	"Select the desired I/O port for this board.  You \
must verify that the board's switch settings correspond to this I/O address.\
  Check the user manual for dip switch settings." \
					20 60 8 \
					"1"		"0x100" \
					"2"		"0x110" \
					"3"		"0x120" \
					"4"		"0x200" \
					"5"		"0x220" \
					"6"		"0x300" \
					"7"		"0x320" \
					"8"		"Abort installation"  2> $OPTFILE

		if [ $? -ne 0 ]
		then
			return
		fi

		selection=`cat $OPTFILE`
		case $selection in
			1)
				PORT=0x100;;
			2) 
				PORT=0x110;;
			3) 
				PORT=0x120;;
			4) 
				PORT=0x200;;
			5) 
				PORT=0x220;;
			6) 
				PORT=0x300;;
			7) 
				PORT=0x320;;
			8)
				return;;
		esac

		dialog --msgbox "I/O address $PORT selected" 5 40

		dialog --title "Altpin" --menu \
					 	"The device driver supports a feature called \
Altpin.  The Altpin feature internally swaps the signals DCD and DSR.  \
This is useful for boards that are equipped with RJ-45 modular jacks for \
serial connections.  The DCD signal is normally on pin 10 of the \
RJ-45 jack, which makes it inaccessible to 8-pin RJ-45 plugs.  When the \
Altpin feature is enabled, pin 2 of the 10-pin RJ-45 connector will \
be interpreted by the device driver as the DCD signal.  This corresponds \
to pin 1 of an 8-pin RJ-45 connector.  Pin 10 then becomes DSR (and is \
inaccessible to 8-pin RJ-45 plugs).  Altpin must be enabled on DigiBoard \
ports using 8-pin RJ-45 cables, connecting to dial-in modems." \
					21 78 3 \
					"OFF"		"Disable Altpin Feature (Default)" \
					"ON"		"Enable Altpin Feature" \
					"ABORT"		"Abort installation"  2> $OPTFILE

		if [ $? -ne 0 ]
		then
			return
		fi

		selection=`cat $OPTFILE`
		case $selection in
			ON)
				ALTPIN=ON;;
			OFF) 
				ALTPIN=OFF;;
			ABORT)
				return;;
		esac

		
		dialog --yesno "You are about to install a $BOARD (${EIGHTK}K) \
at memory address $MEM, I/O address $PORT, with Altpin turned $ALTPIN.  \
Do you wish to install this board now?" 15 70
		
	if [ $? -eq 0 ]
	then
		CHANGESMADE=1
		NUMCARDS=`expr $NUMCARDS + 1`
		echo "$NUMCARDS ENABLED $ALTPIN $PORTS $PORT $MEM" \
| awk '{printf("%-10s%-10s%-10s%-10s%-10s%-10s\n",$1,$2,$3,$4,$5,$6)}'\
 >> $INSTALLFILE
	fi

}

############################ MAIN PROGRAM ##############################

NUMCARDS=`grep "define NUMCARDS" $SOURCEFILE | awk '{print $3}'`

ORIGNUMCARDS=$NUMCARDS

makeinstallfile

CHANGESMADE=0

dialog --msgbox "This setup utility may delete or create some device \
nodes.  When configuring your DigiBoard setup, please make sure \
that no one is using these devices.  This will prevent any conflicts." 10 40

if [ $? -ne 0 ]
then
	clear
	exit 0
fi

while :
do
	dialog --title "DigiBoard PC/Xe and PC/Xi Setup" --menu \
		"Select the desired option" 15 70 4 \
		"SHOW"			"Show boards currently installed" \
		"INSTALL"		"Install new board" \
		"REMOVE"		"Remove installed board" \
		"QUIT"			"Exit program"   2> $OPTFILE

	if [ $? -ne 0 ]
	then
		if [ $CHANGESMADE -eq 1 ]
		then
			dialog --yesno "Changes were made.  Do you wish to save these \
changes and have them take effect?" 10 60
			if [ $? -eq 0 ]
			then
				writeoutchanges
			else
				dialog --msgbox "Changes not saved." 10 60
			fi
		fi
				
		clear
		rm -f $INSTALLFILE $OPTFILE
		exit 0
	fi

	selection=`cat $OPTFILE`
	
	if [ "$selection" = SHOW ]
	then
		show
	fi

	if [ "$selection" = INSTALL ]
	then
		installnew
	fi

	if [ "$selection" = REMOVE ]
	then
		if [ $NUMCARDS -eq 0 ]
		then
			dialog --msgbox "There are no cards to remove!" 10 40
			continue
		fi
	
		removecard
	fi
	
	if [ "$selection" = QUIT ]
	then
		if [ $CHANGESMADE -eq 1 ]
		then
			dialog --yesno "Changes were made.  Do you wish to save these \
changes and have them take effect?" 10 60
			if [ $? -eq 0 ]
			then
				writeoutchanges
			else
				dialog --msgbox "Changes not saved." 10 60
			fi
		fi

		clear
		rm -f $INSTALLFILE $OPTFILE
		exit 0
	fi

done
