:
#	@(#) mkdev.eccd 23.5 91/11/23 
#
#	Copyright (C) 1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	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) Corollary, Inc., 1986, 1987, 1988, 1989, 1990, 1991.
#	All Rights Reserved.
#	This Module contains Proprietary Information of
#	Corollary, Inc., and should be treated as Confidential.
#
#	mkdev/eccd - Corollary ECC Daemon Configuration

LANG=english_us.ascii
PATH=/etc:/bin:/usr/bin:/etc/conf/bin:/tcb/bin:

export LANG PATH

IDRC="/etc/idrc.d/ecc"
IDSD="/etc/idsd.d/ecc"
RCD="/etc/conf/rc.d/ecc"
SDD="/etc/conf/sd.d/ecc"

: ${OK=0}  ${FAIL=1}  ${STOP=10}  ${HALT=11}

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

cleanup()
{
	trap '' 1 2 3 15
	rm -f /dev/z$$
	exit $1
}

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

getyn()
{
	while	echo "$* (y/n)? \c" >&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 				;;
		[nN])	return $FAIL				;;
		*)	echo "\nPlease answer y or n" >&2	;;
		esac
	done
}

error()
{
	echo "\nError: $*" >&2
	exit $FAIL
}

ECCprompt()
{
	while	
	quit=" or enter q to quit:"
	mesg="\tECC Daemon Initialization Program\n
	1. Install ECC Daemon.
	2. Remove ECC Daemon.
	h. Help

	
Select an option"
do	
	prompt || cleanup $OK
	x=$cmd
	case $x in
	1)	mode=install
		ECCprompt_more || return $FAIL
		break						;;
	2)	getyn "Are you sure" && {
			mode=remov
			break;
		}						;;
	q|Q)	cleanup $OK					;;
	\?|h|H)	echo "
	This procedure is used to install or remove the starting of the
	ECC daemon.  The script that controls this is $IDRC and
	is run when entering multi-user mode.

	The ECC daemon itself is a program that scans all of memory checking
	for single-bit errors.  Single-bit errors themselves are harmless
	and automatically corrected by hardware.  However, if an additional
	bit is corrupted, a double-bit error will occur and the system will
	shutdown.

	So, the ECC daemon helps avoid double-bit errors by letting the
	system administrator know of existing single-bit errors.  This
	discovery is relayed via the system console and /usr/adm/messages.
	The system administrator should periodically check /usr/adm/messages
	for any single-bit error notifications and use the ecc utility
	to remove single-bit error pages from the system.

	The ECC daemon should be run once a day; the defaults shown below
	are reasonable.

	If the removal option is run, the $IDRC script is
	simply removed.

	If the install options is run, the $IDRC script is created
	based upon these selectable options:

		1. Begin memory page scan.
		2. Check for single-bit errors.

	The above defaults are all individually selectable.

	" | pg -e -p "Press return to continue "; continue	;;
	*)	echo "\nEnter 1, 2, h or q" >&2			;;
	esac
done
	return $OK
}

ECCprompt_more()
{
	while	
	quit=" or enter q to quit:"
	mesg="
	1. Begin memory page scan.
	2. Check for single-bit errors.
	3. Install current or default choices and exit.

	
Select an option"
do
	prompt || cleanup $OK
	x=$cmd
	case $x in
	1) ECCtopt; continue					;;
	2) ECCfopt; continue					;;
	3) return $OK						;;
	q|Q) cleanup $OK					;;
	*)  echo "\nEnter 1, 2, 3 or q\n" >&2		;;
	esac
done
}

ECCtopt()
{
	while	
	quit=" or enter q to quit:"
mesg="
Enter period, in hours, to begin memory page scan\n(default ${topt})"
do
	prompt ${topt} || cleanup $OK
	x=$cmd
	case $x in
	[qQ])	return $OK ;;
	*)	expr $x + 1 >/dev/null 2>&1
		[ $? = 0 ] || {
			echo "Error: must be numeric"
			continue
		}
	esac
	topt=$x
	return $OK
done
}

ECCfopt()
{
	while	
quit=" or enter q to quit."
mesg="Enter period, in minutes, to check for single-bit errors\n(default $fopt)"
do
	prompt ${fopt} || cleanup $OK
	x=$cmd
	case $cmd in
	[qQ])	return $OK	;;
	*)
		expr $x + 1 >/dev/null 2>&1
		[ $? = 0 ] || {
			echo "Error: must be numeric"
			continue
		} ;;
	esac
	fopt=$x
	return $OK
done
}

ECCinstall()
{
	echo "Memory page scan period is ${topt} hours."
	echo "Single-bit error check period is ${fopt} minutes."
	echo "Creating $IDRC..."
	echo ":
#
# start ECC daemon
#
rm -f /etc/eccdpid
/etc/eccd -t $topt -f $fopt" >$IDRC
	chmod 744 $IDRC
	ln $IDRC $RCD
	[ ! -d /etc/idsd.d ] && {
		mkdir /etc/idsd.d
		chgrp sys /etc/idsd.d
		chmod 755 /etc/idsd.d
	}
	echo ":
#
# stop ECC daemon
#
/etc/eccd -s" >$IDSD
	chmod 744 $IDSD
	ln $IDSD $SDD
}

ECCremove()
{
	rm -f $IDRC
	rm -f $IDSD
	rm -f $RCD
	rm -f $SDD
	echo "The ecc scripts have been removed from the system.\n"
}

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

#########Script begins here.

set_trap
clearscr
topt=24
fopt=10

if  grep -s  "%ecc" /dev/string/cfg >/dev/null 2>&1
then
      : do nothing
else
      error "ECC Daemon Initialization Program can only be run on\nmachines with Corollary or Corollary compatible architectures."
fi

ECCprompt || cleanup $FAIL

case $mode in
install) ECCinstall || cleanup $FAIL			;;
remov)	 ECCremove					;;
esac

cleanup $OK
