cleanup()
{
	trap "" 1 2 3 15
	if [ $LOADED -gt 0 ]; then
		echo "\n\nThe following packages have been successfully loaded:"
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_LOADED` ]; then
				eval echo "\$${GROUP}_NAME"
			fi
		done
	fi
	cd $ROOTDIR/$INSTALL
	rm -f CONTENTS REL *.Rlist *.info *.preins *.prepkg *.setup *.size *.ti
	tape -f/dev/rmt/${DRIVE}mn status > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\nRewinding, please wait..."
		wait
	elif [ ! "`tape -f/dev/rmt/${DRIVE}mn status 2>&1 | grep bot`" ]; then
		echo "\nRewinding, please wait..."
		tape -f/dev/rmt/${DRIVE}m rew > /dev/null 2>&1
	fi
	sync
	if [ $AUTO ]; then 
		/opus/bin/opdos del /opus/inst_all > /dev/null 2>&1
		/opus/bin/opdos del /opus/inst_os > /dev/null 2>&1
	fi
	exit 0
}
err_cleanup()
{
	trap "" 1 2 3 15
	for GROUP in $MASTER_SAVE; do
		if [ `eval echo \\$\${GROUP}_LOADING` ]; then
			eval echo "\\\n\\\nThe \$${GROUP}_NAME may not have been installed properly."
		fi
	done
	if [ $LOADED -eq 0 ]; then
		if [ $CO_LOADING ]; then
			echo "This package is needed in order to boot the root file system."
		else
			echo "\nThe Core package is needed in order to boot the root file system."
		fi
	else
		echo "\nThe following packages have been sucessfully loaded:"
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_LOADED` ]; then
				eval echo "\$${GROUP}_NAME"
			fi
		done
	fi
	cd $ROOTDIR/$INSTALL
	rm -f CONTENTS REL *.Rlist *.info *.preins *.prepkg *.setup *.size *.ti
	tape -f/dev/rmt/${DRIVE}mn status > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\nRewinding, please wait..."
		wait
	elif [ ! "`tape -f/dev/rmt/${DRIVE}mn status 2>&1 | grep bot`" ]; then
		echo "\nRewinding, please wait..."
		tape -f/dev/rmt/${DRIVE}m rew > /dev/null 2>&1
	fi
	if [ $AUTO ]; then 
		/opus/bin/opdos del /opus/inst_all > /dev/null 2>&1
		/opus/bin/opdos del /opus/inst_os > /dev/null 2>&1
	fi
	sync
	exit 1
}
rtc()
{
	echo "\nPress RETURN to continue: \c"
	line
}
invld()
{
	echo "\n${BELL}Invalid response."
	sleep 1
}
args()
{
	echo $#
}
sortlist()
{
	if [ $# -gt 1 ]; then
		if [ -x /bin/sort ]; then
			for item in  $*; do
				echo $item
			done | sort -u
		elif [ -x /usr/bin/uniq ]; then
			for item in  $*; do
				echo $item
			done | uniq
		else
			for item in  $*; do
				echo $item
			done
		fi
	else
		echo $*
	fi
}
increment()
{
	# This is a lot quicker than expr
	XVAR=`eval echo \\$$1`
	XVARNAME=$1
	case $XVAR in
		0)
			eval $XVARNAME=1
			;;
		1)
			eval $XVARNAME=2
			;;
		2)
			eval $XVARNAME=3
			;;
		3)
			eval $XVARNAME=4
			;;
		4)
			eval $XVARNAME=5
			;;
		5)
			eval $XVARNAME=6
			;;
		6)
			eval $XVARNAME=7
			;;
		7)
			eval $XVARNAME=8
			;;
		8)
			eval $XVARNAME=9
			;;
		9)
			eval $XVARNAME=10
			;;
		10)
			eval $XVARNAME=11
			;;
		11)
			eval $XVARNAME=12
			;;
		12)
			eval $XVARNAME=13
			;;
		13)
			eval $XVARNAME=14
			;;
		14)
			eval $XVARNAME=15
			;;
		15)
			eval $XVARNAME=16
			;;
		16)
			eval $XVARNAME=17
			;;
		17)
			eval $XVARNAME=18
			;;
		18)
			eval $XVARNAME=19
			;;
		19)
			eval $XVARNAME=20
			;;
		*)
			eval $XVARNAME=`eval expr \\$$XVARNAME + 1`
			;;
	esac
}
help()
{
	trap "exit" 1 2 3 15
	while :; do
		echo $CLEAR
		echo "\n\n${REVERSE} Tape Installation Help Menu ${UNBOLD}"
		echo "------------------------------------------------------------------------------"
		echo "\nChoose:  0) Exit help"
		echo "         1) Read information about a selected package"
		echo "         2) Look at the list of files for a selected package"
		echo "\nEnter [0-2]: \c"
		read MODE
		case $MODE in
			0) 
				exit;;
			1)
				SHELL=xyzyx
				while :; do
					echo $CLEAR
					echo "\n\n$REVERSE Read information about a selected package: $UNBOLD"
					echo "------------------------------------------------------------------------------"
					echo "\nThese are the available packages:\n"
					display first
					echo "------------------------------------------------------------------------------"
					echo "\nPackages:"
					for i in $MASTER_ALL; do
						echo $i
					done | pr -4 -t
					echo "\nEnter a package name: \c"
					GRP=`line | tr '[a-z]' '[A-Z]'`
					if [ `args $GRP` -gt 1 ]; then
						echo "\n${BELL}Invalid response: only one name expected"
						sleep 1
						continue
					fi
					case $GRP in
						Q)
							continue;;
						*\;*)
							invld;;
						[A-Z]*)
							echo $CLEAR
							if [ "`eval echo \\$\${GRP}_NAME`" = "" ]; then
								invld
								continue
							fi
							echo
							pr -t $ROOTDIR/$INSTALL/$GRP.info | pg -p"=== `eval echo \\$\${GRP}_NAME`.  Press RETURN to continue: ===(page %d): "
							continue 2;;
						*)
							invld;;
					esac
				done;;
			2)
				SHELL=xyzyx
				while :; do
					echo $CLEAR
					echo "\n\n$REVERSE Look at the list of files for a selected package: $UNBOLD"
					echo "------------------------------------------------------------------------------"
					echo "\nThese are the available packages:\n"
					display first
					echo "------------------------------------------------------------------------------"
					echo "\nPackages:"
					for i in $MASTER_ALL; do
						echo $i
					done | pr -4 -t
					echo "\nEnter a package name: \c"
					GRP=`line | tr '[a-z]' '[A-Z]'`
					if [ `args $GRP` -gt 1 ]; then
						echo "\n${BELL}Invalid response: only one name expected"
						sleep 1
						continue
					fi
					case $GRP in
						Q)
							continue;;
						*\;*)
							invld;;
						[A-Z]*)
							if [ "`eval echo \\$\${GRP}_NAME`" = "" ]; then
								invld
								continue
							else
								echo $CLEAR
								pr -2 -a -t $ROOTDIR/$INSTALL/$GRP.Rlist | grep -v "^install" | grep -v "^usr\/options" | pg -p"=== `eval echo \\$\${GRP}_NAME`.  Press RETURN to continue: ===(page %d): "
								continue 2
							fi;;
						*)
							invld;;
					esac
				done;;
			*)
				invld;;
		esac
	done
}
display()
{
	DCOUNT=`args $MASTER_ALL`
	case $1 in
		select)
			CCNT=11
			;;
		first)
			CCNT=12
			;;
		init)
			CCNT=14
			;;
		more)
			CCNT=16
			;;
		load)
			CCNT=17
			;;
		*)
			CCNT=15
			;;
	esac
	if [ "$DCOUNT" -gt "$CCNT" ]; then
		CMD="pg -p\"=== Press RETURN to continue ===(page %d): \""
	else
		CMD=cat
	fi
	if [ $1 = select -a "$FIRST" ]; then
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_FLAG` ]; then
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BBLOCK}\ ${GROUP}"
			else
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BLOCK}\ ${GROUP}"
			fi
		done | eval $CMD
	elif [ $1 = select ]; then
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_LOADED` ]; then
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\ \ ${GROUP} \(installed\)"
			elif [ `eval echo \\$\${GROUP}_FLAG` ]; then
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BBLOCK}\ ${GROUP}"
			else
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BLOCK}\ ${GROUP}"
			fi
		done | eval $CMD
	elif [ $1 = first ]; then
		for GROUP in $MASTER_SAVE; do
				eval echo "\	\	\	\$${GROUP}_NAME\\\r${GROUP}"
		done | eval $CMD
	elif [ $1 = load ]; then
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_FLAG` ]; then
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BBLOCK}\ ${GROUP}"
			else
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\ \ ${GROUP}"
			fi
		done | eval $CMD
	elif [ $1 = more ]; then
		for GROUP in $MASTER_SAVE; do
			if [ `eval echo \\$\${GROUP}_LOADED` ]; then
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\${BBLOCK}\ ${GROUP}"
			else
				eval echo "\	\	\	\$${GROUP}_NAME\\\r\ \ ${GROUP}"
			fi
		done | eval $CMD
	fi
}
install()
{
	while [ ! "$AUTO" ]; do
		echo $CLEAR
		echo "\n\n$REVERSE Ready to install $UNBOLD"
		echo "------------------------------------------------------------------------------"
		echo "The following packages will be installed:\n"
		display load
		echo "------------------------------------------------------------------------------"
		echo "Choose:  1) re-select"
		echo "\n[Default: RETURN to begin installation]: \c"
		read ANS
		case $ANS in
			"")
				break;;
			1)
				echo $CLEAR
				return 2;;
			*)
				invld
				continue;;
		esac
	done
	COUNT=0
	for GRP in $GROUPS; do
		increment COUNT
	done
	tape -f/dev/rmt/${DRIVE}mn status > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\nRewinding, please wait..."
		wait
	fi
	if [ "`tape -f/dev/rmt/${DRIVE}mn status 2>&1 | grep bot`" ]; then
		echo "\n\nSkipping the tape header..."
		tape -f /dev/rmt/${DRIVE}mn fsf > /dev/null 2>&1
	fi
	for GROUP in $MASTER_SAVE; do
		if [ "`eval echo \\$\${GROUP}_FLAG`" -a ! "`eval echo \\$\${GROUP}_LOADED`" ]; then
			eval echo "\\\n\\\nInstalling the \$${GROUP}_NAME..."
			
			NEEDS=`line < $ROOTDIR/$INSTALL/$GROUP.size`
			SPACE=`expr "\`df $ROOTDIR 2>/dev/null\`" : '.*: *\([0-9]*\)'`
			if [ "$SPACE" -lt "$NEEDS" ]; then
				eval echo "\\\n\\${BELL}Error:  the \$${GROUP}_NAME cannot be installed."
				echo "Not enough space on the hard disk."
				echo "$SPACE blocks are available in the root file system."
				echo "$NEEDS blocks are needed."
				return 1
			fi

			eval ${GROUP}_LOADING=1
			# Do any pre-installation work.
			if [ -f $ROOTDIR/$INSTALL/$GROUP.preins ]; then
				$ROOTDIR/$INSTALL/$GROUP.preins
				if [ $? -ne 0 ]; then
					return 1
				fi
			fi
			cpio -icvdumC512000 < /dev/rmt/${DRIVE}mn 2> /dev/null
			if [ $? -ne 0 ]; then
				eval echo "\\\nError installing the \$${GROUP}_NAME..."
				sync
				return 1
			fi
			if [ -s $ROOTDIR/$INSTALL/$GROUP.setup ]; then
				if [ $GROUP = TI ]; then
					TERMINFO=$instROOT/usr/lib/terminfo; export TERMINFO
				fi
				sh $ROOTDIR/$INSTALL/$GROUP.setup
			fi
			eval ${GROUP}_LOADING=""
			eval ${GROUP}_LOADED=1
			increment LOADED
			cp $ROOTDIR/$INSTALL/$GROUP.Rlist $ROOTDIR/$INSTALL/$GROUP.info $ROOTDIR/$USR_OPT
			COUNT=`expr $COUNT - 1`
			if [ $COUNT -eq 0 ]; then
				break
			fi
		else
			eval echo "\\\n\\\nSkipping the \$${GROUP}_NAME..."
			tape fsf 1 /dev/rmt/${DRIVE}mn > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				eval echo "\\\nError fast forwarding over the \$${GROUP}_NAME..."
				sync
				return 1
			fi
		fi
		sync
	done
	tape -f /dev/rmt/${DRIVE}m rew > /dev/null 2>&1 &
	return 0
}
select()
{
	while :; do
		for GRP in $MASTER_SAVE; do
			eval ${GRP}_FLAG=""
		done
		while [ ! "$MORE" -o "$RETURN" ]; do
			trap "err_cleanup" 1 2 3 15
			RETURN=""
			GROUPS=""
			COUNT=1
			echo $CLEAR
			echo "\n\n${REVERSE} Opus Systems Tape Installation ${UNBOLD}"
			echo "----------------------------------------------------------------------------"
			if [ $FIRST ]; then
				echo "These are the available packages:\n"
				display first
			else
				echo "These are the available packages:"
				echo "${BLOCK} = available"
				display select
			fi
			echo "------------------------------------------------------------------------------"
			echo "\nChoose:  0) quit"
			echo "         1) get information on packages"
			echo "         2) install specific packages"
			echo "\n[Default: RETURN to install all packages]: \c"
			read ANS
			case $ANS in
				"")
					GROUPS=$MASTER_LIST
					for GRP in $MASTER_LIST; do
						eval ${GRP}_FLAG=1
					done
					return;;
				0)
					if [ $LOADED -gt 0 ]; then
						cleanup
					else
						while :; do
							echo "\n${BELL}WARNING: no packages have been loaded."
							echo "\nThe Core package is needed in order to boot the root file system."
							echo "Are you sure you want to quit? [y/n]: \c"
							read QUIT
							case $QUIT in
								y)
									err_cleanup;;
								n)
									continue 2;;
								*)
									invld;;
							esac
						done
					fi;;
				1)
					trap "continue" 1 2 3 15
					(help)
					trap "err_cleanup" 1 2 3 15
					continue;;
				2)
					break;;
				*)
					invld
					continue;;
			esac
		done
		CHOICES=""
		for GRP in $MASTER_LIST; do
			if [ ! "`eval echo \\$\${GRP}_FLAG`" ]; then
				CHOICES="$CHOICES $GRP"
			fi
		done
		SELECTED=""
		while :; do
			if [ ! "$CO_FLAG" -a ! "$CO_LOADED" -a -f $ROOTDIR/$INSTALL/CO.info ]; then
				SELECTED=1
				GROUPS=CO
				CO_FLAG=1
			fi
			echo $CLEAR
			echo "\n\n${REVERSE} Opus Systems Tape Installation ${UNBOLD}"
			echo "------------------------------------------------------------------------------"
			echo "These are the available packages:"
			if [ $SELECTED ]; then
				echo "$BLOCK = available, $BBLOCK = selected"
			else
				echo "$BLOCK = available"
			fi
			display select
			if [ ! "$CO_LOADED" -a -f $ROOTDIR/$INSTALL/CO.info ]; then
				echo "\nThe CORE package will be installed by default"
				echo "------------------------------------------------------------------------------"
			else
				echo "------------------------------------------------------------------------------"
			fi
			echo "\nChoose:  0) return to the top level menu"
			if [ $SELECTED ]; then
				echo "         1) clear selected entries"
			fi
			echo "         or enter the names (i.e, BU AU)"
			echo "         of the packages you wish to install."
			if [ "$SELECTED" -o "$CO_FLAG" ]; then
				echo "\n[Default: RETURN to install the selected packages]: \c"
			fi

			echo "\nAvailable packages:"
			for i in $CHOICES; do
				echo $i
			done | pr -4 -t
			if [ $SELECTED ]; then
				echo "\nEnter 0, 1, or package names: \c"
			else
				echo "\nEnter 0, or package names: \c"
			fi
			LINE=`line | tr '[a-z]' '[A-Z]'`
			if [ ! "$LINE" ]; then
				echo "\n\007At least one package must be chosen."
				sleep 1
				continue
			else
				COUNT=`args $LINE`
				while [ "$COUNT" -gt 1 ]; do
					for i in $LINE; do
						if [ ! "`eval echo \\$\${GROUP}_NAME`" ]; then
							echo "\n\007$i is not an available package."
							sleep 1
							continue 2
						fi
					done
					LINE=`sortlist $LINE`
				done
			fi
			for GROUP in $LINE; do
				case $GROUP in
					"")
						SET=0
						for GRP in $MASTER_LIST; do
							if [ `eval echo \\$\${GRP}_FLAG` ]; then
								return
							fi
						done
						echo "\n${BELL}At least one package must be selected"
						sleep 2
						continue;;
					0)
						RETURN=1
						continue 2;;
					1)
						for GRP in $MASTER_LIST; do
							eval ${GRP}_FLAG=""
						done
						RETURN=1
						continue;;
					*\;*)
						# Filter any lines with two semicolons;
						# the shell this as two commands
						# (after eval deals with it).
						invld
						continue;;
					[A-Z]*)
						if [ ! "`eval echo \\$\${GROUP}_NAME`" ]; then
							invld
							continue
						fi
						if [ `eval echo \\$\${GROUP}_LOADED` ]; then
							echo "\n${BELL}$GROUP has already been installed"
							sleep 1
							continue
						elif [ `eval echo \\$\${GROUP}_FLAG` ]; then
							echo "\n${BELL}$GROUP has already been selected"
							sleep 1
							continue
						fi
						eval ${GROUP}_FLAG=1
						PRE=""
						while [ -f $ROOTDIR/$INSTALL/$GROUP.prepkg ]; do
							PRE_GRPS=`line < $ROOTDIR/$INSTALL/$GROUP.prepkg`
							NEWLIST=""
							for GRP in $PRE_GRPS; do
								if [ ! "`eval echo \\$\${GRP}_FLAG`" -a ! "`eval echo \\$\${GRP}_LOADED`" ]; then
									NEWLIST="$NEWLIST $GRP"
									PRE=1
								fi
							done
							if [ ! "$PRE" ]; then
								break
							fi
							for GRP in $MASTER_LIST; do
								eval ${GRP}_PRE_FLAG=""
							done
							if [ "$NEWLIST" ]; then
								echo $CLEAR
								echo "\n\n${BELL}The following package(s) must be installed"
								eval echo "before the \$${GROUP}_NAME:"
								PRE_COUNT=1
								ADD=$GROUP
								for GRP in $NEWLIST; do
									if [ ! "`eval echo \\$\${GRP}_FLAG`" -a ! "`eval echo \\$\${GRP}_LOADED`" ]; then
										ADD="$ADD $GRP"
										eval echo "=== \$${GRP}_NAME ==="
										eval ${GRP}_PRE_FLAG=1
										increment PRE_COUNT
									fi
								done
								while :; do
									echo "\nWould you like to add these packages?"
									echo "----------------------------------"
									for GRP in $ADD; do
										eval echo "\$${GRP}_NAME"
									done
									echo "----------------------------------"
									echo "Enter [y/n]: \c"
									read ANS
									case $ANS in 
										y)
											for GRP in $MASTER_LIST; do
												if [ `eval echo \\$\${GRP}_PRE_FLAG` ]; then
													eval ${GRP}_FLAG=1
												fi
											done
											break 2;;
										n)
											eval ${GROUP}_FLAG=""
											COUNT=`expr $COUNT - 1`
											break 2;;
										*)
											invld
											continue;;
									esac
								done
							fi
						done
						SELECTED=1
						increment COUNT
						GROUPS=""
						CHOICES=""
						for i in $MASTER_LIST; do
							if [ `eval echo \\$\${i}_FLAG` ]; then
								GROUPS="$GROUPS $i"
							else
								CHOICES="$CHOICES $i"
							fi
						done
						if [ $COUNT -eq $MLIST_COUNT ]; then
							return
						fi;;
					*)
						invld
						continue;;
				esac
			done
		done
	done
}

trap "sync; exit 1" 1 2 3 15

#
# Initialization and main loop
#

# Set terminal variables
BELL=""; export BELL
BLINK="[5m"; export BLINK
CLEAR="[2J"
CLEAR="${CLEAR}\c"; export CLEAR
REVERSE="[7m"; export REVERSE
UNDER="[4m"; export UNDER
UNBOLD="[0m"; export UNBOLD
XUNBOLD=$UNBOLD
#BLOCK="\\026"
#BBLOCK="${BLINK}\\026${UNBOLD}"
BLOCK="o"
BBLOCK="+"

if [ "$1" = debug -o "$1" = DEBUG ]; then
	CLEAR=""; export CLEAR
	REVERSE=""; export REVERSE
	UNBOLD=""; export UNBOLD
	set -x
fi

ROOTDIR=/install; export ROOTDIR
instROOT=/install; export instROOT
INSTALL=/install; export INSTALL
USR_OPT=/usr/options; export USR_OPT
PATH=/bin:/etc:/usr/bin:/opus/bin:$instROOT/bin:$instROOT/etc:$instROOT/usr/bin:.; export PATH
DRIVE=0
DRIVE_STAT0=`/opus/bin/opdos asciiread \\\opus\\\opus.cfg | grep "^<mt/0"`
DRIVE_STAT1=`/opus/bin/opdos asciiread \\\opus\\\opus.cfg | grep "^<mt/1"`
if [ ! "$DRIVE_STAT0" -a ! "$DRIVE_STAT1" ]; then
	echo $CLEAR
		echo "\n\n${BELL}There is no entry in opus.cfg for tape devices mt/0 or mt/1"
		echo "Cannot install from tape."
		rtc
		exit 1
fi
while [ $DRIVE_STAT1 ]; do
	echo "\n\nDo you want to load the tape from drive 0 or drive 1?"
	echo "\nEnter [0/1]: \c"
	read DRIVE
	case $DRIVE in
		0|1)
			break;;
		*)
			invld;;
	esac
done
if [ ! "$AUTO" ]; then
	echo "\n\nInsert the Opus5 tape in drive $DRIVE and press RETURN: \c"
	line
fi
while [ ! "`tape -f/dev/rmt/${DRIVE}m rew 2>&1 | grep bot`" ]; do
	echo "${BELL}Make sure that the tape is properly installed in drive $DRIVE."
	rtc
done

if [ -d $ROOTDIR/$INSTALL ]; then
	cd $ROOTDIR/$INSTALL
	rm -f CONTENTS REL *.Rlist *.info *.preins *.prepkg *.setup *.size *.ti
fi

trap "err_cleanup" 1 2 3 15

cd $ROOTDIR
echo "\nInstalling setup files..."
tape -f/dev/rmt/${DRIVE}m rew > /dev/null 2>&1
cpio -icdumC256000 < /dev/rmt/${DRIVE}mn "install/*"
sync
if [ ! -f $ROOTDIR/$INSTALL/CONTENTS -o ! -f $ROOTDIR/$INSTALL/REL ]; then
	tape -f/dev/rmt/${DRIVE}m rew > /dev/null 2>&1
	echo "\n\n${BELL}$0: tape header not found."
	echo "Cannot perform installation."
	rtc
	exit 1
fi

cd $ROOTDIR/$INSTALL

MASTER_LIST=`line < $ROOTDIR/$INSTALL/CONTENTS`
MASTER_SAVE="$MASTER_LIST"; export MASTER_SAVE

COUNT=0
for GROUP in $MASTER_LIST; do
	eval ${GROUP}_NAME=\"`line < $ROOTDIR/$INSTALL/$GROUP.info`\"
	eval ${GROUP}_FLAG=""
	eval ${GROUP}_LOADED=""
	eval ${GROUP}_LOADING=""
	increment COUNT
done

TOTAL=$COUNT
MLIST_COUNT=$COUNT
LOADED=0
MORE=""
FIRST=1
cd $ROOTDIR
while [ $LOADED -lt $TOTAL ]; do
	if [ "$AUTO" -a "$FIRST" ]; then
		for GROUP in $MASTER_LIST; do
			if [ $AUTO = ALL ]; then
				eval ${GROUP}_FLAG=1
			else
				case $GROUP in
					CO|BU|AU|AS|LP|TI|FACE|SD|ASSIST|F77|GRAPH)
						eval ${GROUP}_FLAG=1;;
				esac
			fi
		done
	else
		select
	fi
	install $GROUPS
	RTN=$?
	FIRST=""
	if [ $RTN -eq 2 ]; then
		GROUPS=""
		continue
	elif [ $RTN -ne 0 ]; then
		err_cleanup
	fi
	for GRP in $GROUPS; do
		eval ${GROUP}_FLAG=""
	done
	if [ "$AUTO" -o $LOADED -eq $TOTAL ]; then
		cleanup
	fi
	while :; do
		COUNT=0
		echo $CLEAR
		echo "\n\n------------------------------------------------------------------------------"
		echo "The following packages have been installed: \n"
		display more
		echo "------------------------------------------------------------------------------"
		echo "\nChoose:  0) Terminate installation"
		echo "         1) install more packages"
		echo "\nEnter [0-1]: \c"
		read ANS
		case $ANS in
			0)
				cleanup;;
			1)
				MASTER_TEMP="$MASTER_LIST"
				MASTER_LIST=""
				MLIST_COUNT=0
				for GRP in $MASTER_TEMP; do 
					if [ ! "`eval echo \\$\${GRP}_LOADED`" ]; then
						increment MLIST_COUNT
						MASTER_LIST="$MASTER_LIST $GRP"
					fi
				done
				MORE=1
				break;;
			*)
				invld;;
		esac
	done
done
