:
# Mkdev script for BocaBoard BB8002/BB1610/BB6410 driver.
# Copyright (c) 1992, Boca Research, Inc.  All rights reserved.
#
#	History:
#
#		Ver: 
#
#		1.1.2 - Modified the old script to support 8002/1610 for 
#		  SCO UNIX/XENIX and to accomodate make node with other
#		  multiport vendors. 
#
#		1.1.2a - Configured the driver to include the poller
#		entry point.
#			
#			
#			
#

PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

IODIR=/usr/sys/io
STOREL="/usr/lib/storel -DM_I386 -Mx -r -octmp.o"
BBFILE=bbiconf.asm
BBOFILE=bbiconf.o
TMPFILE=/tmp/bbiconfig$$

FIRSTDEV="0"
RCD5DIR=/etc/rc.d/5
DNLDFILE=/usr/lib/bbi/bbi1610
DNLDEXE=/usr/lib/bbi/bbild

: ${OK=0} ${FAIL=1} ${TRUE=0} ${FALSE=1}

NAME=bbi
FUNCS="bbiinit bbiopen bbiclose bbiread bbiwrite bbiioctl bbipoll bbi_tty"
IPL=5
TTYSTR="/dev/ttyz00"
AGAIN=NO
let="a"
BRDID="a b d e f g h i j k m n o p q r s u v w x y z 1 2 3 4 5 6 7 8 9"
SLOT=""

IOBASE1=0
IOBASE2=0
IOBASE3=0
IOBASE4=0
PTMP=0

PORTFLAG="0"
# Function Definitions
#########################

# ---------- STANDARD ROUTINES -------- These routines are common to scripts
#                                       requiring kernel relinking.
# Define traps for critical and non critical code.
#
set_trap ()  {
        trap 'echo "\nInterrupted! Exiting ..."; cleanup 1' 1 2 3 15
}

unset_trap ()  {
        trap '' 1 2 3 15
}

# Remove temp files and exit with the status passed as argument
#
cleanup () {
        trap '' 1 2 3 15
        [ "$tmp" ] && rm -f $tmp*
        exit $1
}

# Prompt for yes or no answer - returns non-zero for no
#
getyn () {
        while   echo "$* (y/n) \c">&2
        do      read yn rest
                case $yn in
                [yY])   return $OK                      ;;
                [nN])   return $FAIL                    ;;
                *)      echo "Please answer y or n" >&2 ;;
                esac
        done
}

# Prompt with mesg, return non-zero on q
prompt () {
        while   echo "\n${mesg}or enter q to quit: \c" >&2
        do      read cmd
                case $cmd in
                +x|-x)  set $cmd                                        ;;
                Q|q)    return $FAIL                                    ;;
                !*)     eval `expr "$cmd" : "!\(.*\)"`                  ;;
                "")     # If there is an argument use it as the default
                        # else loop until 'cmd' is set
                        [ "$1" ] && {
                                cmd=$1
                                return $OK
                        }
                        : continue
                        ;;
                *)      return $OK                                      ;;
                esac
        done
}

# Print an error message
error () {
        echo "\nError: $*" >&2
        return $FAIL
}

# Configure error message
conferr ()  {
        error "configure failed to update system configuration.
Check ${CONFDIR}/conflog for details."
}

# Determine operating system
get_os () {
        OSVER=`uname -r`
        case "$OSVER" in
                3.2* ) OS=unix; CONFDIR=/etc/conf/cf.d ;;
                2.[23]* ) OS=xenix; CONFDIR=/usr/sys/conf ;;
                *)  error "cannot determine operating system release"
                        return $FAIL ;;
        esac
        return $OK
}

# perms list needed if link kit must be installed
permschk () {
        if [ -f /etc/perms/inst ]; then
                PERM=/etc/perms/inst
        else
                error "Cannot locate /etc/perms/inst. Needed to verify
linkkit installation"
                cleanup $FAIL
        fi
}

# test to see if link kit is installed
linkchk ()  {
        cd /
        until   fixperm -i -d LINK $PERM
        do      case $? in
                4)  echo "\nThe Link Kit is not installed." >&2 ;;
                5)  echo "\nThe Link Kit is only partially installed." >&2  ;;
                *)  echo "\nError testing for Link Kit.  Exiting." >&2
                        cleanup $FAIL ;;
                esac

                # Not fully installed. Do so here
                getyn "\nDo you wish to install it now?" || {
                        # answered no
                        echo "
The link kit must be installed to run this program.  Exiting ..."
                        cleanup $OK
                }

                # answered yes, so install link kit
                echo "\nInvoking /etc/custom\n"
                /etc/custom -o -i LINK || {
                        # custom exited unsuccessfully
                        error "custom failed to install Link Kit successfully.  Please try again."
                        cleanup $FAIL
                }
        done
}

# See if driver already configured
prodchk () {
        confchk

        echo "\n\n\tBocaBoard (TM) SCO UNIX/XENIX driver installation."
        echo "\tCopyright (c) 1992, Boca Research, Inc."
        echo "\tThis script configures the BocaBoard driver"
        echo "\tinto the system and allows you to rebuild the kernel."
        echo "\tThis driver supports up to 4 boards."
        echo "\n\n"

        case $cf_state in
        YES|PART) echo "
The BocaBoard driver is already installed.  If you continue
you may overwrite the existing system configuration."
                getyn "Do you wish to continue?" || return $FAIL
		AGAIN=YES
		killnodes || return $FAIL
                ;;
        NO)	 
		echo "Configuring BocaBoard driver."
                ;;
        esac

        return $OK
}

asklink ()  {
        getyn "
You must create a new kernel to effect the driver change you specified.
Do you wish to create a new kernel now?"  ||  {
                echo "
To create a new kernel execute ${CONFDIR}/link_${OS}. Then you
must reboot your system by executing  /etc/shutdown  before the
changes you have specified will be implemented.\n"
                        return $FAIL
                }
        return $OK
}

#Link the kernel
klink () {
        [ "$_RELINK" ] && return

        cd $CONFDIR

        if [ "$OS" = "unix" ]
        then
                ../bin/idbuild || return $FAIL
                return $OK
        else
                ./link_xenix || {
                        echo "
The xenix kernel has failed to link.  Recheck the configuration of your
system and try again by changing to the $CONFDIR directory and
running ./link_xenix\n"
                        return $FAIL
                }
                getyn "
The xenix kernel has been successfully relinked.  Do you want this
kernel to boot by default?" || {
                        echo "The new kernel is in ${CONFDIR}/xenix."
                        return $OK
                }
                ./hdinstall
                return $OK
        fi
}

# CONFIGURE routines

confchk () 
{
        cd $CONFDIR

        MAJOR=`./configure -j ${NAME}` || {
                cf_state=NO
                return $TRUE
        }
        cf_state=YES
        return $TRUE
}

confadd () 
{
        echo "\nAdding device to system configuration files ..."
        cd $CONFDIR
        [ $cf_state = YES ] && {
                echo "Deleting previous driver"
                ./configure -d -c -m $MAJOR
		if [ AGAIN = NO ]
		then
                	killnodes || return $FAIL
		fi
        }
        MAJOR=`./configure -j NEXTMAJOR`
        echo "\n\nConfiguring driver with major number ${MAJOR}."

        if [ "$OS" = "unix" ]
        then
                confuadd || return $FAIL
        else
                confxadd || return $FAIL
        fi

        return $OK
}

# Unix. Configure a non-required driver. Make an sdevice file for it.
#
confuadd () 
{
        ./configure -c -a $FUNCS -m $MAJOR -h $NAME -M 1 4 > conflog || {
                conferr
                return $FAIL
        }
        ./configure -c -m $MAJOR -d -R > conflog || {
                conferr
                return $FAIL
        }

        rm -f conflog

        [ "$IO1" ] || return $OK
        echo "$NAME     Y       1       $IPL    0       0       $IO1    $IO1END 0       0" > ../sdevice.d/$NAME

        [ "$IO2" ] || return $OK
        echo "$NAME     Y       1       $IPL    0       0       $IO2    $IO2END 0       0" >> ../sdevice.d/$NAME

        [ "$IO3" ] || return $OK
        echo "$NAME     Y       1       $IPL    0       0       $IO3    $IO3END 0       0" >> ../sdevice.d/$NAME

        [ "$IO4" ] || return $OK
        echo "$NAME     Y       1       $IPL    0       0       $IO4    $IO4END 0       0" >> ../sdevice.d/$NAME

        return $OK
}

# Xenix. Calculate the vectors and configure the driver.
confxadd () {
        ./configure -c -a $FUNCS -m $MAJOR -l $IPL -v $V1 $V2 $V3 $V4 -r\
          > conflog || {
                conferr
                return $FAIL
        }

        return $OK
}

# display the intended tty's nodes for /dev for this board
#

letdisplay() 
{
	IOP=$IOBASE1
        while [ $IOBASE1 != 0 ]
        do 
	    IOBASE1=`expr $IOBASE1 - 1`

            echo "2E8\c"

            while :
            do
                if test -r "/dev/tty${1}00"
                then
                    shift
                else

			# Save first device for download
			#

			if [ "$FIRSTDEV" = "0" ]
			then
				FIRSTDEV="/dev/tty${1}00"
			fi

		    echo ", with\c"
		    PTMP=`expr $IOP - $IOBASE1`
		    portcount
                    echo "devices starting at /dev/tty${1}00 for board number ${PTMP}"
                    shift
                    break
                fi
            done
        done 

	IOP=$IOBASE2
        while [ $IOBASE2 != 0 ]
        do 
	    IOBASE2=`expr $IOBASE2 - 1`
            echo "2F8\c"
            while :
            do
                if test -r "/dev/tty${1}00"
                then
                    shift
                else

		    if [ "$FIRSTDEV" = "0" ]
		    then
			FIRSTDEV="/dev/tty${1}00"
		    fi

		    echo ", with\c"
		    PTMP=`expr $IOP - $IOBASE2`
		    portcount
                    echo "devices starting at /dev/tty${1}00 for board number ${PTMP}"
                    shift
                    break
                fi
            done
        done 

	IOP=$IOBASE3
        while [ $IOBASE3 != 0 ]
        do 
	    IOBASE3=`expr $IOBASE3 - 1`
            echo "3E8\c"
            while :
            do
                if test -r "/dev/tty${1}00"
                then
                    shift
                else

		    if [ "$FIRSTDEV" = "0" ]
		    then
			FIRSTDEV="/dev/tty${1}00"
		    fi

		    echo ", with\c"
		    PTMP=`expr $IOP - $IOBASE3`
		    portcount
                    echo "devices starting at /dev/tty${1}00 for board number ${PTMP}"
                    shift
                    break
                fi
            done
        done 

	IOP=$IOBASE4
        while [ $IOBASE4 != 0 ]
        do 
	    IOBASE4=`expr $IOBASE4 - 1`
            echo "3F8\c"
            while :
            do
                if test -r "/dev/tty${1}00"
                then
                    shift
                else

		    if [ "$FIRSTDEV" = "0" ]
		    then
			FIRSTDEV="/dev/tty${1}00"
		    fi

		    echo ", with\c"
		    PTMP=`expr $IOP - $IOBASE4`
		    portcount
                    echo "devices starting at /dev/tty${1}00 for board number ${PTMP}"
                    shift
                    break
                fi
            done
        done 

        return $OK

}

portcount()
{
	if [ $PTMP = 1 ]
	then
		echo " $PT1 \c"
	elif [ $PTMP = 2 ]
	then
		echo " $PT2 \c"
	elif [ $PTMP = 3 ]
	then 
		echo " $PT3 \c"
	elif [ $PTMP = 4 ]
	then
		echo " $PT4 \c"
	fi
}

getconf() 
{
        while :
        do
		IOBASE1=0
		IOBASE2=0
		IOBASE3=0
		IOBASE4=0
		PTMP=0
        	getbrds || return $FAIL
            echo "\nYou have chosen to install $BOARDS BocaBoard(s) at I/O address:"

        	letdisplay $BRDID

        	echo

        	getyn "Is this correct" || {
                        # answered no
			PORTFLAG="0"
			PT1=0
			PT2=0
			PT3=0
			PT4=0
			PORTSUM=0
			BOARDS=0
			IOBASE1=0
			IOBASE2=0
			IOBASE3=0
			IOBASE4=0
			IO1=
			IO2=
			IO3=
			IO4=
			IOBASE=0
                        continue
                }
                break
        done

    	return $OK

}

# Build the /etc/rc.d/5/bbi for downloading the code 
#

buildrc()
{
	echo "$DNLDEXE -d$DNLDFILE -t$FIRSTDEV" >> $RCD5DIR/bbi 
	return $OK

}

# Get the params for each board. This sets 'IO#', 'v#', and 'PT#'
# variables.
#

getbrds () 
{
        while echo "
Enter the number of boards to configure (1 - 4) or 'q' to quit: \c "

        read x

        echo

        do
                case $x in
                        1|2|3|4)        BOARDS=$x; break        ;;
                        q|Q)            echo "\nInstallation aborted" >&2
                                        cleanup $FAIL   ;;
                        *)              echo "
You cannot configure more than 4 boards."
                                        continue        ;;
                esac
        done

# For each board, get the IRQ, the IO base and the number of ports

        BRD=0
        PORTSUM=0

        getbrd || return $FAIL
        IO1=$IOBASE
        htod $IO1
        IO1END=`expr $decval + 7 | awk '{ printf("%02x", $1) }'`
        PT1=$PORTS
        BRD=`expr $BRD + 1`
        PORTSUM=`expr $PORTSUM + ${PORTS}`
        [ "$BRD" != "$BOARDS" ] || return $OK

        getbrd || return $FAIL
        IO2=4$IOBASE
        htod $IO2
        IO2END=`expr $decval + 7 | awk '{ printf("%02x", $1) }'`
        PT2=$PORTS
        BRD=`expr $BRD + 1`
        PORTSUM=`expr $PORTSUM + ${PORTS}`
        [ "$BRD" != "$BOARDS" ] || return $OK

        getbrd || return $FAIL
        IO3=8$IOBASE
        htod $IO3
        IO3END=`expr $decval + 7 | awk '{ printf("%02x", $1) }'`
        PT3=$PORTS
        BRD=`expr $BRD + 1`
        PORTSUM=`expr $PORTSUM + ${PORTS}`
        [ "$BRD" != "$BOARDS" ] || return $OK

        getbrd || return $FAIL
        IO4=C$IOBASE
        htod $IO4
        IO4END=`expr $decval + 7 | awk '{ printf("%02x", $1) }'`
        PT4=$PORTS
        BRD=`expr $BRD + 1`
        PORTSUM=`expr $PORTSUM + ${PORTS}`
        [ "$BRD" != "$BOARDS" ] || return $OK

        return $FAIL
}

# Get info for a single board. Sets the 'PORTS' variable
getbrd () {

        PLACE=""
        if [ "$BOARDS" != "1" ]
        then
		PLACE="s"
        fi

	if [ $PORTFLAG = "0" ]
	then
        	while echo "
       Select the base I/O address for the board${PLACE}:
        1) 2E8
        2) 2F8
        3) 3E8
        4) 3F8

       Select an option or enter 'q' to quit: \c"
        	read x
       		do case $x in
        	1) IOBASE="2E8"; IOBASE1=`expr $BOARDS ` ; break ;;
        	2) IOBASE="2F8"; IOBASE2=`expr $BOARDS ` ; break ;;
        	3) IOBASE="3E8"; IOBASE3=`expr $BOARDS ` ; break ;;
        	4) IOBASE="3F8"; IOBASE4=`expr $BOARDS ` ; break ;;
        	Q|q) echo "\nInstallation aborted" >&2
         	cleanup $FAIL ;;
        	*) echo "\nEnter 1, 2, 3, 4, or q\n" >&2 ;;
        	esac
       		done
		PORTFLAG="1"
	fi
#How many ports are available on board #$BRD

	SLOT=""
	if [ "$BOARDS" != "1" ]
	then
		if [ "$BRD" = "0" ]
		then 
			SLOT=" number 1"
		elif [ "$BRD" = "1" ]
		then
			SLOT=" number 2"
		elif [ "$BRD" = "2" ]
		then
			SLOT=" number 3"
		elif [ "$BRD" = "3" ]
		then
			SLOT=" number 4"
		fi
	fi
			
        while echo "

       Number of ports for board${SLOT}:

        1) 10 ports on Boca Research 8002
        2) 16 ports on Boca Research 1610

       Select an option or enter 'q' to quit: \c"

        read x
        do      case $x in
                1)      PORTS=10; break ;;
                2)      PORTS=16; break ;;
                Q|q)    echo "\nInstallation aborted" >&2
                        cleanup $FAIL   ;;
                *)      echo "\nEnter 1, 2 or q\n" >&2 ;;
                esac
        done

        return $OK
}

# Get decimal value from hex number
#
htod() 
{
        decval=`echo $1 | awk '
                { len = length($1); dec = 0; mult = 1; }
                { for (x = len; x > 0; x--) {
                        c = substr($1, x, 1);
                        if (c >= "0" && c <= "9") val = c;
                        if (c == "A") val = 10;
                        if (c == "B") val = 11;
                        if (c == "C") val = 12;
                        if (c == "D") val = 13;
                        if (c == "E") val = 14;
                        if (c == "F") val = 15;
                        dec += val * mult;
                        mult *= 16;
                        }
                }
                { printf("%d", dec); }
        '`
}

# Unix. Create the new space.c file.
#
updspace () 
{
        echo "Updating driver configuration ..."
        cd /etc/conf/pack.d
        [ -d ./bbi ] || { echo "
The directory /etc/conf/pack.d/bbi does not exist.  Please be sure to
install the product according to the installation instructions."
                return $FAIL
        }
        cd bbi
        echo "
#include <sys/types.h>
#include <sys/tty.h>
#include \"bbi.h\"

int Bbi_numttys = ${PORTSUM};
int Bbi_numbrds = ${BOARDS};
struct tty bbi_tty[${PORTSUM}];
int BbiInClose[${PORTSUM}] = {0};
BBI_DEV bbi_dev[${PORTSUM}];
BBI_BRD Bbi_info[${BOARDS}] = {
\c" > ./space.c

        [ "$IO1" ] || {
                echo "};" >> ./space.c
                return $OK
        }
        echo "\t0x$IO1, 0, $PT1, 0, 0," >> ./space.c

        [ "$IO2" ] || {
                echo "};" >> ./space.c
                return $OK
        }
        echo "\t0x$IO2, 1, $PT2, 0, 0," >> ./space.c

        [ "$IO3" ] || {
                echo "};" >> ./space.c
                return $OK
        }
        echo "\t0x$IO3, 2, $PT3, 0, 0," >> ./space.c

        [ "$IO4" ] || {
                echo "};" >> ./space.c
                return $OK
        }
        echo "\t0x$IO4, 3, $PT4, 0, 0," >> ./space.c

echo "};" >> ./space.c
        return $OK
}

# Xenix. Create the new bbiconf.asm file. Note that this makes a hard
# assumption about the byte size of the tty structure (96 bytes).
#

updxconf () 
{
        echo "Updating driver configuration ..."
        cd $IODIR
        echo "
        TITLE   \$bbiconf

        .386
DGROUP	GROUP	CONST, _BSS, _DATA
PUBLIC  _Bbi_numbrds
PUBLIC  _Bbi_numttys
PUBLIC  _BbiInClose
PUBLIC  _BbiWDrain
PUBLIC  _Bbi_info
PUBLIC  _bbi_dev

_DATA	SEGMENT  DWORD USE32 PUBLIC 'DATA'
_bbi_dev	DB 	12*${PORTSUM} DUP (0)	
_Bbi_numttys	DD	$PORTSUM
_Bbi_numbrds	DD	$BOARDS
_BbiInClose	DD    	$PORTSUM DUP (0)
_BbiWDrain	DD    	$PORTSUM DUP (0)
COMM	_bbi_tty:BYTE:96*${PORTSUM}" >  ${IODIR}/$BBFILE

echo "_Bbi_info\t\c" >> ${IODIR}/$BBFILE

	case $BOARDS in
	1) echo "\tDD\t${IO1}H\n\tDD\t0\n\tDD\t${PT1}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   ;;
	2) echo "\tDD\t${IO1}H\n\tDD\t0\n\tDD\t${PT1}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO2}H\n\tDD\t0\n\tDD\t${PT2}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   ;;
	3) echo "\tDD\t${IO1}H\n\tDD\t0\n\tDD\t${PT1}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO2}H\n\tDD\t0\n\tDD\t${PT2}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO3}H\n\tDD\t0\n\tDD\t${PT3}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   ;;
	4) echo "\tDD\t${IO1}H\n\tDD\t0\n\tDD\t${PT1}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO2}H\n\tDD\t0\n\tDD\t${PT2}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO3}H\n\tDD\t0\n\tDD\t${PT3}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   echo "\tDD\t${IO4}H\n\tDD\t0\n\tDD\t${PT4}\n\tDD\t0\n\tDD\t0" >>  ${IODIR}/$BBFILE
	   ;;
	esac

	echo "\n
_DATA      ENDS
_BSS	SEGMENT  DWORD USE32 PUBLIC 'BSS'
_BSS      ENDS
CONST	SEGMENT  DWORD USE32 PUBLIC 'CONST'
CONST      ENDS
_TEXT	SEGMENT  DWORD USE32 PUBLIC 'CODE'
	ASSUME   CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
_TEXT	ENDS
END\n" >>  ${IODIR}/$BBFILE
	return $OK
}

# Xenix. Assemble the new bbiconf.asm
asmxconf () {
        cd $IODIR

        $STOREL $BBFILE > /dev/null 2>&1 || {
                        echo "
The configuration file ${IODIR}/$BBFILE could not be assembled."
                        return $FAIL
        }
        mv ctmp.o $BBOFILE
        return $OK
}

# Xenix. Patch the link script with the new object names
#
updxlink () {
        cd /usr/sys/conf

        grep -s bbi.o link_xenix > /dev/null || {
                cp link_xenix link_xenix.00 || {
                        echo "Cannot copy link_xenix" >&2
                        exit $FAIL
                }
                trap "mv link_xenix.00 link_xenix; exit $FAIL" 1 2 3 15
                sed "s!c.o!& ../io/bbi.o ../io/bbiconf.o!" link_xenix.00 \
                  > link_xenix || {
                        echo "Cannot edit link_xenix" >&2
                        mv link_xenix.00 link_xenix
                        exit $FAIL
                }
                unset_trap
                chmod 700 link_xenix
                return $OK
        }
        return $OK
}

# Update the init and node config files. This expects the 'BOARDS',
# 'PT1-4' variables to be set.

updinit () {
        tmp1=${NAME}.$$
        tmp2=${NAME}type.$$

        echo "Updating system node and init files ..."
        [ -d /usr/lib/bbi ] || mkdir /usr/lib/bbi
        [ -f /usr/lib/bbi/bbittys ] && rm -f /usr/lib/bbi/bbittys

        [ "$OS" = "unix" ] && {
                rm -f /etc/conf/init.d/$NAME
                rm -f /etc/conf/node.d/$NAME
        }

        b=0
        mdev=0
	let="a"
	pidx=0
	oldpcnt=0
	set $BRDID

        while [ "$b" != "$BOARDS" ]
        do
		pidx=0

                case $b in
                        0)      pcnt=$PT1 ;;
                        1)      pcnt=$PT2 ;;
                        2)      pcnt=$PT3 ;;
                        3)      pcnt=$PT4 ;;
                        *)      echo "$b is not a supported number of boards"
                                return $FAIL ;;
                esac

		while :
		do
			if test -r "/dev/tty${1}00"
			then
				shift
			else
				let=$1
				shift
				break
			fi
		done

		while [ "$pidx" != "$pcnt" ]
		do
			donode $let $mdev $pidx $oldpcnt
			mdev=`expr $mdev + 1`
			pidx=`expr $pidx + 1`
			oldpcnt=`expr $oldpcnt + 1`
		done
	
                b=`expr $b + 1`
        done
        if [ "$OS" = "xenix" ]
        then
                chmod 664 /etc/ttys
        else
                sort $tmp1 > /etc/conf/init.d/$NAME
#               sort $tmp2 >> /etc/ttytype
                rm -f $tmp1 $tmp2
        fi
        return $OK
}

# Given a tty letter (1) and a minor device # (2) and a port number,
# create the node file entry and the init file entry for terminal
# and modem devices
#

donode () 
{
	mdm=`expr $2 + 128`
	lstr=`echo $3 | awk '{ printf("%02d", $1) }'`
	
	echo "/dev/tty$1$lstr\r\c"
	m=`echo $1$lstr | tr "[a-z]" "[A-Z]"`

	echo "tty$1$lstr" >> /usr/lib/bbi/bbittys
	echo "tty$m" >> /usr/lib/bbi/bbittys

	if [ "$OS" = "unix" ] 
	then
		echo "$1$lstr:2:off:/etc/getty tty$1$lstr m" >> $tmp1
		echo "$m:2:off:/usr/lib/uucp/uugetty -t60 tty$m 3" >> $tmp1

		echo "$NAME	tty$1$lstr	c	$2" >> /etc/conf/node.d/$NAME	
		echo "$NAME	tty$m	c	$mdm" >> /etc/conf/node.d/$NAME	
	else
		grep -s tty${1}$lstr /etc/ttys > /dev/null \
		  || echo "0mtty${1}$lstr" >> /etc/ttys
		grep -s tty$m /etc/ttys > /dev/null \
		  || echo "03tty$m" >> /etc/ttys

		[ /dev/tty$1$lstr ] && {

			# Here we are adding to fix the stupid bug of XENIX that can't
			# .. handle the minor number 08 and 09 this could be XENIX can only
			# .. dealing with octal number instead.
			#

# -- b -- (25/04/93)-OS
#
			mnum=`expr $lstr + $4`

			if [ $1 -lt 10 ]
			then
				mnum=`echo $3 | awk '{ printf("%01d", $1) }'`
			fi
# For debug
#	echo "mknod /dev/tty${1}$lstr c $MAJOR $mnum"
#	read ot

#			mknod /dev/tty${1}$lstr c $MAJOR $mnum
			mknod /dev/tty${1}$lstr c $MAJOR $4
			chmod 666 /dev/tty${1}$lstr
		}

# -- e --

		[ /dev/tty$m ] && {
			mknod /dev/tty$m c $MAJOR $mdm
			chmod 666 /dev/tty$m
		}
	fi
	grep -s tty$1$lstr /etc/ttytype > /dev/null \
	  || echo "unknown\ttty$1$lstr" >> $tmp2
	grep -s tty$m /etc/ttytype > /dev/null \
	  || echo "dialup\ttty$m" >> $tmp2
}


# Xenix.  Unix deletes old nodes and inittab entries when the kernel
# environment is rebuilt.  For Xenix we build a configuration file
# so that we know which nodes and tty entries to extirpate.
#

killnodes() 
{
        echo "Removing device nodes and /etc/ttys entries"

        cd /etc

        if [ "$OS" = "xenix" ]
        then
                cp ttys ttys.00
                cp ttytype ttytype.00
		cp /usr/lib/bbi/bbittys /tmp/bbittys.old
		echo "Removing node:"
                trap "mv ttys.00 ttys; mv ttytype.00 ttytype; exit $FAIL" 1 2 3 15
                cat /usr/lib/bbi/bbittys | while read x
                do
                        [ -c /dev/$x ] && {
				echo "/dev/$x\r\c"
                                rm /dev/$x
                                grep -v $x ttys > /tmp/ttys
                                mv /tmp/ttys ttys
                                grep -v $x ttytype > /tmp/ttytype
                                mv /tmp/ttytype ttytype
                        }
                        echo "/dev/$x\r\c"
                done
                rm ttys.00 ttytype.00
                echo "\n"
		echo "A listing of the removed nodes is in the file /tmp/bbittys.old"
        else
                cp ttytype ttytype.00
		cp /usr/lib/bbi/bbittys /tmp/bbittys.old
		echo "Removing node:"
                trap "mv ttytype.00 ttytype; exit $FAIL" 1 2 3 15
                cat /usr/lib/bbi/bbittys | while read x
                do
                        [ -c /dev/$x ] && {
				echo "/dev/$x\r\c"
                                rm /dev/$x
                                grep -v $x ttytype > /tmp/ttytype
                                mv /tmp/ttytype ttytype
                        }
                done
		echo "\n"
		echo "A listing of the removed nodes is in the file /tmp/bbittys.old"	
                rm ttytype.00
        fi

        cd $CONFDIR
        return $OK
}

# main

set_trap

permschk

linkchk

get_os || cleanup $FAIL

prodchk || cleanup $FAIL

getconf || cleanup $FAIL

buildrc || cleanup $FAIL

#getbrds || cleanup $FAIL

confadd || cleanup $FAIL

if [ "$OS" = "unix" ]
then
        updspace || cleanup $FAIL
else
        updxconf || cleanup $FAIL
        asmxconf || cleanup $FAIL
        updxlink || cleanup $FAIL
fi

updinit || cleanup $FAIL

asklink || cleanup $OK

klink || cleanup $FAIL

#/tcb/bin/ttys_update
#/tcb/bin/ttyupd

cleanup $OK
