#
# set -x
# Globals
#
PATH=/bin:/usr/bin:/etc:/etc/conf/bin
tmp=/tmp/tcp$$
ROOT=
CONF=${ROOT}/usr/sys/conf
DEVDIR=${ROOT}/dev
ETC=${ROOT}/etc
TCP_CONF=${ROOT}/usr/lib/tcprt
TCP_IF=${TCP_CONF}/if.conf
TCPPERM=${ETC}/perms/tcprt			# permlist of this product
INET=${ROOT}/usr/sys/inet
FAIL=0                          ## Added by Aaron on 12/01/95
OK=1                            ## 

# function definitions
#
# usage: safemv file1 file2
# move file1 to file2 without chance of interruption
safemv() {
	trap "" 1 2 3 15
	mv $1 $2
	trap 1 2 3 15
}

# usage: safecopy file1 file2
# copies file1 to file2 without chance of interruption
safecopy() {
	trap "" 1 2 3 15
	cp $1 $2
	trap 1 2 3 15
}

#
# prompt the user to answer a yes no question or 'q' to quit
# Usage:
#       prompt_yn "Message" default
prompt_yn() {
        mesg=$1
        default=$2

        while :
        do
                echo "${mesg} (y/n) [${default}] or 'q' to quit: \c"
                read result

                case $result in
                q|Q) result="Q"; return $FAIL;;
                y|Y) result="Y"; return $OK;;
                n|N) result="N"; return $OK;;
                "") result=`echo $default | tr "yn" "YN"`; return $OK;;
                esac

                echo "Illegal value, please type 'y' 'n' or 'q'"
        done
}

# Prompt for yes or no answer with message passed as argument -
# returns non-zero for no
getyn() {
	while	echo "\n$* (y/n/q) \c">&2
	do	read yn rest
		case $yn in
			[yY]) return 0				;;
			[nN]|[qQ]) return 1			;;
		*)	echo "Please answer y,n or q" >&2       ;;
		esac
	done
}

# print a prompt
#
p() {
	set -f
	/bin/echo $*
	set +f
	return $?
}

# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}



# Re-link new kernel
relink() {
    cd /etc/conf/bin
    if [ -z "$_NOPROMPT" ] ; then
	echo "\nRe-linking the kernel ... \c" >&2
    fi
    ./idbuild -B 
    return 0
}

#
# Edit the sdevice file to remove the driver
edrmvdlk() {
		ed -s $1 << TOAST > /dev/null
/^$1/
d
a
dlkpci	N	4	5	1	2	0	0	0	0
.
w
q
TOAST
}

addhex() {
echo "$1 $2" | nawk ' {
	printf("%x", atoh($1) + atoh($2));
    }
    function atoh(number) {
	i = 0
	j = length(number)
	result = 0
	while ( i < j ) {
	    result = result + (hexval(substr(number,j - i,1)) * (16 ^ i))
	    i = i + 1
	}
	return result
    }
    function hexval(letter) {
	if (letter == "a") return 10 + 0
	if (letter == "b") return 11 + 0
	if (letter == "c") return 12 + 0
	if (letter == "d") return 13 + 0
	if (letter == "e") return 14 + 0
	if (letter == "f") return 15 + 0
	return (letter + 0)
    }'
}

#
# Prompt the user to make a selection a list of values
# Usage:
#       prompt_select "Message" default "value_list"
prompt_select() {
        mesg=$1
        default=$2
        values=$3

        while :
        do
                echo "${mesg} (${values}) [${default}] or 'q' to quit: \c"
                read result
                case $result in
                Q|q)
                        result="Q"
                        return $FAIL
                        ;;
                "")
                        result=$default
                        ;;
                esac

                for i in $values
                do
                        if [ "$i" = "$result" ]
                        then
                                return $OK
                        fi
                done
                echo "Illegal value, must be one of (${values})"
        done
}

#
# select_PCI()
#
# Let user to select PCI type used by driver to figure out PCI configuration
# space.
#
select_PCI() {
        PCI_type=0;
        
        echo ""
        echo "Note:"
        echo "  The default is to use the PCI BIOS calls (in 32-bit protected"
        echo "  mode). Some systems may not support these calls, in which case"
        echo "  there will be problems at boot time. If using a system that"
        echo "  does not support 32-bit protected mode PCI BIOS calls, direct"
        echo "  hardware accesses must be used."
        echo "  PCI mechanism 1 or PCI mechanism 2 should be selected depending"
        echo "  on your machine support."
        echo ""
        echo ""
        echo "Please specify the way used by PCI to access hardware........."
        echo ""
        echo "  0. Use 32-bit protected mode PCI BIOS calls."
        echo "  1. Direct access to the HW, using PCI mechanism 1."
        echo "  2. Direct access to the HW, using PCI mechanism 2."
        echo ""
        prompt_select " Enter number of PCI access type selection" $PCI_type "0 1 2" 
        if [ "$result" = "Q" ] 
        then
                  cleanup $FAIL 
        fi
        PCI_type=$result
        echo ""
        case $PCI_type in
          0) echo "PCI access type : Use 32-bit protected mode PCI BIOS calls";
             PCIaccess="DCE_USE_PCI_BIOS";
             ;;
          1) echo "PCI access type : Direct access to the HW, using PCI mechanism 1.";
             PCIaccess="DCE_USE_PCI_MECHANISM1";
             ;;
          2) echo "PCI access type : Direct access to the HW, using PCI mechanism 2.";
             PCIaccess="DCE_USE_PCI_MECHANISM2";
             ;;
        esac
        echo ""
        ed -s /tmp/dlk/Space.c << TOAST > /dev/null
/#define DCE_HW_ACCESSES/
d
i
#define DCE_HW_ACCESSES           $PCIaccess
.
w
q
TOAST

}

#
# system_dce()
#
# function to produce the info for the System file for the DC21x4
# boards
#
# The info produced :
#
# The interrupt level - from the user
# The slot number (and IO space used by the board ) - from the user
# The buffers consumed by the driver.
#
system_dce() {
	bd=$1

#
# Added by Aaron on 11/24/95
#
        if [ $bd -eq 0 ]
        then    
                select_PCI
        fi
        
        MediaType=0;
        LineSpeed=0;

        echo "Please choose the line speed that you want............"
        echo ""
        echo "  0. Use default value."
        echo "  1. 10 MB/sec mode."
        echo "  2. 100 MB/sec mode."
        echo "  3. Automatic detection between 10 MB/sec and 100 MB/sec mode."
        echo ""
        prompt_select " Enter number of line speed selection" $LineSpeed "0 1 2 3" 
                LineSpeed=$result
        echo ""
        case $LineSpeed in
          0) echo "Line Speed : Use default value.";
             SerialTXT="XXX_DC21140_DEF_SERIAL_MODE";
             ;;
          1) echo "Line Speed : 10 MB/sec mode.";
             SerialTXT="XXX_TP";
             ;;
          2) echo "Line Speed : 100 MB/sec mode.";
             SerialTXT="XXX_100TX";
             SIAModeTXT="XXX_DC2104X_DEF_SERIAL_MODE";
             ;;
          3) echo "Line Speed : AUTOSENSE_MODE";
             SerialTXT="XXX_AUTOSENSE";
             ;;
        esac
        echo ""
##        if [ $LineSpeed -eq 2 ]
##        then
##          prompt_yn "Do you want to use point to point full duplex mode?" "n" || cleanup $FAIL 1
##          [ $result = "Y" ] && SerialTXT="XXX_SYM_SCR_FULL_DUPLEX"
##        else
          echo "Please choose the media mode that you want............"
          echo ""
	  echo "  0. Use default value."
          echo "  1. Point to point full duplex twisted pair connection."
#
# Masked by Aaron on 03/14/96
#
##          echo "  2. AUI connection."
          echo "  2. BNC connection."
          echo "  3. Twisted pair connection."
          echo "  4. Automatic detection between TP and BNC."
          echo "  5. Twisted pair connection with linkfail test ignore & set polarity plus."
          echo ""
          prompt_select " Enter number of media mode selection" $MediaType "0 1 2 3 4 5" 
               MediaType=$result
          echo ""
          case $MediaType in
	     0) SIAModeTXT="XXX_DC2104X_DEF_SERIAL_MODE";
        	echo "Media Mode : Use default value.";
		;;
             1) SIAModeTXT="XXX_TP_FULL_DUPLEX";
                SerialTXT="XXX_TP_FULL_DUPLEX";
                [ $LineSpeed -eq 2 ] && SerialTXT="XXX_100TX_FULL_DUPLEX";
                echo "Media Mode : TP_FULL_DUPLEX_MODE";
                ;;
#
# Masked by Aaron on 03/14/96
#
##             2) SIAModeTXT="XXX_AUI";
##                echo "Media Mode : AUI_MODE";
##                ;;
             2) SIAModeTXT="XXX_BNC";
                echo "Media Mode : BNC_MODE";
                ;;
             3) SIAModeTXT="XXX_TP";
                echo "Media Mode : TP_MODE";
                ;;
             4) SIAModeTXT="XXX_AUTOSENSE";
                echo "Media Mode : AUTOSENSE_MODE";
                ;;
             5) SIAModeTXT="XXX_TP_LINK_DISABLE";
                echo "Media Mode : TP_LINK_DISABLE_MODE";
                ;;
          esac
##        fi

        ed -s /tmp/dlk/Space.c << TOAST > /dev/null
/#define XXX_DC2104X_SIA_MODE_$bd/
d
i
#define XXX_DC2104X_SIA_MODE_$bd        $SIAModeTXT
.
/#define XXX_DC21140_SERIAL_MODE_$bd
d
i
#define XXX_DC21140_SERIAL_MODE_$bd        $SerialTXT
.
w
q
TOAST

##
## Added by Aaron on 04/02/96 
## to solve the problem of add/remove driver on SCO OpenServer
## if "dlkpci0" line still in sdevice and is 'N' then add driver and
## re-build will cause 
## kslgen: "cannot determine major/minor numbers for node /dev/dlkpci0
##
##        ed -s /etc/conf/cf.d/sdevice << TOAST > /dev/null
##/dlkpci$brd
##d
##.
##w
##q
##TOAST

    BIO=0
    EIO=0
    IRQ=0

        currdir=`pwd`

###        cd /etc/conf/cf.d

###        echo "Modifying Streams Data Configuration Parameters"

###        sparam=0
###        sparam=`./configure -y NBLK2048`
###       echo -n Value of NBLK2048 changed from $sparam t
###        sparam=`expr $sparam + 10`
###        echo o $sparam
###        idtune -f NBLK2048 $sparam

###        sparam=0
###        sparam=`./configure -y NBLK64`
###        echo -n Value of NBLK64 changed from $sparam t
###        sparam=`expr $sparam + 10`
###        echo o $sparam
###        idtune -f NBLK64 $sparam

###        cd $currdir

        NMINORS="1"
}

# main()

cd /
tput clear

# Add drivers via configure
#

while	echo "Do you wish to install or delete the network driver (i/d/q) \c"
do	read ANSWER
	case $ANSWER in
	I|i)
		break
		;;
	D|d)
		cd /etc/conf/bin
		idinstall -d -e dlkpci
                /usr/sbin/netinfo -r -d dlkpci_3
                /usr/sbin/netinfo -r -d dlkpci_2 
                /usr/sbin/netinfo -r -d dlkpci_1 
                /usr/sbin/netinfo -r -d dlkpci_0 
                /etc/confnet.d/configure -r -p inet -d dlkpci_3
                /etc/confnet.d/configure -r -p inet -d dlkpci_2 
                /etc/confnet.d/configure -r -p inet -d dlkpci_1 
                /etc/confnet.d/configure -r -p inet -d dlkpci_0 
		cd /
		# Relink the kernel with the new driver information
		if [ -z "$_NOPROMPT" ] ; then
		    if getyn "\nDo you wish to relink the kernel now?"
		    then
		      relink
		    else
		      echo "\nChanges will not take effect until the kernel is relinked." >&2
		    fi
		else
		    relink
		fi
		cleanup 0
		;;
	Q|q)
		cleanup 0
		;;
	*)	echo "Please answer 'i', 'd' or 'q'."
		;;
	esac
done
echo	" "
#
# Deal with the D-Link driver
#
#dlk_prompt

#getyn "Do you want to continue?"
#if [ $? = "1" ]
#then
#	p "You can rerun at a later time"
#	exit 0
#fi
#echo	" "

cd /tmp
if [ ! -d dlk ] ; then
	mkdir dlk
fi
cd dlk

# space.c
##echo "unsigned long DE220_BOARDS = $brd;" > ./Space.c
##echo "unsigned long de220intl[] = { $vect0, $vect1, $vect2, $vect3 };" >> ./Space.c
##echo "unsigned long de220ioaddr[] = { 0x$ioaddr0,0x$ioaddr1,0x$ioaddr2,0x$ioaddr3 };" >> ./Space.c
if [ ! -f /usr/lib/tcprt/dlk/spacedce.c ] ; then
	echo ""
	echo "Can not find Space.c at /usr/lib/tcprt/dlk directory"
	exit 0
fi
cp /usr/lib/tcprt/dlk/spacedce.c ./Space.c
cd /

brd=0

cfg0=N
cfg1=N
cfg2=N
cfg3=N

while getyn "Do you want to configure Board_$brd?"
do
    echo "\nBoard_$brd configuring ..."

    case "$brd" in
    0)
	cfg0=Y
	;;
    1)
	cfg1=Y
	;;
    2)
	cfg2=Y
	;;
    3)
	cfg3=Y
	;;
    esac


    /usr/sbin/netinfo -a -d dlkpci_$brd
    /etc/confnet.d/configure -i -p inet -d dlkpci_$brd

    ###
    ### Let user change the setting of DFE-500 card
    ### This call must be put after 'idinstall' because /etc/conf/pack.d/dlkpci/space.c 
    ### is created after 'idinstall'
    ###
    system_dce  $brd    

    brd=`expr $brd + 1`

done

cd /tmp
if [ ! -d dlk ] ; then
	mkdir dlk
fi
cd dlk


#
# mdevice(4)
#
# dlkpci   -       Device name
# Ihp   -       Function list, init routine, halt routine, poll routine
# iScHM -       Characteristics of driver, installable, STREAMS, character,
#               control hardware, multiple major numbers
# DCE   -       Handler prefix, ex: DCEinit()...
# 0     -       Block Major number
# 0-3   -       Character Major number
# 1     -       Minimum units
# 4     -       Maximum units
# -1    -       DMA channel, -1 means does not use DMA
#
echo "dlkpci      Ihp  iScHM  dce   0       0-3       1       4       -1" > ./Master


#
# sdevice(4)
#
# dlkpci    -      Device name               
# Y/N    -      Configure
#   4    -      Unit
#   5    -      IPL (Interrupt Privilege Level)
#   0    -      Type, does not require an interrupt line
#   0    -      Vector, no vector
#   0    -      SIOA, Start I/O Address
#   0    -      EIOA, End I/O Address
#   0    -      SCMA, Start Controller Address
#   0    -      ECMA, End Controller Address
#
echo "dlkpci  $cfg0  4  0   0   0    0    0  0  0" > ./System
if [ $brd -gt 1 ]; then
   echo "dlkpci  $cfg1  4  0   0   0    0    0  0  0" >> ./System
fi
if [ $brd -gt 2 ]; then
   echo "dlkpci  $cfg2  4  0   0   0    0    0  0  0" >> ./System
fi                              
if [ $brd -gt 3 ]; then
   echo "dlkpci  $cfg3  4  0   0   0    0    0  0  0" >> ./System
fi                          

#
# idmknod(1M)
#
# clone         -       DSP internal name
# dlkpci_0         -       name of node to be inserted
# c:0           -       character device and major number 0        
# dlkpci           -       minor device number
#
echo "clone     dlkpci_0    c:0       dlkpci" > ./Node
if [ $brd -gt 1 ] ; then
   echo "clone     dlkpci_1    c:1       dlkpci" >> ./Node
fi
if [ $brd -gt 2 ] ; then
   echo "clone     dlkpci_2    c:2       dlkpci" >> ./Node
fi
if [ $brd -gt 3 ] ; then
   echo "clone     dlkpci_3    c:3       dlkpci" >> ./Node
fi


# Driver.o
cp /usr/lib/tcprt/dlk/dlkpci.o ./Driver.o

###        echo "Modifying Streams Data Configuration Parameters"
/etc/conf/bin/idtune -m -c NBLK4096 10
/etc/conf/bin/idtune -m -c NBLK2048 100

if [ -d /etc/conf/pack.d/dlkpci ] ; then
	idinstall -u -m -s -n -o -p -e dlkpci
else
	idinstall -a dlkpci
fi


cd .. ; rm -rf dlk

echo ""

#while getyn "\n\tDo you want to bind netdriver and protocol?"
#do
#    /etc/confnet.d/configure -i
#done

cd /

# Relink the kernel with the new driver information
if [ -z "$_NOPROMPT" ] ; then
    if getyn "\nDo you wish to relink the kernel now?"
    then
	relink
    else
	echo "\nUNIX will not work until the kernel is relinked." >&2
	cleanup 0
    fi
else
    relink
fi

echo "\n\treboot the system\n" >&2


