:
#	@(#) h-sierra.sh 1.1 90/02/05 
#
#	Copyright (C) The Santa Cruz Operation, 1989, 1990.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
# hs - UNIX 3.2 High Sierra / ISO9660 filesystem installation
#
PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

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

#
# this is a generic mkdev script for adding and removing filesystems
# in order to adapt it to other filesystem types it should only be
# necessary to modify the following three variables
#

# name of configuration file in /etc/conf/sfsys.d
name=hs

# filesystem name (as it appears in first field of configuration file)
fsname=HS

# descriptive name of filesystem
description="High Sierra / ISO9660 filesystem"

tmp=/tmp/fs$$

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

# ---------- 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
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# clear the screen if it is supported
clearscr() {
	# check if the terminal environment is set up
	[ "$TERM" ] && clear 2> /dev/null
}

# 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 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: $*" >&2
	return $FAIL
}

# perms list needed if link kit must be installed
permschk () {
	if [ -f /etc/perms/extmd ]; then
		PERM=/etc/perms/extmd
	else
		error "Cannot locate /etc/perms/extmd. 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 wish 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 "Invoking /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 filesystem change you specified.
Do you wish to create a new kernel now?"  ||  {
		echo "
To create a new kernel execute /etc/conf/cf.d/link_unix."
			return $FAIL 
		}
	return $OK
}

# re-link new kernel
klink() {
	cd /etc/conf/cf.d
	./link_unix || { 
		echo "\nError: Kernel link failed."
		cleanup $FAIL
	}
	return $OK
}

# ---------- CONFIGURE ROUTINES ------- These routines extract information 
#					from and modify kernel configuration
#					files and parameters.

# This routine checks whether the filesystem is currently configured
# It returns OK only if driver is fully installed.
confchk () {
	fieldval=`awk "/$fsname/{print \\$2}" /etc/conf/sfsys.d/$name` || {
			error "awk failed to read filesystem configuration file."
			cleanup $FAIL
		}
	case "$fieldval" in
		Y)	return $OK
			;;
		N)	return $FAIL
			;;
		*)
			error "unknown value in filesystem configuration file."
			cleanup $FAIL
	esac
}

# Add filesystem
confadd() {
	cd /etc/conf/sfsys.d
	sed "s/$fsname[ 	][ 	]*N/$fsname	Y/" $name > $tmp.sed || { 
		error "\nsed unable to edit /etc/conf/sfsys.d/$name."
		return $FAIL
	}
	unset_trap
	mv $tmp.sed $name
	set_trap
	change=YES
	echo "\n\nSystem configuration files have been successfully modified."
	return $OK
}


# Remove filesystem
confrm() {
	cd /etc/conf/sfsys.d
	sed "s/$fsname[ 	][ 	]*Y/$fsname	N/" $name > $tmp.sed || { 
		error "\nsed unable to edit /etc/conf/sfsys.d/$name."
		return $FAIL
	}
	unset_trap
	mv $tmp.sed $name
	set_trap
	change=YES
	echo "\n\nSystem configuration files have been successfully modified."
	return $OK
}

# ---------- MAIN ROUTINES ---------- 	These routines execute the flow of 
#					events to complete the task 
#					corresponding to the main menu.

add_filesys()  {

	# Check state of driver in system configuration files.
	confchk && {
 		getyn "
The $description is already present in the system configuration files.
Do you wish to relink the kernel ?" && {
			klink
			cleanup $OK
		}
		return $FAIL
	}

	# Modify system configuration files to include driver.
	confadd || cleanup $FAIL
	return $OK
}

rm_filesys()  {
	confchk || {
		echo "
The $description is not currently present in system configuration files."
		return $FAIL
	}
	# Update system configuration files.
	confrm || return $FAIL
	return $OK
}

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

cd /
set_trap
permschk
linkchk

clearscr 
while
	mesg="\n\n\n$description Configuration Program \n
	1. Add $description to system configuration.
	2. Remove $description from system configuration.
	
Select an option "
do
	prompt || break
  	case $cmd in
		1) add_filesys && break  ;;

		2) rm_filesys && break   ;;

		*) echo "\n$cmd is not valid. Try again."
		   continue  ;;
	esac
done
	
if [ "$change" = "YES"  -a ! "$_RELINK" ] 
then
	asklink && klink  
fi

cleanup $OK
