#!/bin/sh
#
# ****************************************************************************
# 
#  @(#) $Id: dgap_config,v 1.14 2004/03/08 00:27:28 scottk Exp $
# 
# ****************************************************************************
# 
#  Copyright (c) 1988-03 Digi International Inc., All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# ****************************************************************************

# Uncomment the next line if this script is ever needed to be debugged.
#set -x

# Force path to include our directory, in case we need our "dialog" binary.
PATH=/etc/dgap:$PATH

INSTALLDIR=`pwd`		# Capture original directory

# Main titles and move help.
maintitle="                  Digi International  -  Config Script"
movetitle="Move using [UP] [DOWN],[Enter] to Select"


# Check to see if the Concentrator is an 8-port 
# (i.e. C/CON-8) concentrator or not    EWS 
# Rewritten for Linux using dialog - SHK
# 
check_ccon8()
{

	# Remember that these variables are global
	eval conbaud=\$fbaud$n${l}c${c}
	this_is_ccon8=no

	if [ ! $conbaud -gt 67 ] ; then


		dialog --backtitle "$maintitle" --nocancel \
			--menu "What type of concentrator is concentrator $c?" \
		15 $detected_cols 4 \
1 "C/CON 16" \
2 "C/CON 8" \
3 "EPC/CON 16" \
4 "IBM RAN 16" \
2>/tmp/dgap.ans

		if [ $? = 1 ]
		then
			xx=1
		else
			xx=`tail -1 /tmp/dgap.ans`
		fi

		rm -f /tmp/dgap.ans

		case $xx in
                1)
			this_is_ccon8=no
			eval nports$n${l}c$c=16
			eval conctype$n${l}c$c=ccon
			;;
                2)
			this_is_ccon8=yes
			eval nports$n${l}c$c=8
			eval conctype$n${l}c$c=ccon
			;;
                3)
			this_is_ccon8=no
			eval nports$n${l}c$c=16
			eval conctype$n${l}c$c=epccon
			;;
		4)
			this_is_ccon8=no
			eval nports$n${l}c$c=16
			eval conctype$n${l}c$c=ran
                        ;;
                esac

	else # This line can not accomodate a C/X  
		eval nports$n${l}c$c=16
		eval conctype$n${l}c$c=epccon
	fi
} # End 8-port check


#
# modports(brd, line, conc, module)
#
modports()
{
	eval t1=\$nports${1}${2}c${3}m${4}
	echo $t1
}

#
# Prompts user for how many ports are on the EM module
#
portsonmod()
{
	pn=$1
	pl=$2
	pc=$3
	pm=$4

	if [ $pm = 0 ]
	then
		dialog --backtitle "$maintitle" --nocancel \
			--menu "How many ports are on EBI module $pc?" \
		15 $detected_cols 8 16 "ports" 9 "ports" 8 "ports" 4 "ports" 2>/tmp/dgap.ans
	else

		dialog --backtitle "$maintitle" --nocancel \
			--menu "How many ports are on EBI module $pm?" \
		15 $detected_cols 8 16 "ports" 9 "ports" 8 "ports" 4 "ports" 2>/tmp/dgap.ans
	fi

	if [ $? = 1 ]
	then
		xx=16
	else
		xx=`tail -1 /tmp/dgap.ans`
	fi

	rm -f /tmp/dgap.ans

	eval nports${pn}${pl}c${pc}m${pm}=$xx
}


#
# Get a concentrator/module ID letter, checking for errors
#
readid()
{
	extrastring1="\n\nThis ID can be any 1 alphabetic character or more.\n\
Use of more than 1 character allows for very descriptive names,\n\
for example \"firstfloor\" or \"copyroom\".\n\n\
Not all applications support such long tty names, so if you are unsure,\n\
stick with just 1 or 2 letters for the ID letter.\n\n"

	while :
	do

		dialog --backtitle "$maintitle" --nocancel\
			--inputbox "$1$extrastring" 20 $detected_cols 2>/tmp/dgap.tmp

		resp=`tail -1 /tmp/dgap.tmp`

		# Translate from upper -> lower.
		resp=`echo $resp | tr "[A-Z]" "[a-z]"`

		# If null, make them enter in something.
		if [ ! "$resp" ]
		then
			dialog --backtitle "$maintitle"  --msgbox \
				"\nPlease supply an id." 7 $detected_cols 2>/dev/null
			continue;
		fi

		# Get rid of all characters BUT alpha characters
		my_result=`echo "$resp" | tr -d "\000-\140" | tr -d "\173-\377"`

		if [ "$my_result" != "$resp" ]
		then
			dialog --backtitle "$maintitle"  --msgbox \
				"\nPlease supply an id with only letters." 7 $detected_cols 2>/dev/null
			continue;
		fi

		# Verify the letter/name they selected isn't already used.
		if ls /dev/${TTYNAME}${resp}[0-9][0-9]* > /dev/null 2>&1
		then
			# If its there, only because of an old config
			# of our driver, then lets us it.
			if ls /dev/dg/dgap/${TTYNAME}${resp}[0-9][0-9]* > /dev/null 2>&1
			then
				name_accepted=1
			else
				name_accepted=0
			fi
		else
			name_accepted=1
		fi

		if [ $name_accepted != 1 ]
		then
			dialog --backtitle "$maintitle"  --msgbox \
				"\nThe id \"${TTYNAME}${resp}\" is already in use in /dev, please select a different id." \
				7 $detected_cols 2>/dev/null
			continue;
		fi

		# check how many times this id has been used
		eval tmp="\$IDCOUNT${resp}"
		if [ -z "$tmp" ]
		then
			tmp=1
		else
			if [ "$tmp" -ge $LCONC ]
			then

				dialog --backtitle "$maintitle"  --msgbox \
"Sorry, that id has already been used $LCONC times.\n\
Please choose a different one.\n\
Available id's are [$IDLIST2].\n\
Please choose one." 14 $detected_cols 2>/dev/null

				continue;
			fi
			tmp=`expr "$tmp" + 1`
		fi
		eval IDCOUNT${resp}=$tmp
		break
	done
	id1=$resp
	id2=$tmp
}


#
# Get next available extend concid from string
#
nextid()
{
	# get the next id from the list
	if [ "$IDLIST1" = " " ]
	then
		# used all ids, start over
		IDLIST1="$IDLIST"
	fi
	id1=`expr "$IDLIST1" : " *\([^ ]*\) "`
	# remove it 
	IDLIST1=`expr "${IDLIST1}" : " *[^ ]*\( .*\)"`

	# count how many times this id has been used
	eval tmp="\$IDCOUNT${id1}"
	if [ -z "$tmp" ]
	then
		tmp=1
	else
		tmp=`expr "$tmp" + 1`
	fi
	eval IDCOUNT${id1}=$tmp
	id2=$tmp
}


#
# epc line configuration
#
epcconf()
{
	# save state
	tmpid1="$IDLIST1"
	tmpid2="$IDLIST2"

	dialog --backtitle "$maintitle" \
--yesno "Configuring adapter ${n} (${BRDNAME}).\n\n\
Line Configuration.\n\n\
The sync lines may be configured two ways.\n\nThe basic method has all \
connections wired \"8 wire direct\" at a speed of either 1228K bps or 10M bps, \
and can be used for quick set-up for most situations.\n\n\
The custom method allows you to choose speeds and wiring methods individually.\n\n\
Do you want to use the basic installation method?" \
18 $detected_cols 2>/dev/null

	if [ $? = 0 ]
	then
		# yes - do basic configuration

		dialog --backtitle "$maintitle" --nocancel \
			--menu "Configuring adapter ${n} (${BRDNAME}).\nBasic Configuration\n\n\
One of two line speeds must be chosen for the basic configuration \
method.  The choices are 1228K bps and 10M bps.\nRunning the sync \
lines at 1228K bps maintains compatibility with C/CON-16 and C/CON-8 \
concentrators.\nRunning the sync lines at 10M bps allows higher \
performance, but is not compatible with C-CON-16 and C-CON-8 \
concentrators and restricts the length of the sync cable.\nFor more \
information, see the manual.\n\n\
Which speed do you wish to run the sync lines at? " \
21 $detected_cols 2 2 "10M bps" 1 "1228K bps" 2>/tmp/dgap.ans

		if [ $? = 1 ]
		then
			ans=2
		else
			ans=`tail -1 /tmp/dgap.ans`
		fi

		rm -f /tmp/dgap.ans

		case $ans in
		1)
			syncbaud=14
			;;
		2)
			syncbaud=74
			;;
		esac


		avail=14
		l=1

		while [ $l -le 2 ]
		do

			# If we can't handle any more, bail outta loop
			if [ $avail -le 0 ]
			then
				eval nconc$n$l=0
				eval baud$n${l}c0=$syncbaud
				eval fbaud$n${l}c0=$syncbaud
				eval nports$n${l}c0=0
				break
			fi

			# Limit number of concs on a line to 8.
			if [ $avail -lt 8 ]
			then
				mc=$avail
			else
				mc=8
			fi

			tmpi=0

			rm -f /tmp/dgap.tmp 

			while [ $tmpi -le $mc ]
			do
				if [ $tmpi -eq 1 ]
				then
					printf " 1 concentrator" >> /tmp/dgap.tmp
				else
					printf " $tmpi concentrators" >> /tmp/dgap.tmp
				fi   
				tmpi=`expr $tmpi + 1`
			done

			string=`cat /tmp/dgap.tmp`
			rm -f /tmp/dgap.tmp   

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $string 2>/tmp/dgap.tmp

			if [ $? = 1 ]
			then
				sel=0
			else
				sel=`tail -1 /tmp/dgap.tmp`
			fi

			rm -f /tmp/dgap.tmp

			# Figure out how many they selected.
			case "$sel" in
			0)
				eval nconc$n$l=0
				eval baud$n${l}c0=$syncbaud
				eval fbaud$n${l}c0=$syncbaud
				eval nports$n${l}c0=0
				;;
			[1-$mc])
				avail=`expr "$avail" - "$sel"`
				eval nconc${n}${l}="$sel"
				eval baud${n}${l}c0=$syncbaud
				eval fbaud${n}${l}c0=$syncbaud
				eval nports${n}${l}c0=0
				c=1
				while [ "$c" -le "$sel" ]
				do
					eval baud$n${l}c$c=$syncbaud
					eval fbaud$n${l}c$c=$syncbaud
					eval nports$n${l}c$c=16

					if [ $AUTOIDS = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2

					[ $id2 = 1 ] && tmpid="$tmpid $id1"

					if [ $avail -lt 3 ]
					then
						mm=$avail
					else
						mm=3
					fi
					if [ "$mm" -gt 0 -a "$this_is_ccon8" = "no" ]
					then
						tmpi=0
						rm -f /tmp/dgap.tmp 

						while [ $tmpi -le $mm ]
						do
							if [ $tmpi -eq 1 ]
							then
								printf " 1 module" >> /tmp/dgap.tmp
							else
								printf " $tmpi modules" >> /tmp/dgap.tmp
							fi
							tmpi=`expr $tmpi + 1`
						done

						string2=`cat /tmp/dgap.tmp`
						rm -f /tmp/dgap.tmp

						dialog --backtitle "$maintitle" --nocancel \
							--menu "How many EBI modules are \
connected to concentrator $c?" 12 $detected_cols 5 $string2 2>/tmp/dgap.tmp

						if [ $? = 1 ]
						then
							nm=0
						else
							nm=`tail -1 /tmp/dgap.tmp`
						fi

						rm -f /tmp/dgap.tmp

						case "$nm" in
						0)
							eval nmod${n}${l}c${c}=0
							;;
						[1-$mm])
							avail=`expr "$avail" - "$nm"`
							eval nmod${n}${l}c${c}=$nm
							m=1
							while [ $m -le $nm ]
							do
								eval nports$n${l}c${c}m$m=16
								portsonmod $n $l $c $m
								if [ $AUTOIDS = "no" ]
								then
									readid "Please select an ID letter of module $m"
								else
									nextid
								fi
								eval id$n${l}c${c}m$m=$id1
								eval idn$n${l}c${c}m$m=$id2

								[ $id2 -eq 1 ] && tmpid="$tmpid $id1"

								m=`expr $m + 1`
							done
							;;
						esac
					else
						eval nmod${n}${l}c${c}=0
					fi
					c=`expr $c + 1`
				done
				;;
			esac

			l=`expr $l + 1`
		done
	else
		#
		# Custom Configuration
		#

		avail=14
		l=1


		while [ $l -le 2 ]
		do

			# If we can't handle any more, bail outta loop
			if [ $avail -le 0 ]
			then
				eval nconc$n$l=0
				eval baud$n${l}c0=14
				eval fbaud$n${l}c0=14
				eval nports$n${l}c0=0
				break
			fi

			# Limit number of concs on a line to 8.
			if [ $avail -lt 8 ]
			then
				mc=$avail
			else
				mc=8
			fi

			tmpi=0

			rm -f /tmp/dgap.tmp2

			while [ $tmpi -le $mc ]
			do
				if [ $tmpi -eq 1 ]
				then
					printf " 1 concentrator" >> /tmp/dgap.tmp2
				else
					printf " $tmpi concentrators" >> /tmp/dgap.tmp2
				fi   
				tmpi=`expr $tmpi + 1`
			done    

			concstringtemp=`cat /tmp/dgap.tmp2`
			rm -f /tmp/dgap.tmp2

			dialog --backtitle "$maintitle" --nocancel \
				--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\nPlease select the cabling option used on line $l\n\n" \
15 $detected_cols 4 \
1 "4 wire direct" \
2 "8 wire direct (default)" \
3 "8 wire synchronous modem" \
4 "RS-232 synchronous modem" \
2>/tmp/dgap.ans


			if [ $? = 1 ]
			then
				sel=2
			else
				sel=`tail -1 /tmp/dgap.ans`
			fi

			rm -f /tmp/dgap.ans

			case "$sel" in
			"")
				option=2
				;;
			[1-4])
				option="$sel"
				;;
			esac

			#
			# Direct 4
			#
			if [ "$option" = 1 ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0 
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
					;;
					[1-$mc])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
15 $detected_cols 4 \
4 "1843K bits/sec  *Not compatible with C/CON-8/16 concentrators*" \
3 "921K  bits/sec  *Not compatible with C/CON-8/16 concentrators*" \
2 "460K  bits/sec" \
1 "230K  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=2
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-2])
					baud="$sel"
					;;
				3)
					baud=67
					;;
				4)
					baud=69
					;;
				esac

				eval nconc=\$nconc$n$l
				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud
				eval nports$n${l}c0=0
				c=1
				while [ $c -le $nconc ]
				do
					eval baud$n${l}c$c=$baud
					eval fbaud$n${l}c$c=$baud
					eval nports$n${l}c$c=16

					if [ $AUTOIDS = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2

					[ $id2 = 1 ] && tmpid="$tmpid $id1"

					if [ $avail -lt 3 ]
					then
						mm=$avail
					else
						mm=3
					fi

					if [ "$mm" -gt 0 -a "$this_is_ccon8" = "no" ]
					then

						tmpi=0
						rm -f /tmp/dgap.tmp 

						while [ $tmpi -le $mm ]
						do
							if [ $tmpi -eq 1 ]
							then
								printf " 1 module" >> /tmp/dgap.tmp
							else
								printf " $tmpi modules" >> /tmp/dgap.tmp
							fi
							tmpi=`expr $tmpi + 1`
						done

						string2=`cat /tmp/dgap.tmp`
						rm -f /tmp/dgap.tmp

						dialog --backtitle "$maintitle" --nocancel \
							--menu "How many EBI modules are \
connected to concentrator $c?" 12 $detected_cols 5 $string2 2>/tmp/dgap.tmp

						if [ $? = 1 ]
						then
							nm=0
						else
							nm=`tail -1 /tmp/dgap.tmp`
						fi

						rm -f /tmp/dgap.tmp

						case "$nm" in
						0)
							eval nmod${n}${l}c${c}=0
							;;
						[1-$mm])
							avail=`expr "$avail" - "$nm"`
							eval nmod${n}${l}c${c}=$nm
							m=1
							while [ $m -le $nm ]
							do
								eval nports${n}${l}c${c}m$m=16
								portsonmod $n $l $c $m
								if [ $AUTOIDS = "no" ]
								then
									readid "Please select an ID letter of module $m"
								else
									nextid
								fi
								eval id$n${l}c${c}m$m=$id1
								eval idn$n${l}c${c}m$m=$id2
								m=`expr $m + 1`
							done
							;;
						esac
					else
						eval nmod${n}${l}c${c}=0
					fi
					c=`expr $c + 1`
				done
			fi
			
			#
			# Direct 8
			#
			if [ "$option" = 2 ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0 
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$mc])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done


				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
22 $detected_cols 13 \
17 "10M   bits/sec   (Not compatible with C/CON-8/16 concentrators)" \
16 "7373K bits/sec   (Not compatible with C/CON-8/16 concentrators)" \
15 "3686K bits/sec   (Not compatible with C/CON-8/16 concentrators)" \
14 "2458K bits/sec   (Not compatible with C/CON-8/16 concentrators)" \
13 "1843K bits/sec   (Not compatible with C/CON-8/16 concentrators)" \
12 "1228K bits/sec   (Default)" \
11 "921K  bits/sec" \
10 "460K  bits/sec" \
9 "230K  bits/sec" \
8 "115K  bits/sec" \
7 "76.8K bits/sec" \
6 "57.6K bits/sec" \
5 "38.4K bits/sec" \
4 "19.2K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=12
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-9]|10|11|12)
					baud=`expr "$sel" + 2`
					;;
				1[3-7])
					baud=`expr "$sel" + 57`
					;;
				"")
					baud=14
					;;
				esac

				eval nconc=\$nconc$n$l
				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud
				eval nports$n${l}c0=0
				c=1

				while [ $c -le $nconc ]
				do
					eval baud$n${l}c$c=$baud
					eval fbaud$n${l}c$c=$baud
					eval nports$n${l}c$c=16

					if [ $AUTOIDS = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2

					[ $id2 = 1 ] && tmpid="$tmpid $id1"

					if [ $avail -lt 3 ]
					then
						mm=$avail
					else
						mm=3
					fi
					if [ "$mm" -gt 0 -a "$this_is_ccon8" = "no" ]
					then

						tmpi=0
						rm -f /tmp/dgap.tmp 

						while [ $tmpi -le $mm ]
						do
							if [ $tmpi -eq 1 ]
							then
								printf " 1 module" >> /tmp/dgap.tmp
							else
								printf " $tmpi modules" >> /tmp/dgap.tmp
							fi
							tmpi=`expr $tmpi + 1`
						done

						string2=`cat /tmp/dgap.tmp`
						rm -f /tmp/dgap.tmp

						dialog --backtitle "$maintitle" --nocancel \
							--menu "How many EBI modules are \
connected to concentrator $c?" 12 $detected_cols 5 $string2 2>/tmp/dgap.tmp

						if [ $? = 1 ]
						then
							nm=0
						else
							nm=`tail -1 /tmp/dgap.tmp`
						fi

						rm -f /tmp/dgap.tmp

						case "$nm" in
						0)
							eval nmod${n}${l}c${c}=0
							;;
						[1-$mm])
							avail=`expr "$avail" - "$nm"`
							eval nmod${n}${l}c${c}=$nm
							m=1
							while [ $m -le $nm ]
							do
								eval nports$n${l}c${c}m$m=16
								portsonmod $n $l $c $m
								if [ $AUTOIDS = "no" ]
								then
									readid "Please select an ID letter of module $m"
								else
									nextid
								fi
								eval id$n${l}c${c}m$m=$id1
								eval idn$n${l}c${c}m$m=$id2
								m=`expr $m + 1`
							done
							;;
						esac
					else
						eval nmod${n}${l}c${c}=0
					fi
					c=`expr $c + 1`
				done
			fi

			#
			# Custom 8 (Sync modem)
			#
			if [ "$option" = 3 ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0 
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp
			
				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$mc])
						avail=`expr $avail - $sel`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
22 $detected_cols 11 \
13 "1228K bits/sec (default)" \
12 "921K  bits/sec" \
11 "460K  bits/sec" \
10 "230K  bits/sec" \
9 "115K  bits/sec" \
8 "76.8K bits/sec" \
7 "57.6K bits/sec" \
6 "38.4K bits/sec" \
5 "19.2K bits/sec" \
4 "14.4K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=13
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-3])
					baud=`expr $sel + 14`
					baud2=`expr $sel + 2`
					;;
				4)
					baud=33
					baud2=6
					;;
				[5-9]|1[0123])
					baud=`expr $sel + 13`
					baud2=`expr $sel + 1`
					;;
				"")
					baud=26
					baud2=14
					;;
				esac

				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud

				eval nports$n${l}c0=0
				
				eval nconc=\$nconc$n$l
				c=1
				while [ $c -lt $nconc ]
				do
					eval nports$n${l}c$c=16

					if [ "$baud2" -lt 10 ]
					then
						eval baud$n${l}c$c=10
					else
						eval baud$n${l}c$c=$baud2
					fi
					eval fbaud$n${l}c$c=$baud2

					c=`expr $c + 1`
				done
				eval nports$n${l}c$c=16
				eval baud$n${l}c$c=$baud
				eval fbaud$n${l}c$c=$baud

				c=1
				while [ $c -le $nconc ]
				do
					if [ $AUTOIDS = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2

					[ $id2 = 1 ] && tmpid="$tmpid $id1"

					if [ $avail -lt 3 ]
					then
						mm=$avail
					else
						mm=3
					fi
					if [ "$mm" -gt 0 -a "$this_is_ccon8" = "no" ]
					then

						tmpi=0
						rm -f /tmp/dgap.tmp 

						while [ $tmpi -le $mm ]
						do
							if [ $tmpi -eq 1 ]
							then
								printf " 1 module" >> /tmp/dgap.tmp
							else
								printf " $tmpi modules" >> /tmp/dgap.tmp
							fi
							tmpi=`expr $tmpi + 1`
						done

						string2=`cat /tmp/dgap.tmp`
						rm -f /tmp/dgap.tmp

						dialog --backtitle "$maintitle" --nocancel \
							--menu "How many EBI modules are \
connected to concentrator $c?" 12 $detected_cols 5 $string2 2>/tmp/dgap.tmp

						if [ $? = 1 ]
						then
							nm=0
						else
							nm=`tail -1 /tmp/dgap.tmp`
						fi

						rm -f /tmp/dgap.tmp

						case "$nm" in
						0)
							eval nmod${n}${l}c${c}=0
							;;
						[1-$mm])
							avail=`expr "$avail" - "$nm"`
							eval nmod${n}${l}c${c}=$nm
							m=1
							while [ $m -le $nm ]
							do
								eval nports$n${l}c${c}m$m=16
								portsonmod $n $l $c $m
								if [ $AUTOIDS = "no" ]
								then
									readid "Please select an ID letter of module $m"
								else
									nextid
								fi
								eval id$n${l}c${c}m$m=$id1
								eval idn$n${l}c${c}m$m=$id2
								m=`expr $m + 1`
							done
							;;
						esac
					else
						eval nmod${n}${l}c${c}=0
					fi
					c=`expr $c + 1`
				done
			fi

			#
			# RS-232 modem
			#
			if [ "$option" = 4 ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0 
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$mc])
						avail=`expr $avail - $sel`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
22 $detected_cols 9 \
9 "76.8K bits/sec" \
8 "64.1K bits/sec" \
7 "57.6K bits/sec" \
6 "38.4K bits/sec" \
5 "19.2K bits/sec" \
4 "14.4K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=12
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-3])
					baud=`expr $sel + 34`
					baud2=`expr $sel + 2`
					;;
				4)
					baud=38
					baud2=6
					;;
				[5-7])
					baud=`expr $sel + 34`
					baud2=`expr $sel + 1`
					;;
				8)
					baud=42
					baud2=9
					;;
				9)
					baud=43
					baud2=9
					;;
				esac

				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud

				eval nports$n${l}c0=0
				
				eval nconc=\$nconc$n$l
				c=1
				while [ $c -lt $nconc ]
				do
					eval nports$n${l}c$c=16

					if [ "$baud2" -lt 10 ]
					then
						eval baud$n${l}c$c=10
					else
						eval baud$n${l}c$c=$baud2
					fi
					eval fbaud$n${l}c$c=$baud2

					c=`expr $c + 1`
				done
				eval nports$n${l}c$c=16
				eval baud$n${l}c$c=$baud
				eval fbaud$n${l}c$c=$baud


				c=1
				while [ $c -le $nconc ]
				do
					if [ $AUTOIDS = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2

					[ $id2 = 1 ] && tmpid="$tmpid $id1"

					if [ $avail -lt 3 ]
					then
						mm=$avail
					else
						mm=3
					fi
					if [ "$mm" -gt 0 -a "$this_is_ccon8" = "no" ]
					then


						tmpi=0
						rm -f /tmp/dgap.tmp 

						while [ $tmpi -le $mm ]
						do
							if [ $tmpi -eq 1 ]
							then
								printf " 1 module" >> /tmp/dgap.tmp
							else
								printf " $tmpi modules" >> /tmp/dgap.tmp
							fi
							tmpi=`expr $tmpi + 1`
						done

						string2=`cat /tmp/dgap.tmp`
						rm -f /tmp/dgap.tmp

						dialog --backtitle "$maintitle" --nocancel \
							--menu "How many EBI modules are \
connected to concentrator $c?" 12 $detected_cols 5 $string2 2>/tmp/dgap.tmp

						if [ $? = 1 ]
						then
							nm=0
						else
							nm=`tail -1 /tmp/dgap.tmp`
						fi

						rm -f /tmp/dgap.tmp

						case "$nm" in
						0)
							eval nmod${n}${l}c${c}=0
							;;
						[1-$mm])
							avail=`expr "$avail" - "$nm"`
							eval nmod${n}${l}c${c}=$nm
							m=1
							while [ $m -le $nm ]
							do
#								eval nports$n${l}c${c}m$m=16
								portsonmod $n $l $c $m
								if [ $AUTOIDS = "no" ]
								then
									readid "Please select an ID letter of module $m"
								else
									nextid
								fi

								eval id$n${l}c${c}m$m=$id1
								eval idn$n${l}c${c}m$m=$id2
								m=`expr $m + 1`
							done
							;;
						esac
					else
						eval nmod${n}${l}c${c}=0
					fi
					c=`expr $c + 1`
				done
			fi
			l=`expr $l + 1`
		done
	fi

	iostr=""
	mstr=""


	printf "You have selected the following configuration for adapter ${n}:\n" > /tmp/dgap.tmp
	printf "Adapter Type: ${BRDNAME}.${iostr}${mstr}\n" >> /tmp/dgap.tmp
	printf "\nConnections           Speed   Cabling         Port Names\n\
----------------------------------------------------------------" >> /tmp/dgap.tmp

	l=1
	while [ $l -le 2 ]
	do
		printf "\nLine $l"  >> /tmp/dgap.tmp
		eval nconc=\$nconc$n$l
		c=0
		while [ $c -lt $nconc ]
		do
			conc=`expr $c + 1`
			printf "\n    Concentrator $conc\t" >> /tmp/dgap.tmp

			eval baud=\$fbaud$n${l}c$c

			eval id=\$id$n${l}c$conc
			eval pn=\$idn$n${l}c$conc

			p1=`expr \( $pn - 1 \) \* 16 + 1`
			eval concsize=\$nports${n}${l}c$conc
			p2=`expr $p1 + $concsize - 1`

			p1=`expr "00$p1" : ".*\(..\)$"`
			p2=`expr "00$p2" : ".*\(..\)$"`
			pnames="${TTYNAME}${id}${p1} - ${TTYNAME}${id}${p2}"

			case ${baud} in
			1)	printf "230K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			2)	printf "460K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			3)	printf "2400\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			4)	printf "4800\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			5)	printf "9600\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			6)	printf "19.2K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			7)	printf "38.4K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			8)	printf "57.6K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			9)	printf "76.8K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			10)	printf "115K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			11)	printf "230K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			12)	printf "460K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			13)	printf "921K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			14)	printf "1228K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			15)	printf "2400\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			16)	printf "4800\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			17)	printf "9600\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			18)	printf "19.2K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			19)	printf "38.4K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			20)	printf "57.6K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			21)	printf "76.8K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			22)	printf "115K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			23)	printf "230K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			24)	printf "460K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			25)	printf "921K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			26)	printf "1228K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			33)	printf "14.4K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			35)	printf "2400\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			36)	printf "4800\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			37)	printf "9600\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			38)	printf "14.4K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			39)	printf "19.2K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			40)	printf "38.4K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			41)	printf "57.6K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			42)	printf "64.1K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			43)	printf "76.8K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			60)	printf "115K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			61)	printf "115K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			62)	printf "230K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			63)	printf "230K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			64)	printf "460K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			65)	printf "460K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			66)	printf "921K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			67)	printf "921K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			68)	printf "1843K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			69)	printf "1843K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			70)	printf "1843K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			71)	printf "2458K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			72)	printf "3686K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			73)	printf "7373K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			74)	printf "10M\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			*)  printf "\tcase got $1           " >> /tmp/dgap.tmp
				;;
			esac

			eval nm=\$nmod${n}${l}c${conc}
			if [ "$nm" != 0 ]
			then

				m=1
				while [ $m -le $nm ]
				do
					printf "\n" >> /tmp/dgap.tmp

					eval id=\$id$n${l}c${conc}m$m
					eval pn=\$idn$n${l}c${conc}m$m
					p1=`expr \( "$pn" - 1 \) \* 16 + 1`
#-					p2=`expr "$p1" + 15`

					m1=`modports ${n} ${l} ${conc} ${m}`
					p2=`expr "$p1" + $m1 - 1`

					p1=`expr "00$p1" : ".*\(..\)$"`
					p2=`expr "00$p2" : ".*\(..\)$"`
					pnames="${TTYNAME}${id}${p1} - ${TTYNAME}${id}${p2}"

					printf "\t   EBI Module $m\t"  >> /tmp/dgap.tmp
					printf "              \t\t$pnames"  >> /tmp/dgap.tmp
					m=`expr $m + 1`
				done

			fi

			c=`expr $c + 1`
		done

		l=`expr $l + 1`
	done

	printf "\n\n\tIs this configuration acceptable?"  >> /tmp/dgap.tmp

	output=`cat /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp

	if [ $collapse_supported -eq 1 ]
	then
		dialog --no-collapse --backtitle "$maintitle" --yesno "$output" $detected_lines $detected_cols 2>/dev/null
        else
		dialog --tab-correct --backtitle "$maintitle" --yesno "$output" $detected_lines $detected_cols 2>/dev/null
	fi

	if [ $? = 0 ]
	then
		# yes
		return 0
	else
		# no
		IDLIST1="$tmpid1"
		IDLIST2="$tmpid2"
		l=1
		while [ $l -le 2 ]
		do
			eval nconc=\$nconc$n$l
			c=1
			while [ $c -le $nconc ]
			do
				eval id=\$id$n${l}c$c
				eval pn=\$IDCOUNT$id
				pn=`expr $pn - 1`
				eval IDCOUNT$id=$pn

				eval nm=\$nmod${n}${l}c$c
				m=1
				while [ $m -le $nm ]
				do
					eval id=\$id$n${l}c${c}m$m
					eval pn=\$IDCOUNT$id
					pn=`expr $pn - 1`
					eval IDCOUNT$id=$pn

					m=`expr $m + 1`
				done
				c=`expr $c + 1`
			done
			l=`expr $l + 1`
		done

		return 1
	fi

}	# end of epcconf



#
# Configuration for Digi Xr 
#
xrconf()
{
	# save state
	tmpid1="$IDLIST1"
	tmpid2="$IDLIST2"
	typemsg="Digi Xr:"

	nconc=1
	eval nconc${n}0="1"

	i=1

	if [ "$AUTOIDS" = "no" ]
	then
		readid "Please select an ID letter for module $i"
	else
		nextid
	fi

	eval id${n}0c${i}=$id1
	eval idn${n}0c${i}=$id2

	pn=$n
	pl=0
	pc=1
	pm=0

	if [ "${BRDNAME}" = "IBM 8-Port Asynchronous PCI Host Adapter" ]
	then
		xx=8
	else
		dialog --backtitle "$maintitle" --nocancel \
			--menu "How many ports are on the ${BRDNAME}?\n\n" \
			12 $detected_cols 3 8 "ports" 4 "ports" 2 "ports" 2>/tmp/dgap.ans

		if [ $? = 1 ]
		then
			xx=8 
		else
			xx=`tail -1 /tmp/dgap.ans`
		fi

		rm -f /tmp/dgap.ans
	fi

	eval nports${pn}${pl}c${pc}m${pm}=$xx

	if [ "${BRDNAME}" = "AccelePort Xr 920 PCI Host Adapter" ]
	then
		eval CONFIGBRDNAME="Digi_AccelePort_\${xx}r_920_PCI"
	else
		eval CONFIGBRDNAME="Digi_AccelePort_\${xx}r_PCI\(EIA-232/RS-422\)"
	fi


	# For now, lets leave this disabled.
	# The interrupt code works in the driver, but I think its best
	# if people were forced to turn it on manually in the config file...

#	dialog --backtitle "$maintitle" \
#		--yesno "In order to reduce response time to small packets (latency),\n\
#it may be helpful to enable interrupts on the adapter.\n\
#However, doing this will significantly increase driver CPU usage \
#on your Linux system.\n\n\
#By default, interrupts are disabled.\n\
#To enable interrupts, answer no.\n\n\
#Do you want to keep interrupts disabled on the adapter?" \
#14 $detected_cols 2>/dev/null
#
#	if [ $? = 1 ]
#	then
#		eval useint${n}=1
#	else
#		eval useint${n}=0
#	fi


	if [ $id2 -eq 1 ]
	then
		tmpid="$tmpid $id1"
	fi

	iostr=""
	mstr=""

	printf "You have selected the following configuration for adapter ${n}:\n\n" > /tmp/dgap.tmp
	printf "Adapter Type: ${BRDNAME}.${iostr}${mstr}

Module\tPort Names
-----------------------------------------" >>/tmp/dgap.tmp

	PORTS_PER_MOD=16
	
	i=1
	while [ "$i" -le $nconc ]
	do
		eval id=\$id${n}0c${i}
		eval pn=\$idn${n}0c${i}
		p1=`expr \( "$pn" - 1 \) \* $PORTS_PER_MOD + 1`
		string=`modports $n 0 $i 0`
		p2=`expr "$p1" + "$string" - 1`
		# add the leading zero to p1 if necessary
		p1=`expr "00$p1" : ".*\(..\)$"`
		p2=`expr "00$p2" : ".*\(..\)$"`
		string="${TTYNAME}${id}${p1} - ${TTYNAME}${id}${p2}"
		printf "\n$i\t\t$string\n" >> /tmp/dgap.tmp
		i=`expr "$i" + 1`
	done

	printf "\n\nIs this configuration acceptable?" >> /tmp/dgap.tmp

	output=`cat /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp

	if [ $collapse_supported -eq 1 ]
	then
		dialog --no-collapse --backtitle "$maintitle" --yesno "$output" 15 $detected_cols 2>/dev/null
	else
		dialog --tab-correct --backtitle "$maintitle" --yesno "$output" 15 $detected_cols 2>/dev/null
	fi

	if [ $? = 0 ]
	then
		# yes
		return 0
	else
		# no
		# restore state of ID allocation
		IDLIST1="$tmpid1"
		IDLIST2="$tmpid2"
		i=1
		while [ $i -le $nconc ]
		do
			eval id=\$id${n}0c${i}
			eval pn=\$IDCOUNT$id
			pn=`expr "$pn" - 1`
			eval IDCOUNT$id=$pn

			i=`expr $i + 1`
		done
		return 1
	fi
}


#
# Configuration for PC/Xem
#
emconf()
{
	# save state
	tmpid1="$IDLIST1"
	tmpid2="$IDLIST2"


	dialog --backtitle "$maintitle" --nocancel \
		--menu "The ${BRDNAME} can have 1 to 4 EBI modules\n
connected, for a total of 8 to 64 ports.\n\nHow many modules are connected to the adapter?" \
		15 $detected_cols 4 1 "module" 2 "modules" 3 "modules" 4 "modules" 2>/tmp/dgap.ans

	if [ $? = 1 ]
	then
		sel=1
	else
		sel=`tail -1 /tmp/dgap.ans`
	fi

	rm -f /tmp/dgap.ans

	nconc=$sel
	eval nconc${n}0="${sel}"

	i=1
	while [ "$i" -le $nconc ]
	do
		if [ "$AUTOIDS" = "no" ]
		then
			readid "Please select an ID letter for module $i"
		else
			nextid
		fi
		eval id${n}0c${i}=$id1
		eval idn${n}0c${i}=$id2
		portsonmod $n 0 $i 0
		if [ $id2 -eq 1 ]
		then
			tmpid="$tmpid $id1"
		fi
		i=`expr "$i" + 1`

		printf "SCOTT: $i\n"
	done

	iostr=""
	mstr=""

	printf "You have selected the following configuration for adapter ${n}:\n" > /tmp/dgap.tmp
	printf "Adapter Type: ${BRDNAME}.${iostr}${mstr}

Module	Port Names
---------------------------\n" >>/tmp/dgap.tmp

	i=1
	while [ "$i" -le $nconc ]
	do
		eval id=\$id${n}0c${i}
		eval pn=\$idn${n}0c${i}
		p1=`expr \( "$pn" - 1 \) \* 16 + 1`
		string=`modports $n 0 $i 0`
		p2=`expr "$p1" + "$string" - 1`
		# add the leading zero to p1 if necessary
		p1=`expr "00$p1" : ".*\(..\)$"`
		p2=`expr "00$p2" : ".*\(..\)$"`
		string="${TTYNAME}${id}${p1} - ${TTYNAME}${id}${p2}"
		printf "	$i	$string\n" >> /tmp/dgap.tmp
		i=`expr "$i" + 1`
	done

	printf "Is this configuration acceptable?" >> /tmp/dgap.tmp

	output=`cat /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp

	dialog --backtitle "$maintitle" --yesno "$output" 15 $detected_cols 2>/dev/null

	if [ $? = 0 ]
	then
		# yes
		return 0
	else
		# no
		# restore state of ID allocation
		IDLIST1="$tmpid1"
		IDLIST2="$tmpid2"
		i=1
		while [ $i -le $nconc ]
		do
			eval id=\$id${n}0c${i}
			eval pn=\$IDCOUNT$id
			pn=`expr "$pn" - 1`
			eval IDCOUNT$id=$pn

			i=`expr $i + 1`
		done
		return 1
	fi
}

#
# cx line configuration
#
cxconf()
{
	# save state
	tmpid1="$IDLIST1"
	tmpid2="$IDLIST2"


	dialog --backtitle "$maintitle" \
--yesno "Configuring adapter ${n} (${BRDNAME}).\n\n\
Line Configuration.\n\n\
The sync lines may be configured two ways.\n\nThe basic method has all \
connections wired \"8 wire direct\" at a speed of 1228K bps, and can \
be used for quick set-up for most situations.\n\nThe custom method allows \
you to choose speeds and wiring methods individually.\n\n\
Do you want to use the basic installation method?" \
18 $detected_cols 2>/dev/null

	if [ $? = 0 ]
	then
		# yes - do basic configuration

		avail=8
		l=1


		while [ $l -le 2 ]
		do

			# If we can't handle any more, bail outta loop
			if [ $avail -le 0 ]
			then
				eval nconc$n$l=0
				eval baud$n${l}c0=14
				eval fbaud$n${l}c0=14
				eval nports$n${l}c0=0
				break
			fi

			tmpi=0

			rm -f /tmp/dgap.tmp

			while [ $tmpi -le $avail ]
			do
				if [ $tmpi -eq 1 ]
				then
					printf " 1 concentrator" >> /tmp/dgap.tmp
				else
					printf " $tmpi concentrators" >> /tmp/dgap.tmp
				fi
				tmpi=`expr $tmpi + 1`
			done

			string=`cat /tmp/dgap.tmp`
			rm -f /tmp/dgap.tmp

			dialog --backtitle "$maintitle" --nocancel \
				--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $string 2>/tmp/dgap.tmp

			if [ $? = 1 ]
			then
				sel=0
			else
				sel=`tail -1 /tmp/dgap.tmp`
			fi

			rm -f /tmp/dgap.tmp

			case "$sel" in
			0)
				eval nconc$n$l=0
				eval baud$n${l}c0=14
				eval fbaud$n${l}c0=14
				eval nports$n${l}c0=0
				;;
			[1-$avail])
				avail=`expr "$avail" - "$sel"`
				eval nconc${n}${l}="$sel"
				eval baud${n}${l}c0=14
				eval fbaud${n}${l}c0=14
				eval nports${n}${l}c0=0
				c=1
				while [ "$c" -le "$sel" ]
				do
					eval baud$n${l}c$c=14
					eval fbaud$n${l}c$c=14
					eval nports$n${l}c$c=16
					if [ "$AUTOIDS" = "no" ]
					then
						readid "Please select an ID letter for concentrator $c:"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2
					if [ $id2 -eq 1 ]
					then
						tmpid="$tmpid $id1"
					fi
					c=`expr $c + 1`
				done
				;;
			esac

			l=`expr $l + 1`
		done

	else

		avail=8
		l=1
		while [ $l -le 2 ]
		do

			# If we can't handle any more, bail outta loop
			if [ $avail -le 0 ]
			then
				eval nconc$n$l=0
				eval baud$n${l}c0=14
				eval fbaud$n${l}c0=14
				eval nports$n${l}c0=0
				break
			fi

			tmpi=0

			rm -f /tmp/dgap.tmp2

			while [ $tmpi -le $avail ]
			do
				if [ $tmpi -eq 1 ]
				then
					printf " 1 concentrator" >> /tmp/dgap.tmp2
				else
					printf " $tmpi concentrators" >> /tmp/dgap.tmp2
				fi
				tmpi=`expr $tmpi + 1`
			done

			concstringtemp=`cat /tmp/dgap.tmp2`
			rm -f /tmp/dgap.tmp2


			dialog --backtitle "$maintitle" --nocancel \
				--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\nPlease select the cabling option used on line $l\n\n" \
15 $detected_cols 4 \
1 "4 wire direct" \
2 "8 wire direct (default)" \
3 "8 wire synchronous modem" \
4 "RS-232 synchronous modem" \
2>/tmp/dgap.ans

			if [ $? = 1 ]
			then
				sel=2
			else
				sel=`tail -1 /tmp/dgap.ans`
			fi

			rm -f /tmp/dgap.ans

			case "$sel" in
			"")
				option="2"
				;;
			[1-4])
				option="$sel"
				;;
			esac

			#
			# Direct 4
			#
			if [ "$option" = "1" ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$avail])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
13 $detected_cols 2 2 "460K bits/sec" 1 "230K bits/sec" 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=2
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-2])
					baud="$sel"
					;;
				esac

				eval nconc=\$nconc$n$l
				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud
				eval nports$n${l}c0=0
				c=1
				while [ $c -le $nconc ]
				do
					eval baud$n${l}c$c=$baud
					eval fbaud$n${l}c$c=$baud
					eval nports$n${l}c$c=16
					if [ "$AUTOIDS" = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2
					if [ $id2 -eq 1 ]
					then
						tmpid="$tmpid $id1"
					fi
					c=`expr $c + 1`
				done
			fi

			#
			# Direct 8
			#
			if [ "$option" = "2" ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$avail])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
22 $detected_cols 12 \
12 "1228K bits/sec (default)" \
11 "921K  bits/sec" \
10 "460K  bits/sec" \
9 "230K  bits/sec" \
8 "115K  bits/sec" \
7 "76.8K bits/sec" \
6 "57.6K bits/sec" \
5 "38.4K bits/sec" \
4 "19.2K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=12
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-9]|10|11|12)
					baud=`expr "$sel" + 2`
					;;
				"")
					baud=14
					;;
				esac

				eval nconc=\$nconc$n$l
				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud
				eval nports$n${l}c0=0
				c=1
				while [ $c -le $nconc ]
				do
					eval baud$n${l}c$c=$baud
					eval fbaud$n${l}c$c=$baud
					eval nports$n${l}c$c=16
					if [ "$AUTOIDS" = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2
					if [ $id2 -eq 1 ]
					then
						tmpid="$tmpid $id1"
					fi
					c=`expr $c + 1`
				done
			fi

			#
			# Custom 8 (Sync modem)
			#
			if [ "$option" = "3" ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$avail])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
22 $detected_cols 13 \
13 "1228K bits/sec (default)" \
12 "921K  bits/sec" \
11 "460K  bits/sec" \
10 "230K  bits/sec" \
9 "115K  bits/sec" \
8 "76.8K bits/sec" \
7 "57.6K bits/sec" \
6 "38.4K bits/sec" \
5 "19.2K bits/sec" \
4 "14.4K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=13
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-3])
					baud=`expr "$sel" + 14`
					baud2=`expr "$sel" + 2`
					;;
				4)
					baud=33
					baud2=6
					;;
				[5-9]|1[0123])
					baud=`expr "$sel" + 13`
					baud2=`expr "$sel" + 1`
					;;
				"")
					baud=26
					baud2=14
					;;
				esac

				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud

				eval nports$n${l}c0=0

				eval nconc=\$nconc$n$l
				c=1
				while [ $c -lt $nconc ]
				do
					eval nports$n${l}c$c=16

					if [ "$baud2" -lt 10 ]
					then
						eval baud$n${l}c$c=10
					else
						eval baud$n${l}c$c=$baud2
					fi
					eval fbaud$n${l}c$c=$baud2

					c=`expr $c + 1`
				done
				eval nports$n${l}c$c=16
				eval baud$n${l}c$c=$baud
				eval fbaud$n${l}c$c=$baud

				c=1
				while [ $c -le $nconc ]
				do
					if [ "$AUTOIDS" = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2
					if [ $id2 -eq 1 ]
					then
						tmpid="$tmpid $id1"
					fi
					c=`expr $c + 1`
				done
			fi

			#
			# RS-232 modem
			#
			if [ "$option" = 4 ]
			then

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring adapter ${n} (${BRDNAME}).\n\nHow \
many concentrators are connected to line $l?" 20 $detected_cols $tmpi $concstringtemp 2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=0
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				while :
				do
					case "$sel" in
					0)
						eval nconc$n$l="$sel"
						eval baud$n${l}c0=14
						eval nports$n${l}c0=0
						l=`expr $l + 1`
						continue 2
						;;
					[1-$avail])
						avail=`expr "$avail" - "$sel"`
						eval nconc$n$l="$sel"
						break
						;;
					esac
				done

				dialog --backtitle "$maintitle" --nocancel \
					--menu "Configuring line $l on adapter ${n} (${BRDNAME}).\n\
Please select the baud rate for the sync channel, used on line $l\n\n" \
23 $detected_cols 9 \
9 "76.8K bits/sec" \
8 "64.1K bits/sec" \
7 "57.6K bits/sec" \
6 "38.4K bits/sec" \
5 "19.2K bits/sec" \
4 "14.4K bits/sec" \
3 "9600  bits/sec" \
2 "4800  bits/sec" \
1 "2400  bits/sec" \
2>/tmp/dgap.tmp

				if [ $? = 1 ]
				then
					sel=9
				else
					sel=`tail -1 /tmp/dgap.tmp`
				fi

				rm -f /tmp/dgap.tmp

				case "$sel" in
				[1-3])
					baud=`expr "$sel" + 34`
					baud2=`expr "$sel" + 2`
					;;
				4)
					baud=38
					baud2=6
					;;
				[5-7])
					baud=`expr "$sel" + 34`
					baud2=`expr "$sel" + 1`
					;;
				8)
					baud=42
					baud2=9
					;;
				9)
					baud=43
					baud2=9
					;;
				esac

				eval baud$n${l}c0=$baud
				eval fbaud$n${l}c0=$baud

				eval nports$n${l}c0=0

				eval nconc=\$nconc$n$l
				c=1
				while [ $c -lt $nconc ]
				do
					eval nports$n${l}c$c=16

					if [ "$baud2" -lt 10 ]
					then
						eval baud$n${l}c$c=10
					else
						eval baud$n${l}c$c=$baud2
					fi
					eval fbaud$n${l}c$c=$baud2

					c=`expr $c + 1`
				done
				eval nports$n${l}c$c=16
				eval baud$n${l}c$c=$baud
				eval fbaud$n${l}c$c=$baud

				c=1
				while [ $c -le $nconc ]
				do
					if [ "$AUTOIDS" = "no" ]
					then
						readid "Please select an ID letter for concentrator $c"
					else
						nextid
					fi

					# Check for an 8-port C/X (set this_is_ccon8)
					check_ccon8

					eval id$n${l}c$c=$id1
					eval idn$n${l}c$c=$id2
					if [ $id2 -eq 1 ]
					then
						tmpid="$tmpid $id1"
					fi
					c=`expr $c + 1`
				done
			fi
			l=`expr $l + 1`
		done
	fi



	iostr=""
	mstr=""

	printf "You have selected the following configuration for adapter ${n}:\n" > /tmp/dgap.tmp
	printf "Adapter Type: ${BRDNAME}.${iostr}${mstr}\n" >> /tmp/dgap.tmp
	printf "\nConnections           Speed   Cabling         Port Names\n\
----------------------------------------------------------------" >> /tmp/dgap.tmp

	note=no
	l=1
	while [ $l -le 2 ]
	do
		printf "\nLine $l" >> /tmp/dgap.tmp
		eval nconc=\$nconc$n$l
		c=0
		while [ $c -lt $nconc ]
		do
			conc=`expr $c + 1`
			printf "\n    Concentrator $conc\t" >> /tmp/dgap.tmp

			eval baud=\$fbaud$n${l}c$c

			eval id=\$id$n${l}c$conc
			eval pn=\$idn$n${l}c$conc

			p1=`expr \( "$pn" - 1 \) \* 16 + 1`
			eval concsize=\$nports${n}${l}c$conc
			p2=`expr "$p1" + $concsize - 1`

			# add the leading zero to p1 if necessary
			p1=`expr "00$p1" : ".*\(..\)$"`
			p2=`expr "00$p2" : ".*\(..\)$"`
			pnames="${TTYNAME}${id}${p1} - ${TTYNAME}${id}${p2}"

			case ${baud} in
			1)	printf "230K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			2)	printf "460K\t4 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			3)	printf "2400\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			4)	printf "4800\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			5)	printf "9600\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			6)	printf "19.2K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			7)	printf "38.4K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			8)	printf "57.6K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			9)	printf "76.8K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			10)	printf "115K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			11)	printf "230K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			12)	printf "460K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			13)	printf "921K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			14)	printf "1228K\t8 wire direct\t$pnames" >> /tmp/dgap.tmp
				;;
			15)	printf "2400\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			16)	printf "4800\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			17)	printf "9600\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			18)	printf "19.2K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			19)	printf "38.4K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			20)	printf "57.6K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			21)	printf "76.8K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			22)	printf "115K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			23)	printf "230K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			24)	printf "460K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			25)	printf "921K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			26)	printf "1228K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			33)	printf "14.4K\t8 wire modem \t$pnames" >> /tmp/dgap.tmp
				;;
			35)	printf "2400\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			36)	printf "4800\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			37)	printf "9600\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			38)	printf "14.4K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			39)	printf "19.2K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			40)	printf "38.4K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			41)	printf "57.6K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			42)	printf "64.1K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			43)	printf "76.8K\tRS-232 modem \t$pnames" >> /tmp/dgap.tmp
				;;
			*)	printf "case got $1           " >> /tmp/dgap.tmp
				;;
			esac

			c=`expr $c + 1`
		done

		l=`expr $l + 1`
	done

	printf "\n\nIs this configuration acceptable?" >> /tmp/dgap.tmp

	output=`cat /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp

	if [ $collapse_supported -eq 1 ]
	then
		dialog --no-collapse --backtitle "$maintitle" --yesno "$output" $detected_lines $detected_cols 2>/dev/null
	else
		dialog --tab-correct --backtitle "$maintitle" --yesno "$output" $detected_lines $detected_cols 2>/dev/null
	fi

	if [ $? = 0 ]
	then
		# yes
		return 0
	else
		# no
		# restore state if ID allocation
		IDLIST1="$tmpid1"
		IDLIST2="$tmpid2"
		l=1
		while [ $l -le 2 ]
		do
			eval nconc=\$nconc$n$l
			c=1
			while [ $c -le $nconc ]
			do
				eval id=\$id$n${l}c$c
				eval pn=\$IDCOUNT$id
				pn=`expr "$pn" - 1`
				eval IDCOUNT$id=$pn

				c=`expr $c + 1`
			done
			l=`expr $l + 1`
		done
		return 1
	fi

}	# end of cxconf


#########################################################################
# Main - install epca
#########################################################################

if [ ! $1 ]
then
	outfile="/etc/dgap.conf"
else
	outfile=$1
fi

platform="ISA"
MAX_NBRDS=32

LCONC=$MAX_NBRDS

TTYNAME="tty"
PRNAME="pr"

termtype=${TERM:-"linux"}
tput -T${termtype} clear

# Find out how big we can make our dialog boxes
dialog --print-maxsize 2>/tmp/dgap.tmp

# Blah, doing --print-maxsize breaks stdin's tty settings...
stty sane

detected_lines=`cat /tmp/dgap.tmp | cut -f2 -d ' ' | tr -d ","`
detected_cols=`cat /tmp/dgap.tmp | cut -f3 -d ' ' | tr -d ","`

rm -f /tmp/dgap.tmp

if [ -z $detected_lines ]
then
	detected_lines=23
else
	detected_lines=`expr "$detected_lines" - 2`
fi

if [ -z $detected_cols ]
then
	detected_cols=`tput cols`
	detected_cols=`expr "$detected_cols" - 4`
else
	detected_cols=`expr "$detected_cols" - 2`
fi

# Find out if our dialogs can have the --no-collapse option...
dialog --help 2>/tmp/dgap.tmp
mytest=`cat /tmp/dgap.tmp | grep no-collapse | wc -l`
if [ $mytest -eq 0 ]
then
	collapse_supported=0
else
	collapse_supported=1
fi
rm -f /tmp/dgap.tmp

dialog --backtitle "$maintitle" \
--yesno "This script is used to configure Digi products for the DGAP driver.\n\n\
This driver supports up to ${MAX_NBRDS} Host Adapters.\n\n\
By default, the tty devices are installed in \"/dev\", \n
and the Transparent Print devices are installed in \"/dev/dg/dgap\".\n\n\
Are you sure you would you like to continue?\n" \
15 $detected_cols 2>/dev/null

if [ $? = 1 ]
then
	tput -T${termtype} clear
	exit 0
fi

if [ -f /etc/dgap.conf ]
then
	dialog --backtitle "$maintitle" \
	--yesno "Detected an old dgap configuration...\n\n\
Would you like this script to back it up to /etc/dgap.conf.bak?\n" \
10 $detected_cols 2>/dev/null
	if [ $? = 0 ]
	then
		if [ -f /etc/dgap.conf.bak ]
		then
			rm -f /etc/dgap.conf.bak
		fi
		mv /etc/dgap.conf /etc/dgap.conf.bak
	else
		rm -f /etc/dgap.conf
	fi
fi

dialog --backtitle "$maintitle" \
--yesno "The port names will look like \"${TTYNAME}a01\", \
where "a" is an ID letter that can be different for each port box, \
and "01" is the port number.\n\n\
Systems with a large number of ports may need to use some letters more \
than once.  When this occurs, the first box to use an ID letter will \
get port numbers 01-16, the second box to use that letter will get \
port numbers 17-32, and so on.\n\n\
You can have the install script pick ID letters for you, assigning \
letters consecutively (a, b, c, d...) and starting over from the \
beginning when it runs out of letters; or you can choose them manually.\n\n\
Manual ID selection is sometimes desirable for larger systems, where it \
can be used to help keep port names more organized.\n\n\
Do you want ID letters picked for you?" \
20 $detected_cols 2>/dev/null

if [ $? = 0 ]
then
	AUTOIDS=yes
else
	AUTOIDS=no
fi


ID1=
ID2=
IDLIST=


if [ "$AUTOIDS" = "yes" ]
then

	{

	percent=0
	count=0

	for i in a b c d e f g h i j k l m n o p q r s t u v w x y z
	{
		if ls /dev/${TTYNAME}${i}[0-9][0-9]* > /dev/null 2>&1
		then
			# If its there, only because of an old config
			# of our driver, then lets us it.
			if ls /dev/dg/dgap/${TTYNAME}${i}[0-9][0-9]* > /dev/null 2>&1
			then
				ID1="${ID1} ${i}"
			else
				ID2="${ID2} ${i}"
			fi
		else
			ID1="${ID1} ${i}"
		fi

		percent=`expr "$count" \* 100 / 25`
		echo $percent
		count=`expr $count + 1`
	}

	IDLIST="${IDTMP}${ID1} "
	printf "$IDLIST" >/tmp/dgap.tmp

	echo;

	} | dialog --backtitle "$maintitle" \
--guage "Scanning for tty ID letters not in use in /dev..." 6 $detected_cols 0 2>/dev/null

	IDLIST=`cat /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp

	IDLIST1="$IDLIST"
	IDLIST2="$IDLIST"
fi


#
# Configuration loop
#
# Initialize variables
#
fepport=""
fepmem=""
nports=""
config=""

#
# Custom installation
#

while :
do

#	dialog --backtitle "$maintitle" \
#		--inputbox "How many adapters do you wish to install (1-${MAX_NBRDS})?" \
#	8 $detected_cols 2>/tmp/dgap.ans

	dialog --backtitle "$maintitle" \
		--menu "How many adapters do you wish to install?" \
		18 $detected_cols 10 \
1 "adapter" 2 "adapters" 3 "adapters" 4 "adapters" 5 "adapters" \
6 "adapters" 7 "adapters" 8 "adapters" 9 "adapters" 10 "adapters" \
11 "adapters" 12 "adapters" 13 "adapters" 14 "adapters" 15 "adapters" \
16 "adapters" 17 "adapters" 18 "adapters" 19 "adapters" 20 "adapters" \
21 "adapters" 22 "adapters" 23 "adapters" 24 "adapters" 25 "adapters" \
26 "adapters" 27 "adapters" 28 "adapters" 29 "adapters" 30 "adapters" \
31 "adapters" 32 "adapters" 2>/tmp/dgap.ans

	if [ $? = 1 ]
	then
		exit
	else
		nbrds=`tail -1 /tmp/dgap.ans`
	fi

	rm -f /tmp/dgap.ans

	[ ! "$nbrds" ] && nbrds=0

	if [ "$nbrds" -ge 1 -a "$nbrds" -le ${MAX_NBRDS} ]
	then
		break
	fi
done


tmpid=""
n=1
while [ "$n" -le "$nbrds" ]
do
	iodisplay=""
	memdisplay=""

	#
	# Set up adapter type index variables N...
	# and corresponding printable strings P... 
	#

	# Leave this stuff here, in case we start supporting
	# more adapters.  
	P0="99"; P1="99"; P2="99"; P3="99"; P4="99"; 
	P5="99"; P6="99"; P7="99"; P8="99"; P9="99"; 
	P10=" 0"; P11=" 1"; P12=" 2"; P13=" 3"; P14=" 4"
	P15=" 5"; P16=" 6"

	N0=99; N1=99; N2=99; N3=99; N4=99; 
	N5=99; N6=99; N7=99; N8=99; N9=99; 
	N10=0; N11=1; N12=2; N13=3; N14=4;
	N15=5; N16=6

	MAXTYPE=6

	while :
	do

		dialog --backtitle "$maintitle" \
			--menu "Configuring adapter $n.\n\nWhat type of adapter do you have?\n\n" \
		18 $detected_cols 7 \
${P10} "AccelePort Xr 920 PCI" \
${P11} "AccelePort Xr PCI (EIA-232/RS-422)" \
${P12} "AccelePort Xem PCI" \
${P13} "AccelePort C/X PCI" \
${P14} "AccelePort EPC/X PCI" \
${P15} "IBM 8-Port Asynchronous PCI Adapter" \
${P16} "IBM 128-Port Asynchronous PCI Adapter" \
 2>/tmp/dgap.ans

	        if [ $? = 1 ]
	        then
			exit
		fi

		btype_index=`tail -1 /tmp/dgap.ans`
		rm -f /tmp/dgap.ans

		[ ! "$btype_index" ] && btype_index=99
		# We may want to use this for bus-specific action
		if [ $btype_index -ge 0 -a $btype_index -le $MAXTYPE ] ; then
			break
		fi
	done


	# Using ALPHA-numeric numbers for BTYPE to keep it to  
	# one digit, more for aesthetics than anything else. 
	case $btype_index in
	${N0})	BTYPE=0
		BRDNAME="AccelePort Xr ISA Host Adapter"
		platform=ISA
		;;
	${N1})	BTYPE=1
		BRDNAME="AccelePort Xem ISA Host Adapter"
		platform=ISA
		;;
	${N2})	BTYPE=2
		BRDNAME="AccelePort C/X ISA Host Adapter"
		platform=ISA
		;;
	${N3})	BTYPE=3
		BRDNAME="AccelePort EPC/X ISA Host Adapter"
		platform=ISA
		;;
	${N4})	BTYPE=4
		BRDNAME="AccelePort Xem EISA Host Adapter"
		platform=EISA
		;;
	${N5})	BTYPE=5
		BRDNAME="AccelePort C/X EISA Host Adapter"
		platform=EISA
		;;
	${N6})	BTYPE=6
		BRDNAME="AccelePort EPC/X EISA Host Adapter"
		platform=EISA
		;;
	${N7})	BTYPE=7
		BRDNAME="AccelePort Xem MC Host Adapter"
		platform=MC
		;;
	${N8})	BTYPE=8
		BRDNAME="AccelePort C/X MC Host Adapter"
		platform=MC
		;;
	${N9})	BTYPE=9
		BRDNAME="AccelePort EPC/X MC Host Adapter"
		platform=MC
		;;
	${N10})	BTYPE=A ; 
		BRDNAME="AccelePort Xr 920 PCI Host Adapter"
		platform=PCI
		;;
	${N11})	BTYPE=A ; 
		BRDNAME="AccelePort Xr PCI (EIA-232/RS-422) Host Adapter"
		platform=PCI
		;;
	${N12})	BTYPE=B ;
		BRDNAME="AccelePort Xem PCI Host Adapter"
		CONFIGBRDNAME="Digi_AccelePort_Xem_PCI"
		platform=PCI
		;;
	${N13})	BTYPE=C ; 
		BRDNAME="AccelePort C/X PCI Host Adapter"
		CONFIGBRDNAME="Digi_AccelePort_C/X_PCI"
		platform=PCI
		;;
	${N14})	BTYPE=D ;
		BRDNAME="AccelePort EPC/X PCI Host Adapter"
		CONFIGBRDNAME="Digi_AccelePort_EPC/X_PCI"
		platform=PCI
		;;
	${N15})	BTYPE=A ; 
		BRDNAME="IBM 8-Port Asynchronous PCI Host Adapter"
		platform=PCI
		;;
	${N16})	BTYPE=C ; 
		BRDNAME="IBM 128-Port Asynchronous PCI Host Adapter"
		CONFIGBRDNAME="Digi_AccelePort_C/X_PCI"
		platform=PCI
		;;
	esac

	case $BTYPE in
	#
	# PCI Adapters
	#
	A|B|C|D)	# No I/O port or memory asked for PCI
		porttmp=0
		memory=0
		conftmp="${conftmp} 0 0"
		memtmp="${memtmp} 0"
		# force default of interrupts off. *ONLY the Xr PCI boards
		# can have interrupts enabled in this driver.
		eval useint${n}=0
		;;
	esac

	case "$BTYPE" in
	0)	xrconf
		;;
	A)
		xrconf
		;;
	1|4|7|B)	emconf
		;;
	2|5|8|C)	cxconf
		;;
	3|6|9|D)	epcconf
		;;
	esac
	if [ $? -eq 0 ]
	then
		fepmem="$fepmem $memtmp"
		config="$config $conftmp"
		fepport="$fepport $porttmp"
		eval BTYPE${n}=\$BTYPE
		eval bname${n}=\$BRDNAME
		eval configbname${n}=\$CONFIGBRDNAME
		eval fepmem$n="$memory"
		eval fepport$n="$porttmp"
		memtmp=""
		conftmp=""
		porttmp=""
		setok=1
	else
		memtmp=""
		conftmp=""
		porttmp=""
		memory=""

		dialog --backtitle "$maintitle"  --msgbox "Press Enter to re-configure adapter #${n}" 6 $detected_cols 2>/dev/null

		setok=0
	fi

	if [ $setok = 1 ]
	then
		dialog --backtitle "$maintitle" \
			--yesno "Altpin is provided to allow the DCD \
input signal to be exchanged with the DSR input.\nThis is useful on \
products with RJ-45 connectors as these products have DCD on pin 10.\n\
Choosing altpin will allow the use of 8-pin connectors while still having \
the benefit of full modem control.\nThe use of \"altpin\" on \
products with DB-25 connectors is not normally recommended.\n\n\
The default setting for altpin is off.\n\nIs this acceptable? " \
17 $detected_cols 2>/dev/null

		if [ $? = 0 ]
		then
			eval altpin${n}=0
		else
			eval altpin${n}=1
		fi
		n=`expr $n + 1`
	fi
done

# this is new stuff that I added to export the variables to 
# the postinstall script
cat >>$outfile <<!
config_begin
!
n=1
while [ $n -le $nbrds ]
do

#	eval tmp=\$bname$n
#	echo BNAME${n}=$tmp >> $outfile

	# Output our SCO compat config name
	eval tmp=\$configbname$n
	printf "board\t$tmp\n" >> $outfile
	printf "\tio 0x000\n" >> $outfile
	printf "\tmem 0x000000\n" >> $outfile

#	eval tmp=\$fepmem$n
#	echo FEPMEM${n}=$tmp >> $outfile
#	eval tmp=\$fepport$n
#	echo FEPPORT${n}=$tmp >> $outfile

	eval bt=\$BTYPE$n
#	echo BTYPE${n}=$bt >> $outfile

	# Output interrupt setting.
#	eval tmp=\$useint$n
#	echo USEINT${n}=$tmp >> $outfile

	case "$bt" in
	A)
	# Xr cards
		eval nconc=\$nconc${n}0

		c=1
		while [ $c -le $nconc ]
		do
			eval pn=\$idn${n}0c${c}

			eval tmp=\$nports$n${l}c$c
			if [ "$tmp" ] ; then  # This isn't always set
				echo NPORTS$n${l}C${c}=$tmp >> $outfile
			fi
			pn=`expr \( $pn - 1 \) \* 16 + 1`

			printf "\tstart $pn\n" >> $outfile

			eval nm=\${nmod${n}${l}c${c}:=0}
			eval tmp=\$nports${n}0c${c}m0
			printf "\tnports $tmp\n" >> $outfile

			eval id=\$id${n}0c${c}
			printf "\tttyname tty$id\n" >> $outfile

			c=`expr $c + 1`
		done
		;;


	0|1|4|7|B)
	# Xem modules
		eval nconc=\$nconc${n}0

#		echo NCONC${n}0=$nconc >> $outfile

		c=1
		while [ $c -le $nconc ]
		do

			printf "\tmodule ports\n" >> $outfile

			eval pn=\$idn${n}0c${c}

			eval tmp=\$nports$n${l}c$c
			if [ "$tmp" ] ; then  # This isn't always set
				echo NPORTS$n${l}C${c}=$tmp >> $outfile
			fi
			pn=`expr \( $pn - 1 \) \* 16 + 1`

			eval nm=\${nmod${n}${l}c${c}:=0}
#			echo NMOD${n}${l}C${c}=$nm >> $outfile
			eval tmp=\$nports${n}0c${c}m0
#			echo NPORTS${n}0C${c}M0=$tmp >> $outfile
			printf "\t\tnports $tmp\n" >> $outfile

			if [ "$nm" != 0 ]
				then
				m=1
				while [ $m -le $nm ]
				do
					eval id=\$id$n${l}c${c}m$m
#					echo ID$n${l}C${c}M${m}=$id >> $outfile
					printf "\t\tid $id\n" >> $outfile

					eval pn=\$idn$n${l}c${c}m$m
#					echo IDN$n${l}C${c}M${m}=$pn >> $outfile

					eval tmp=\$nports$n${l}c${c}m$m
#					echo NPORTS$n${l}C${c}M$m=$tmp >> $outfile
					printf "\t\tnports $tmp\n" >> $outfile
					pn=`expr \( $pn - 1 \) \* 16 + 1`

					printf "\t\tstart $pn\n" >> $outfile

					m=`expr $m + 1`
				done
			fi

			eval id=\$id${n}0c${c}
			printf "\t\tid $id\n" >> $outfile
			printf "\t\tstart $pn\n" >> $outfile

			c=`expr $c + 1`
		done
		;;
	2|3|5|6|8|9|C|D)
	# CX & EPC modules
		eval tmp=\$nconc$n0
#		echo NCONC${n}0=$tmp >> $outfile
		l=1
		while [ $l -le 2 ]
		do

#			printf "\tline $l\n" >> $outfile
			myline=`expr $l - 1`
			printf "\tline $myline\n" >> $outfile

			eval nconc=\$nconc$n$l
#			echo NCONC${n}${l}=$nconc >> $outfile

			eval tmp=\$baud$n${l}c0
#			echo BAUD$n${l}C0=$tmp >> $outfile
			printf "\t\tspeed $tmp\n" >> $outfile

			c=1
			while [ $c -le $nconc ]
			do

				eval tmp=\$conctype$n${l}c$c
				printf "\t\t\tconc $tmp\n" >> $outfile

				eval pn=\$idn$n${l}c$c
#				echo IDN$n${l}C${c}=$pn >> $outfile
				eval tmp=\$nports$n${l}c$c
#				echo NPORTS$n${l}C${c}=$tmp >> $outfile
				printf "\t\t\tnports $tmp\n" >> $outfile

				eval id=\$id$n${l}c${c}
				printf "\t\t\tid $id\n" >> $outfile

				pn=`expr \( $pn - 1 \) \* 16 + 1`

				printf "\t\t\tstart $pn\n" >> $outfile

				eval nm=\${nmod${n}${l}c${c}:=0}
#				echo NMOD${n}${l}C${c}=$nm >> $outfile

				if [ "$nm" != 0 ]
				then
					m=1
					while [ $m -le $nm ]
					do
						eval id=\$id$n${l}c${c}m$m
#						echo ID$n${l}C${c}M${m}=$id >> $outfile

						printf "\t\t\t\tmodule ports\n" >> $outfile

						eval pn=\$idn$n${l}c${c}m$m
#						echo IDN$n${l}C${c}M${m}=$pn >> $outfile

#						eval tmp=\$nports$n${l}c${c}m$m
#						echo NPORTS$n${l}C${c}M$m=$tmp >> $outfile

						printf "\t\t\t\tnports $tmp\n" >> $outfile
						printf "\t\t\t\tid $id\n" >> $outfile

						pn=`expr \( $pn - 1 \) \* 16 + 1`

						printf "\t\t\t\tstart $pn\n" >> $outfile

						m=`expr $m + 1`
					done
				fi

				eval tmp=\$baud$n${l}c$c
				printf "\t\tspeed $tmp\n" >> $outfile

				c=`expr $c + 1`
			done
			l=`expr $l + 1`
		done
		;;
	esac


	# Output altpin setting
	eval tmp=\$altpin$n
	echo altpin $tmp >> $outfile

	# Output useintr setting
	eval tmp=\$useint$n
	echo useintr $tmp >> $outfile



	n=`expr $n + 1`
done

printf "config_end\n" >> $outfile

dialog --backtitle "$maintitle" \
	--yesno "Would you like to load the driver with this configuration now? " \
8 $detected_cols 2>/dev/null

if [ $? = 0 ]
then
	/etc/init.d/dgap restart dialog
else
	dialog --backtitle "$maintitle"  --msgbox \
"When you are ready to load the driver with this configuration,\n\
please type /etc/init.d/dgap restart" 6 $detected_cols 2>/dev/null

fi

#end of request script
