:
#
# @(#) mfgmkdev.sh 1.8 93/02/07 
#
# Copyright (C) 1992 The Santa Cruz Operation, Inc.
#
# The information in this file is provided for the exclusive use of the
# licensees of The Santa Cruz Operation, Inc.  Such users have the right 
# to use, modify, and incorporate this code into other products for 
# purposes authorized by the license agreement provided they include
# this notice and the associated copyright notice with any such 
# product.  The information in this file is provided "AS IS" without 
# warranty.
#
#	Copyright (C) Microfield Graphics, Inc., 1992.
#
# mfg - UNIX DMA Driver Installation/Removal
#

PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

tmp=/tmp/mfg.$$
CONFBIN=/etc/conf/bin
SCRIPTPATH=/usr/lib/vidconf/ihv/microfield/scripts

# Define return values
: ${OK=0} ${FAIL=1} ${OKDEL=4} ${STOP=10} ${HALT=11}

# Function definitions
######################

set_trap()  {	
	trap 'echo "Interrupted! Giving up..."; cleanup 1' 1 2 3 15
}

unset_trap()  {
	trap '' 1 2 3 15
}
 
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

clearscr() {
	[ "$TERM" ] && [ -x /bin/clear ] && clear 2> /dev/null
}

# Prompt for continuation
pause() {
	while	echo "\nHit [Enter] to continue\c">&2
	do	read yn rest
		case $yn in
		*)	return $OK			;;
		esac
	done
}

# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	echo "\n$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n${mesg}or enter q to ${quit}: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an argument use it as the default
			# else loop until 'cmd' is set
			[ "$1" ] && { 
				cmd=$1
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Print an error message
error() {
	echo "\nError:\t$*" >&2
}

# ------- Install Drivers -------- 	These routines install drivers in the
#					configuration files.

addmfg () {

	# mfgupdate updates the system configuration files in /etc/conf
	${SCRIPTPATH}/mfgupdate "${DRIVER}" || {
		error "The $DESCR installation has been aborted.\n"
		cleanup $FAIL 
	}

	return $OK
}

# ------- Configure Drivers -------- 	These routines modify the driver 
#					configuration files.

cfgmfg () {

	# mfgconfig allows user to set interrupt, I/O address and MEM address
	${SCRIPTPATH}/mfgconfig "${DRIVER}"
	case $? in
        $OKDEL) 				   # just deleted the driver
		touch /usr/lib/vidconf/.new_unix   # cause a relink in update.sh
		cleanup $OKDEL
		;;

	$OK) 					   # just configured a driver
		touch /usr/lib/vidconf/.new_unix   # cause a relink in update.sh
		;;

	*) 	error "The $DESCR configuration has been aborted.\n"
		cleanup $FAIL 
		;;
	esac

	return $OK

}


# main()
########

set_trap


DESCR="Microfield T8, T8/2, V8 and V8/2 DMA Driver /dev/mfg"
DRIVER=mfg

${CONFBIN}/idcheck -p ${DRIVER} 2>> $ERRFILE
if [ $? != 0 ]
then
    # prompt allows user to skip configuring the driver if already installed
    echo "It appears that the Microfield DMA Driver is already installed."
    getyn "Do you want to remove or reconfigure the kernel driver?" || {
	cleanup $OK
    }
else
    echo "Installing the Microfield T8, T8/2, V8 and V8/2 DMA Driver."
fi

cfgmfg					# configure (or delete) the driver
addmfg					# add driver to system configuration

cleanup $OK

