#ident	"@(#)proto:desktop/menus/useanswers	1.1.1.46"

function do_fd
{
	[ -n "$SH_VERBOSE" ] && set -x
	typeset FDISK=fdisk
	typeset disks dev i
	integer dsk disk 

	call unlink /tmp/fdisk.out
	if (( NDISKS > 1 ))
	then
		disks='1 2'
	else
		disks=1
	fi
	typeset CMDS=
	for dsk in $disks
	do
		ACT=
		disk=dsk-1
		{
			integer j=1 k=1
			for i in 1 2 3 4
			do
				if [ "${PARTTYPE[disk*4+i]}${PARTSTART[disk*4+i]}${PARTEND[disk*4+i]}" != "${PARTORIG[disk*4+i]}" ]
				then
					echo "d $j"
					case "${PARTTYPE[disk*4+i]}" in
					"$UNIX")
						type=1
						if [ "${PARTSTATUS[disk*4+i]}" = "$Boot" ]
						then
							ACT="a $k"
							print UNIX_PARTITION[$disk]=YES > /tmp/fdisk.info
						fi
						;;
					"$PRE5DOS")
						type=2
						;;
					"$DOS")
						type=3
						;;
					"$SYSCONFIG")
						type=4
						;;
					"$other")
						type=5
						;;
					"$unused")
						continue
						;;
					esac
					CMDS="$CMDS${nl}c ${type} ${PARTSTART[disk*4+i]} ${PARTLENGTH[disk*4+i]}"
					let k=k+1
				else
					if [ "${PARTSTATUS[disk*4+i]}" = "$Boot" ]
					then
						ACT="a $k"
					fi
					let k=k+1
					let j=j+1
				fi
			done
			CMDS="$CMDS${nl}${ACT}"
			[ -z "$CMDS" ] || echo "$CMDS"
			if [ "$BOOTSECTOR" = "$Yes" ]
			then
				echo "${nl}S"
			else
				echo "${nl}s"
			fi
		} | $FDISK -L ${CHAR_DISK_NODES[disk]} \
			>/dev/null 2>/tmp/fdisk.err || return 1
	done
	[ -s /tmp/fdisk.info ] && {
		. /tmp/fdisk.info
		call unlink /tmp/fdisk.info
	}
	return 0
}

function do_disksetup
# arg 1 is the disk (0 or 1)
{
	[ -n "$SH_VERBOSE" ] && set -x
	integer total_ticks=0 tick_counter=0
	typeset SCAN=true SCAN_WID SETUP_WID tick c
	typeset root_dir=/.extra.d
	[ "$SEC_MEDIUM_TYPE" = "cdrom" ] && root_dir=/cd-rom/.extra.d

	while getopts n c
	do
		case $c in
		n)
			SCAN=false
			;;
		*)
			faultvt "$0: $FAULTVT_INTERN_ERR3" 
			;;
		esac
	done
	shift $((( OPTIND - 1 )))
	[ -s /tmp/hd.$1.lay ] || return
	if $SCAN
	then
		eval display \"\$DISKSETUP_DISK$1_MSG\"
		SETUP_WID=$CURWIN
		# The -g option is for the gauge.
		$root_dir/usr/sbin/disksetup -g -m ${MEMSIZE} -x /tmp/hdscripts$1 \
		-d /tmp/hd.$1.lay -IsB ${CHAR_DISK_NODES[$1]} | {
			# disksetup might exit before giving us total_ticks, or it might
			# exit without giving us any ticks.  Either way, it's an error
			# condition.
			while :
			do
				read total_ticks
				if [ -z "$total_ticks" ] || (( total_ticks == 0 ))
				then
					wclose $SETUP_WID
					break
				fi
				read tick
				[ -z "$tick" ] && {
					wclose $SETUP_WID
					break
				}
				# We have total_ticks and at least one tick, so put up the
				# gauge and the display.
				wclose $SETUP_WID
				display "$SURFACE_SCAN_TEXT"
				SCAN_WID=$CURWIN
				eval GAUGE_TITLE=\$DISK$1_GAUGE_TITLE
				gauge_start $total_ticks
				(( tick_counter += 1 ))
				gauge_add
				while read tick
				do
					(( tick_counter += 1 ))
					# Sometimes disksetup sends us more
					# ticks than it said it would.
					(( tick_counter <= total_ticks )) && gauge_add
				done
				wclose $GAUGEWID
				wclose $SCAN_WID
				break
			done
		}
		if (( total_ticks == 0 )) ||
		   (( tick_counter < total_ticks )) ||
		   [ ! -s /tmp/hdscripts$1 ]
		then
			# Surface analysis has failed.  Certain combinations of host
			# adaptors and disk drives make surface analysis impossible, so
			# try again without surface scan.
			do_disksetup -n $1
		else
			return
		fi
	else
		$root_dir/usr/sbin/disksetup -n -m ${MEMSIZE} -x /tmp/hdscripts$1 \
		-d /tmp/hd.$1.lay -IsB ${CHAR_DISK_NODES[$1]} || {
			display -w "$DISKSETUP_FAULT" -bg $ERROR_BG -fg $ERROR_FG
			footer "$INSTERR"
			input_handler
			halt
		}
		return
	fi
}

function required_fsmods
{
	# See which filesystem modules we need before starting installation.
	# 'bfs' is always needed so that the stand filesystem can be mounted.
	# 's5' is statically built into the mini_kernel, so we don't list it here.

	integer i vxfs_needed=0 ufs_needed=0

	for i in 1 3 4 11 12 13
	do
		case ${SLTYPE[i]} in
		vxfs)
			(( vxfs_needed = 1 ))
			;;
		ufs)
			(( ufs_needed = 1 ))
			;;
		*)
			;;
		esac
	done
	print bfs
	(( ufs_needed )) && print dow sfs ufs 
	(( vxfs_needed )) && print vxfs
}

function do_fs
{
	[ -n "$SH_VERBOSE" ] && set -x
	typeset dsk SLI SL type block i line MKFS_WAIT_WID

	display "$INETINST_COPY_WAIT"
	sh_rm -f /tmp/hd.[0-9].lay /tmp/mnttab
	for SL in 1 2 3 4 6 10 11 12 13 14 15 16
	do
		type="${SLTYPE[SL]}"
		case "${SLTYPE[SL]}" in
		"$off" | memfs)
			continue
			;;
		$slice)
			block="-"
			type=-
			;;
		*)
			block=${SLBLKSIZE[SL]}
			;;
		esac
		let dsk=SLDISK[SL]-1
		if (( (SL == 1) && (dsk == 0) ))
		then
			export ROOTFS=$type
		fi
		echo "$SL${tab}${SLFS[SL]}${tab}$type${tab}$block${tab}${SLSIZE[SL]}M${tab}1R" >> /tmp/hd.$dsk.lay
		echo "${BLOCK_DISK_NODES[dsk]%s0}s$SL${tab}${SLFS[SL]}${tab}${SLTYPE[SL]}${tab}rw,suid${tab}1" >> /tmp/mnttab
	done

	umountall> /dev/null 2>&1
	> /etc/mnttab
	# Once us94-15732 is fixed, use sh_modadmin -U instead,
	# and remove /sbin/modadmin from ramdfs.proto.
	/sbin/modadmin -U bfs vxfs ufs sfs dow
	# One can manually create /tmp/dont.cleanup if one does not want
	# the ramdisk cleaned up.  This is a handy debugging hook, but
	# don't try it on small memory (<12MB) machines.
	[ -f /tmp/dont.cleanup ] || {
		clean_ramd
		clean_memfs
		add_ramd_swap
	}
	get_tools '*disksetup'
	# We have a display on the screen that we're about to close, but because
	# get_tools takes almost no time for network or CD, we'll wait for a second
	# to close it.  Otherwise the display will flash too quickly to read.
	call sleep 1
	wclose
	[ -s /tmp/swap.rem ] && $(</tmp/swap.rem)
	do_disksetup $HD0OPT 0
	do_disksetup $HD1OPT 1
	call unlink /.extra.d/usr/sbin/disksetup
	/usr/sbin/swap -a ${BLOCK_DISK_NODES[0]%s0}s2 || {
		print -u2 "/usr/sbin/swap -a ${BLOCK_DISK_NODES[0]%s0}s2 failed"
		faultvt "$GenericFatalErr"
		halt
	}
	# Uncomment the line below if desired.  I originally thought that, after
	# adding the hard disk swap, I should get rid of the ramdisk swap files,
	# but I no longer see any need to do that.
	# [ -f /tmp/dont.cleanup ] || del_ramd_swap

	display "$MKFS_WAIT"
	MKFS_WAIT_WID=$CURWIN
	get_fsmods $(required_fsmods)
	for i in 1 2 3 4 6
	do
		if [ "${SLINODES[$i]}" = "$No" ]
		then
			SLI="${SLI}${i}"
		fi
	done
	if [ "${SLINODES[10]}" = "$No" ]
	then
		SLI="${SLI}a"
	fi
	if [ "${SLINODES[11]}" = "$No" ]
	then
		SLI="${SLI}b"
	fi
	if [ "${SLINODES[12]}" = "$No" ]
	then
		SLI="${SLI}c"
	fi
	if [ "${SLINODES[13]}" = "$No" ]
	then
		SLI="${SLI}d"
	fi
	for i in 0 1
	do
		[ -s /tmp/hdscripts$i ] || continue
		while read line
		do
			case "$line" in
			*/mount*)
				line="mount${line#*mount}"
				;;
			*ufs/mkfs*\-o\ C,*${CHAR_DISK_NODES[$i]%s0}s[${SLI}]*)
				eval line=\"/etc/fs/ufs/mkfs -o L,${line#*C,}\"
				;;
			*sfs/mkfs*\-o\ C,*${CHAR_DISK_NODES[$i]%s0}s[${SLI}]*)
				eval line=\"/etc/fs/sfs/mkfs -o L,${line#*C,}\"
				;;
			*vxfs/mkfs*\-o\ C,*${CHAR_DISK_NODES[$i]%s0}s[${SLI}]*)
				eval line=\"/etc/fs/vxfs/mkfs -o L,${line#*C,}\"
				;;
			esac
			print "$line"
		done < /tmp/hdscripts$i
	done > /tmp/hdscripts.tmp

	while read line
	do
		[ -z "$line" ] && continue
		print "$line || do_fs_failed"
	done < /tmp/hdscripts.tmp > /tmp/hdscripts.sh
	call unlink /tmp/hdscripts.tmp
	. /tmp/hdscripts.sh
	sh_rm -rf /.extra.d/diskset
	wclose $MKFS_WAIT_WID
	PKGINSTALL_TYPE=NEWINSTALL
	return 0
}

function do_fs_failed
{
	faultvt "$MKFS_FAILED"
	halt
}

function do_node
{
	# Just leave 'NODE' in the environment -- the base package
	# will take care of it
	export NODE
}

function do_pkg
{
	[ -n "$SH_VERBOSE" ] && set -x
	typeset i

	# If dynatext was not selected and desktop was selected, force 
	# dynatext on so that icon stubs are installed.
	if (( dtext_icon ))
	then
		dynatext=1
		FULL=NO
	else
		FULL=YES
	fi

	INSTPKGS=$SET_NAME
	for i in $ALLPKGS
	do
		if (( $i ))
		then
			INSTPKGS="$INSTPKGS $i"
		fi
	done
}

function do_tune
{
	export PTNUM
}

function useanswers
{
	[ -n "$SH_VERBOSE" ] && set -x

	[ "$SEC_MEDIUM_TYPE" = tape ] && retension_tape
	[ "$NOND" = "no" ] && {
		do_fd || {
			display -w "$FDISKerror" -bg $ERROR_BG -fg $ERROR_FG
			footer "$FDISKERROR_FOOTER"
			input_handler
			footer
			$SILENT_INSTALL && halt
			return 1
		}
		[ -f /tmp/fsinit_done ] || fsinit
		do_fs
	}
	do_node
	do_pkg
	do_tune
	return 0
}
