:
#	@(#)std_funcs	6.1 92/04/10

# M000 rogerk Tue Nov 10 16:10:12 PST 1992
#	- Removed nefarious "cd /"
#	- Previously set PATH variable now takes precedence

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

# set up the environment
PATH=${PATH:-/bin:/usr/bin:/etc}	# M000
LANG=english_us.ascii
export LANG PATH

# Prompt for yes or no answer - returns non-zero for no
#
# Usage: getyn "question" [ "more question" ... ]
# returns $OK on 'y' or 'Y'
# returns $FAIL on 'n' or 'N'
# loops on any other response
#
getyn() {
	while	:
	do	term_echo "\n$* (y/n) \c"
		read yn rest
		case $yn in
		[yY])	return $OK 				;;
		[nN])	return $FAIL				;;
		*)	term_echo "Please answer y or n"	;;
		esac
	done
}

# Prompt with mesg, return non-zero on quit
#
# Usage: prompt [ "default value" ]
# requires: variable mesg to be set to some message
# returns $FAIL on receiving a 'q' or 'Q'
# returns $OK on any other response
# returns $OK and sets cmd to the default value on 'return'
# notes: always sets 'cmd' to the value returned by the user or the
#        default value
# notes: responses beginning with a '!' are evaluated by the shell
# notes: responses of '-x' and '+x' cause a 'set -x' and 'set +x'
#
prompt() {
	while	:
	do	term_echo "\n${mesg}or enter 'q' to quit: \c"
		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 to a log and to standard error.
#
# Usage: error [ -l<logfile> ] "message"
# Arguments are an optional logfile name (or use a global var $LOGFILE) 
#       and a quoted error message
# Notes: error always returns $FAIL.
#
error() {
	# user can provide a logfile either in an argument
	# or as an environment variable: "LOGFILE"
	case $1 in
		-l*) LOGFILE=`expr "$1" : "-l\(.*\)"`
		     shift
		     ;;
	esac
	
	# put "ERROR" at the beginning of each line, for grep convenience
	estring=`echo "$*" | sed 's/^/ERROR: /'`
	[ $LOGFILE ] && echo "\n$estring" >> $LOGFILE

	echo "$estring" >&2
	return $FAIL
}

# Print a message
#
# Usage: message [ -l<logfile> ] "message"
# Notes: message always returns $OK.
# Arguments are an optional logfile name (or use a global var $LOGFILE) 
#       and a quoted warning message
#
message() {
	# user can provide a logfile either in an argument
	# or as an environment variable: "LOGFILE"
	case $1 in
		-l*) LOGFILE=`expr "$1" : "-l\(.*\)"`
		     shift
		     ;;
	esac
	[ $LOGFILE ] && echo "$*" >> $LOGFILE
	
	echo "$*"
	return $OK
}


# Print a warning message
#
# Usage: warning [ -l<logfile> ] "message"
# Arguments are an optional logfile name (or use global var $LOGFILE) 
#       and a quoted warning message
# Notes: warning always returns $FAIL.
#
warning() {
	# user can provide a logfile either in an argument
	# or as an environment variable: "LOGFILE"
	case $1 in
		-l*) LOGFILE=`expr "$1" : "-l\(.*\)"`
		     shift
		     ;;
	esac
	wmessage=`echo "$*" | sed 's/^/WARNING: /'`
	[ $LOGFILE ] && echo "\n$wmessage" >> $LOGFILE
	echo $wmessage >&2
	return $FAIL
}

# Print a message to a log file, not to the screen
#
# Usage: warning [ -l<logfile> ] "message"
# Arguments are an optional logfile name (or use global var $LOGFILE) 
#       and a quoted message
# Notes: log always returns $OK.
#
log() {
	# user can provide a logfile either in an argument
	# or as an environment variable: "LOGFILE"
	case $1 in
		-l*) LOGFILE=`expr "$1" : "-l\(.*\)"`
		     shift
		     ;;
	esac
	[ $LOGFILE ] && echo "\n$*" >> $LOGFILE
	return $OK
}

# Print an message to the terminal
#
# Usage: term_echo "message"
# Argument is a quoted message
# Notes: term_echo always returns the used file descriptor
#
term_echo() {
        _te_terminal=1
        while [ ! -t $_te_terminal ]
        do
                _te_terminal=`expr $_te_terminal + 1`
        done
	echo "$*" >&${_te_terminal}
	return $_te_terminal
}

# Abort the current program and start up a shell for the user
#
# Usage: exec_shell
#
exec_shell()
{
	echo "Shell escape.  System will halt when this shell exits."
	trap 1 2 3 15
	PATH=/bin:/etc:/usr/bin:/mnt/bin:/mnt/etc:/mnt/usr/bin
	PS1="<Installation> "
	HOME=/
        export HOME PS1 PATH
	sh -i
	sync 
        sync
        haltsys
}
