:
#	@(#) graphics.sh 7.3 93/02/07 
#
#       Copyright (C) The Santa Cruz Operation, 1989.
#       This Module contains Proprietary Information of
#       The Santa Cruz Operation, and should be treated as Confidential.
#

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

PARSER_DIR=/usr/lib/vidconf
export PARSER_DIR

LINK_COMMAND=link_unix
LINK_DIR=/etc/conf/cf.d
CLASS_DIR=/etc/conf/pack.d/cn
export LINK_COMMAND LINK_DIR CLASS_DIR 

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

# FUNCTIONS
#########################

# ---------- STANDARD ROUTINES -------- These routines are common to scripts
#					requiring kernel relinking.

# Define traps for critical and non critical code.
set_trap()  {	
	trap 'echo "Interrupted! Exiting ..."; cleanup 1' 1 2 3 15
}

unset_trap()  {
	trap '' 1 2 3 15
}
 
# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	exit $1
}

# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	echo "$* (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 function
prompt () { 
    while echo "\n${mesg}$quit \c" >&2
    do
        read cmd
        case $cmd in
            Q|q)
                return $FAIL
                ;;
            +x|-x)
                set $cmd
                ;;
            !*)
                eval `expr "$cmd" : "!\(.*\)"`
                ;;
            "") # "quit" is null when prompt is "press return to continue"
                [ -z "$quit" ] && return $OK
                # if there is an argument use it as the default
                # else loop until cmd is set
                [ "$1" ] && { 
                    cmd=$1
                    return $OK
                }
                error Invalid response
                ;;
            *)  return $OK
                ;;
        esac
    done
}

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

# Configure error message
conferr()  {
	error "\nConfigure failed to update system configuration.
Check /etc/conf/cf.d/conflog for details."
}

# perms list needed if link kit must be installed
permschk () {
	if [ -f /etc/perms/extmd ]; then
		PERM=/etc/perms/extmd
	elif [ -f /etc/perms/inst ]; then
		PERM=/etc/perms/inst
	else
		error "Cannot locate linkkit permlist needed to verify
 linkkit installation"
		cleanup $FAIL
	fi
}

# test to see if link kit is installed
linkchk()  {
	cd /
	until	fixperm -i -d LINK $PERM
	do	case $? in
		4)  echo "\nThe Link Kit is not installed." >&2	;;
		5)  echo "\nThe Link Kit is only partially installed." >&2  ;;
		*)  echo "\nError testing for Link Kit.  Exiting." >&2; cleanup $FAIL  ;;
		esac

		# Not fully installed. Do so here
		getyn "\nDo you want to install it now?" || {
			# answered no
			echo "
The link kit must be installed to run this program.  Exiting ..."
			cleanup $OK
		}

		# answered yes, so install link kit
		echo "\nInvoking /etc/custom\n"
		/etc/custom -o -i LINK || {
			# custom exited unsuccessfully
			error "custom failed to install Link Kit successfully.  Please try again."
			cleanup $FAIL
		}
	done
}

asklink()  {
	getyn "
You must create a new kernel to effect the driver change you specified.
Do you want to create a new kernel now?"  ||  {
		echo "
To create a new kernel execute $LINK_DIR/$LINK_COMMAND. Then you
must reboot your system by executing  /etc/shutdown -i0  before the 
changes you have specified will be implemented.\n"
			return $FAIL 
		}
	return $OK
}

# re-link new kernel
klink() {
	cd $LINK_DIR
	./$LINK_COMMAND || { 
		echo "\nError: Kernel link failed."
		cleanup $FAIL
	}
	return $OK
}

##
## Main

if [ -f /usr/lib/vidconf/scripts/*.sh ]
then
	cd /usr/lib/vidconf/scripts

	for i in *.sh
	do
		./$i
	done
fi

# if the iqm file exists, iqm mode is on
if [ -f "$IQM_FILE" ]
then
	. $IQM_FILE
	if [ -n "$IQM_VIDEO_CARD" -a -n "$IQM_VIDEO_MODE" -a -n "$IQM_VIDEO_MONITOR" ]
	then
		exec /usr/lib/vidconf/vidiqm $IQM_VIDEO_CARD $IQM_VIDEO_MODE $IQM_VIDEO_MONITOR
	fi
fi

# otherwise go interactive
/usr/lib/vidconf/vidconf

# Rebuild the kernel if $PARSER_DIR/.new_unix exists.  
# This file is created by a ~/vidconf/script for a video driver.
# Normally ~/vidconf/update.sh handles .new_unix. This is 
# a special case for the mfg/mgx kernel drivers.

set_trap

if [ ! -f ${PARSER_DIR}/.new_unix ]; then
	cleanup $OK
fi

rm -f ${PARSER_DIR}/.new_unix

#
# relink kernel occurs when $PARSER_DIR/.new_unix has been 
# created by VIDSCRIPT but the user selected Quit->Exit to exit
permschk
linkchk
if [ "$_RELINK" -o "$_NOPROMPT" ]
then
    klink
else
    asklink && klink  
fi
