:
#set -x
# Globals
#
LKPERM=/etc/perms/inst
PATH=/bin:/usr/bin:/etc
tmp=/tmp/tcp$$
pkg="TCPRT"				# permlist package name
set="SCO TCP Runtime"			# appears in prompts
objs=""					# driver objs
dlk_objs="../inet/dlink/dlink.o ../inet/dlink/dlinkiv.o"

ROOT=
CONF=${ROOT}/usr/sys/conf
DEVDIR=${ROOT}/dev
ETC=${ROOT}/etc
TCP_CONF=${ROOT}/usr/lib/tcp
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
}

# Check existence of a driver via configure, and set $maj (major dev number)
# configure (and confchk) returns value of 0 if driver is present, or 1 if not
confchk() {
	maj=`./configure -j $pref` || {
		maj=`./configure -j NEXTMAJOR`
		return 1
	}
	return 0
}

# Check for driver already there, only add if not there
# Arguments are driver routine prefix, driver functions, 
# driver type (c, b, s), interrupt priority level (optional), 
# and interrupt vector (optional).
adddriver() {
	IPL=    IV=
	pref=$1    fns=$2    typ=$3    ipl=$4    iv=$5
	confchk || {
		[ "$ipl" ] && IPL="-l $ipl"
		[ "$iv" ] && IV="-v $iv"
############### NEXT LINE IS FOR DEBUG ONLY ###########
######		echo $fns $typ $maj $IPL $IV
		echo "$pref:\n" > conflog
		./configure -a $fns -$typ -m $maj $IPL $IV >> conflog 2>&1 || {
			echo "
Error:  configure failed to update system configuration.
Check /usr/sys/conf/conflog for details." >&2
			return 1
		}
		rm -f conflog
	}
	return 0
}

# 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
}

# Test to see if link kit is installed
chklinkkit() {
    until	fixperm -i -d LINK $LKPERM
    do	case $? in
	4)	echo "The Link Kit is not installed." >&2		;;
	5)	echo "The Link Kit is only partially installed." >&2	;;
	*)	echo "Error testing for Link Kit. Exiting."; cleanup 1 ;;
	esac
	# Not fully installed. Do so here
	while	echo "Do you wish to install it now? (y/n) \c"
	do	read ANSWER
		case $ANSWER in
		Y|y)	custom -o -i LINK
			break
			;;
		N|n)	echo "Drivers cannot be installed without the Link Kit."
			cleanup 1
			;;
		*)	echo "Please answer 'y' or 'n'. \c"
			;;
		esac
	done
    done
}

chktcp() {
	fixperm -i -d ${pkg} ${TCPPERM}
	if [ $? = "0" ]
	then
		if [ ! -d ${DEVDIR}/inet ]
		then
			echo "Drivers cannot be installed without the TCPRT package."
			cleanup 1
		fi
	else
		echo "Drivers cannot be installed without the TCPRT package."
		cleanup 1
	fi
}

# Re-link new kernel
relink() {
    [ "$_RELINK" ] && return 0
    cd /usr/sys/conf
    echo "\nRe-linking the kernel ... \c" >&2
    ./link_xenix > kmakelog 2>&1 || {
	    echo "\nError:  Kernel link failed.
Check /usr/sys/conf/kmakelog for details." >&2
	    cleanup 1
    }
    rm -f kmakelog
    return 0
}

installkernel() {
    echo "\n\nKernel with driver modifications is in /usr/sys/conf/xenix">&2
    getyn "Do you want this kernel to boot by default?" || {
 	    # selected no.
	    echo "
Changes will not be reflected unless /usr/sys/conf/xenix is copied to /xenix,
or the pathname of the kernel with the changes is entered explicitly
at the 'Boot: ' prompt">&2
 	    return 0
    }
    # Yes, install kernel 
    ./hdinstall > hdinstlog 2>&1 || {
	    echo "\nError:  hdinstall failed to install new kernel in /xenix.
Check /usr/sys/conf/hdinstlog for details." >&2
	    return 0
    }
    rm -f hdinstlog
    echo "\nThe new kernel is installed in /xenix.
Reboot your system to activate it." >&2
}

# D-LINK driver banner message
dlkprompt() { 
/bin/cat - <<	EOF

			D-LINK SETUP

		Welcome to D-LINK XENIX TCP/IP !

EOF
}

# main()

cd /

# check to see if the drivers can be installed
#

chklinkkit

#
# check to see if TCP/IP is installed
#

#chktcp

#
# check on the existance of the configuration file for
# the "mkdev tcp" script
#
if [ ! -d ${TCP_CONF} ]
then
	mkdir ${TCP_CONF}
fi

# Add drivers via configure
#
cd $CONF

# Get the major device number of the clone driver
#
clonemaj=`./configure -j CLONE` || {
	echo "\nError: clone driver missing from /usr/sys/conf/master.
Restore the clone driver and resume this installation." >&2
	cleanup 1
}

while	echo "Do you wish to install or delete the dlink driver (i/d/q) \c"
do	read ANSWER
	case $ANSWER in
	I|i)	break
		;;
	D|d)	
		/usr/lib/mkdev/rmdlk
		cleanup $?
		;;
	Q|q)
		cleanup 0
		;;
	*)	echo "Please answer 'i', 'd' or 'q'."
		;;
	esac
done
#
#
#
#
dlkprompt
getyn "Do you want to continue?"
if [ $? = "1" ]
then
	p "You can rerun mkdev at a later time"
	exit 0
fi

#
#
# Deal with the Ethernet driver
#

while	p "Which driver will be installed (1.DE100 2.DE200) [1]: \c"
do	read board
	case "$board" in
	"")
		board=1;
		break 2;
		;;
	1|2)
		break 2;
		;;
	*)
		p "enter 1 or 2 only."
	esac
done

proto=0

while	p "Interrupt vector number [5]: \c"
do	read vect
	case "$vect" in
	2)
		vect=25;
		break 2;
		;;
	[3-7])
		break 2;
		;;
	8)
		vect=24;
		break 2;
		;;
	9)
		vect=25;
		break 2;
		;;
	1[0-9])
		vect=`expr $vect - 8 + 24`;
		break 2;
		;;
	"")
		vect=5;
		break 2;
		;;
	*)
		p "Vector number must be decimal digits"
		;;
	esac
done
echo "vector=${vect}"
while	p "IO base address (in hexadecimal) [300]: \c"
do	read iobase
	case "$iobase" in
	"")
		iobase=300
		break 2;
		;;
	
	[0-9,a-f,A-F]*)
		break 2;
		;;
	*) p "IO base must be hexadecimal digits"
		;;
	esac
done

memsize=2000
while	p "Memory base address (in hexadecimal) [d0000]: \c"
do	read membase
	case "$membase" in
	"")
		membase=d0000
		break 2;
		;;
	
	[0-9,a-f,A-F]*)
		break 2;
		;;
	*) p "Address must be hexadecimal digits"
		;;
	esac
done

if adddriver dlk "dlkinit dlkinfo" c 5 ${vect}
then
	if [ -c ${DEVDIR}/dlink0 ]
	then
		rm -f ${DEVDIR}/dlink0
	fi
	mknod ${DEVDIR}/dlink0 c ${clonemaj} ${maj}
	sed -e 's/# DLINK/     /' ${ETC}/strcf > $tmp.strcf 
	safecopy $tmp.strcf ${ETC}/strcf 
	rm -f $tmp.strcf
	if [ -f $TCP_IF ]
	then
		cp $TCP_IF $tmp.if
		sed -e "/IF_DLINK/d"  -e "/dlk_objs/d" $tmp.if > $TCP_IF
	fi
	echo "IF_DLINK=dlink" >> $TCP_IF
	cd $INET/dlink
	sed -e "s/__IRQ__/${vect}/" -e "s/__RAMBASE__/${membase}/" \
		-e "s/__IOBASE__/${iobase}/" -e "s/__PROTO__/${proto}/" \
	< ${INET}/dlink/dlinkiv.S > ${INET}/dlink/dlinkiv.s
fi
	cd $INET/dlink
	if [ $board = 1 ] ; then
		cp de100z.o dlink.o
	else
		cp de200z.o dlink.o
	fi
	/usr/lib/storel ${INET}/dlink/dlinkiv.s
	cd $CONF
	objs=${objs}${dlk_objs}

# First remove $objs if they are already (or partially) there
cp link_xenix link_xenix.00 || {
	echo "\n\nError:  cannot backup link_xenix." >&2
	cleanup 1
}

trap "mv link_xenix.00 link_xenix; cleanup 1" 1 2 3 15

for i in $objs
do	while grep -s $i link_xenix > /dev/null
	do	sed "s! $i!!" link_xenix > $tmp.link
		safecopy $tmp.link link_xenix  && rm -f $tmp.link
	done
done

# add $objs to link line
sed "s!c\.o!& $objs!" link_xenix > $tmp.link || {
	echo "\n\nError:  Cannot edit link_xenix." >&2
	mv link_xenix.00 link_xenix
	cleanup 1
}
#
# record the object list for later removal
#
echo "dlk_objs=\"${dlk_objs}\"" >> $TCP_IF

safecopy $tmp.link link_xenix  && rm -f $tmp.link
trap 1 2 3 15

chmod 700 link_xenix

# Relink the kernel with the new driver information 
if getyn "\nDo you wish to relink the kernel now?"
then
	eval $_RELINK relink
else
	echo "\n$set will not work until the kernel is relinked." >&2
	cleanup 0
fi

# install if requested
#
installkernel
