:
#ident @(#)en0.sh	1.1 91/02/14
#
# Altos Base I/O ethernet driver setup script
#
#set -x
# Globals
#
LKPERM=/etc/perms/inst
PATH=/bin:/usr/bin:/etc:/etc/conf/bin
tmp=/tmp/en0$$
ROOT=
CONF=${ROOT}/usr/sys/conf
DEVDIR=${ROOT}/dev
ETC=${ROOT}/etc

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

# 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 "Attempting to install/deinstall the driver anyway." >&2
		return 0
		;;
	*)	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 /etc/conf/cf.d
    if [ -z "$_NOPROMPT" ] ; then
	echo "\nRe-linking the kernel ... \c" >&2
    fi
    ./link_unix 2>&1 | tee -a kmakelog || {
	    echo "\nError:  Kernel link failed.
Check /etc/conf/cf.d/kmakelog for details." >&2
	    cleanup 1
    }
    rm -f kmakelog
    return 0
}

# main()

cd /

# check to see if the drivers can be installed
#

chklinkkit

while	echo "Do you wish to install or delete the en0 driver (i/d/q) \c"
do	read ANSWER
	case $ANSWER in
	I|i)	
		break
		;;
	D|d)	
		if getyn "\nDo you  want to continue ?"
		then
			echo "Removing en0 driver..."
			cd /etc/conf/sdevice.d
			echo "en0	N	1	5	1	10	1cd0	1cd2	0	0" > ./en0
			cd /
			# Relink the kernel with the new driver information 
			if getyn "\nDo you wish to relink the kernel now?"
			then
		    	eval $_RELINK relink
			else
		    	echo "\nChanges will not take effect until the kernel is relinked." >&2
		    	cleanup 0
			fi
			cleanup 0
		else
			echo "\nAborting remove."
			cleanup 1
		fi
		;;
	Q|q)
		cleanup 0
		;;
	*)	echo "Please answer 'i', 'd' or 'q'."
		;;
	esac
done
#
# Deal with en0
#
echo "Installing en0 driver..."
echo "en0	Y	1	5	1	10	1cd0	1cd2	0	0" > /etc/conf/sdevice.d/en0

# Relink the kernel with the new driver information 
if getyn "\nDo you wish to relink the kernel now?"
then
	eval $_RELINK relink
else
	echo "\nThe en0 driver will not work until the kernel is relinked." >&2
	cleanup 0
fi

# install if requested
#
echo ""
echo "To complete installation the system must be rebooted via init 6\n" >&2
