#      @(#)dlk	1.0 SCO Unix 3.2 TCP/IP/NFS  source
# 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

# 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 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
    return 0
}

#
# Edit the sdevice file to remove the driver
edrmvdlk() {
		ed -s $1 << TOAST > /dev/null
/^$1/
d
a
dlk0	N	4	5	1	2	300	31f	d0000	d1fff
.
w
q
TOAST
}

# D-link driver banner message
dlk_prompt() {
/bin/cat - <<	EOF

		D-Link Network adapter setup

There are four parameters which you will need to supply :

	1. The Interrupt Vector Number
	2. Base I/O address
	3. Base memory address
	4. PCMCIA Slot Number

If you wish to go back and reconfigure your board or read the
D-Link adapter documentation answer no to the next question.

EOF
}

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)
    }'
}

# main()

cd /
tput clear

# Add drivers via configure
#

while	echo "Do you wish to install or delete the dlk driver (i/d/q) \c"
do	read ANSWER
	case $ANSWER in
	I|i)
		break
		;;
	D|d)
		cd /etc/conf/pack.d
		idinstall -d -e dlk0
		cd /etc/conf/sdevice.d
		edrmvdlk dlk0
		cd /
#		grep -v dlk /etc/tcp > /tmp/tcp && cp /tmp/tcp /etc/tcp
#		grep -v dlk /etc/strcf > /tmp/strcf && cp /tmp/strcf /etc/strcf
#		rm -f /tmp/tcp /tmp/strcf
		if [ -f /etc/strcf.old ]
		then
			cp /etc/strcf.old /etc/strcf
			rm /etc/strcf.old
		fi
		# 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
#
# Deal with the D-Link driver
#
dlk_prompt

getyn "Do you want to continue?"
if [ $? = "1" ]
then
	p "You can rerun mkdev at a later time"
	exit 0
fi

vect=5
ioaddr=300
ioend=31f
maddr=d0000
mend=d3fff
slot=0

while	p "Interrupt vector number [5]: \c "
do	read vect
	case "$vect" in
	"")
		vect=5;
		break;
		;;
	2)
		vect=5;
		break;
		;;
	[0-9]*)
		break;
		;;
	*)
		p "Vector number must be decimal digits"
		;;
	esac
done

while	p "IO base address (in hexadecimal) [300] : \c"
	do	read ioaddr
		case "$ioaddr" in
		"")
			ioaddr=300
			break;
			;;
		[0-3][0-9ABCDEFabcdef]?)
			case "$ioaddr" in
			3f[!0])
				p "Address must be 3 hexadecimal digits"
				;;
			*)
				break;
				;;
			esac
			;;
		*) p "Address must be 3 hexadecimal digits"
			;;
		esac
	done

while	p "Memory base address (in hexadecimal) [d0000]: \c"
do	read maddr
	case "$maddr" in
	"")
		maddr=d0000
		break;
		;;

	[0-9,a-f,A-F]*)
		break;
		;;
	*) p "Address must be hexadecimal digits"
		;;
	esac
done

while	p "PCMCIA slot number [0]: \c"
do	read slot
	case "$slot" in
	"")
		slot=0
		break;
		;;
	[0-9]*)
		break;
		;;
	*) p "Slot number must be decimal "
		;;
	esac
done

mend=`addhex $maddr 3fff`
ioend=`addhex $ioaddr 1f`

cd /tmp
if [ ! -d dlk ] ; then
	mkdir dlk
fi
cd dlk
# system
echo "dlk0      Y       4       5       1       $vect   $ioaddr $ioend  $maddr  $mend" > ./System
# node
echo "clone     dlk0    c       dlk0" > ./Node
# master
echo "dlk0      I       iScH            dlk     0       0       1       4       -1" > ./Master
# space.c
echo "unsigned long DLKbase = 0x$maddr;" > ./Space.c
echo "unsigned short DLKport = 0x$ioaddr;" >> ./Space.c
echo "unsigned char DLKirq = 0x$vect;" >> ./Space.c
echo "unsigned char DLKslot = 0x$slot;" >> ./Space.c
echo "int Protocol = 0;" >> ./Space.c
# Driver.o
	cp /usr/lib/tcprt/dlk/de65n.o ./Driver.o
if [ -d /etc/conf/pack.d/dlk0 ] ; then
	idinstall -u -m -s -n -o -p -e dlk0
else
	idinstall -a -e dlk0
	/etc/conf/bin/idmknod dlk0
fi
cd .. ; rm -rf dlk

echo ""

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 "\n$set will not work until the kernel is relinked." >&2
	cleanup 0
    fi
else
    relink
fi

# install if requested
#
echo ""
#while echo "Do you want to modify /etc/strcf(y/n)? \c"
#do read yn
#	case $yn in
#	Y|y)
		if [ ! -f /etc/strcf.old ]
		then
			cp /etc/strcf /etc/strcf.old
		fi
		cp /usr/lib/tcprt/dlk/strcf /etc/strcf
#		break
#		;;
#	N|n)
#		break
#		;;
#	*)
#		echo "Please answer y/n"
#		;;
#	esac
#done
echo ""
echo "reboot the system\n" >&2
