:
#set -x
PATH=/bin:/usr/bin:/etc
tmp=/tmp/tcp$$
PERM=/etc/perms/inst                    # OS (link kit) permlist
objs=""
IFCONF=/usr/lib/tcp/if.conf
STRCF=/etc/strcf
DEVDIR=/dev

# Function Definitions
# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# 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 returns value of 0 if driver is present, or 1 if not.
confchk() {
	maj=`./configure -j $pref` || return 1
	return 0
}

# Check if driver is there, only remove if it is.
# Arguments are driver routine prefix, driver type (c, b, s).
rmvdriver() {
	pref=$1    typ=$2
	confchk && {
		./configure -d -$typ -m $maj > 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) \c">&2
	do      read yn rest
		case $yn in
		[yY])   return 0                                ;;
		[nN])   return 1                                ;;
		*)      echo "Please answer y or n" >&2         ;;
		esac
	done
}

# Test to see if link kit is installed
chklinkkit() {
    until       fixperm -i -d LINK $PERM
    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
}

# Re-link new kernel
relink() {
    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 with tape driver in default location
    ./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
}

# main()
cd /

# Clean up and exit after signals
trap "cleanup 1" 1 2 3 15

#
# stop tcp if the script exists
#
if [ -x /etc/tcp ]
then 
	/etc/tcp stop
fi

# Check to see that the link kit is installed, 
# since drivers cannot be removed without it.
chklinkkit

# Get the objectlist from the stored interface file
if [ -f ${IFCONF} ]
then
	. ${IFCONF}
	cp ${IFCONF} ${tmp}.if
	sed -e "/IF_DLINK/d" -e "/dlk_objs/d" ${tmp}.if > ${IFCONF}
else
	cleanup 0
fi

# Remove drivers via configure, and return permlist to original
# state to avoid errors during future installations.

echo "\nUpdating system configuration ... "

cd /usr/sys/conf

objs="${dlk_objs}"

if [ -n "${IF_DLINK}" ]
then
	rmvdriver dlk c && {
		cp $STRCF $tmp.strcf
		sed "/dlink / s!^ !# DLINK!" $tmp.strcf > $STRCF
		rm -f $DEVDIR/dlink0
	} || cleanup 1
else
	cleanup 0
fi

# Edit link_xenix to remove $objs 
cp link_xenix link_xenix.00 || {
	echo "Error:  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
	if grep -s "$i" link_xenix > /dev/null
	then
		cp link_xenix $tmp.link
		sed "s! $i ! !" $tmp.link > link_xenix
	fi
done
rm -f $tmp.link
trap 1 2 3 15
chmod 700 link_xenix

# Relink the kernel with the new driver information 
relink

# Install the kernel in the default location
installkernel
#
cleanup 0
