#!/bin/sh
# $Id: questions.sh,v 1.1 1998/12/30 19:53:06 danw Exp $
# Interrogate the user about disk configuration and other install variables.

# Relies on files created by hdinfo.

# /tmp/vars outputs
#	disk		disk device containing NetBSD partition
#	start		starting sector of NetBSD partition
#	size		ending sector of NetBSD partition
#	disksize	total number of sectors on $disk
#	create		whether we need to create the partition
#	rootsize	sectors for root subpartition
#	swapsize	sectors for swap subpartition
#	cachesize	sectors for cache subpartition
#	u1size		sectors for u1 subpartition
#	rootmail	where to forward root mail
#	mail		whether to send configuration mail
#	mail_cc		where to cc configuration mail to
#	action		what to do after installing

. /tmp/vars

getresp() {
	read resp
	case "$resp" in
		"")
			resp="$1"
			;;
	esac
}

yesno() {
	while :; do
		read resp
		case "$resp" in
			"")
				resp="$1"
				break
				;;
			y*)
				resp="yes"
				break
				;;
			n*)
				resp="no"
				break
				;;
		esac
		echo -n "Please enter 'y' or 'n': [$1] "
	done
}

displayvalues() {
	if [ -n "$rootmail" ]; then
		rootmail_msg="$rootmail"
	else
		rootmail_msg="<not forwarded>"
	fi

	if [ "$mail" = yes ]; then
		mail_msg="yes (also to: $mail_cc)"
	else
		mail_msg=no
	fi

	cat << EOM

Disk to install on:			$disk
Create a new partition:			$create
Starting sector of partition:		$start
Number of sectors in partition:		$size
Root subpartition size:			`expr $rootsize / 2048`MB
Swap subpartition size:			`expr $swapsize / 2048`MB
Cache subpartition size:		`expr $cachesize / 2048`MB
/u1 subpartition size:			`expr $u1size / 2048`MB
Root mail forwards to:			$rootmail_msg
Mail configuration to SIPB:		$mail_msg
Action after installation:		$action

EOM
}

if [ -n "$action" ]; then
	echo "You have previously entered the following values:"
	displayvalues
	echo -n "Do you want to use these values? [yes] "
	yesno yes
	if [ "$resp" = yes ]; then
		exit 0
	fi
else
	echo ""
	echo "Before we do the actual installation, we have some questions to"
	echo "ask about the way you want your system installed and configured."
	echo "Most of the questions have reasonable defaults."
fi
echo ""

# See if the user wants to install on an existing NetBSD partition.
disk=none
if [ -s /tmp/netbsdpart ]; then
	proto_disk=`awk ' { if ($3 > size) { disk = $1; size = $3; } }
			  END { print disk; }' /tmp/netbsdpart`
	echo "You have the following existing NetBSD partitions:"
	echo ""
	awk '{ print $1 ": " int($3 / 2048) "MB"; }' /tmp/netbsdpart
	echo ""
	if [ -s /tmp/freespace ]; then
		echo "Type 'none' if you want to create a new partition."
	fi
	while :; do
		echo -n "Use which disk's partition? [$proto_disk] "
		getresp "$proto_disk"; disk="$resp"
		set -- `grep "^$disk" /tmp/netbsdpart`
		if [ "$1" = "$disk" -o \
		     "$disk" = none -a -s /tmp/freespace ]; then
			break
		fi
	done
	if [ "$disk" != none ]; then
		echo ""
		echo "The MBR says that this partition starts at sector $2"
		echo "and is $3 sectors long.  Please confirm:"
		echo -n "What sector does the NetBSD partition start at? [$2] "
		getresp "$2"; start="$resp"
		echo -n "How many sectors long is it? [$3] "
		getresp "$3"; size="$resp"
		create=no
	fi
fi

# If no existing NetBSD partition selected, find out where to create a new one.
if [ "$disk" = none ]; then
	proto_disk=`awk '{ if ($3 > size) { disk=$1; size=$3; } }
			   END { print disk; }' /tmp/freespace`
	echo "We need to create a NetBSD partition to install onto.  You have"
	echo "the following areas of free space on your disks:"
	echo ""
	awk '{ print $1 ": " int($3 / 2048) "MB at sector " $2 }' \
		/tmp/freespace
	echo ""
	while :; do
		echo -n "Use free space on which disk? [$proto_disk] "
		getresp "$proto_disk"; disk="$resp"
		set -- `grep "^disk" /tmp/netbsdpart`
		if [ "$1" = "$disk" ]; then
			echo -n "You already have a NetBSD partition on that"
			echo " disk; you can't create another one."
			continue
		fi
		set -- `grep "^$disk" /tmp/freespace | sort -nr +3 | head -1`
		if [ "$1" = "$disk" ]; then
			break
		fi
	done
	echo -n "What starting sector? [$2] "
	getresp "$2"; start="$resp"
	echo -n "How many sectors long? [$3] "
	getresp "$3"; size="$resp"
	create=yes
fi

# Find the size of the picked disk.
disksize=`disklabel "$disk" | awk '/^total sectors: / {print $3}'`
if [ -z "$disksize" ]; then
	echo "No size found for ${disk}!  This shouldn't happen."
	exit 1
fi

# Add one to memory size to get the "advertised" size, since hw.physmem
# has the 384k from 640k-1024k subtracted out.
memsize=$(expr $(sysctl -n hw.physmem) / 1048576 + 1)

# Get subpartition sizes for root, swap, afs cache, and /u1.
# Reserve 55MB or 144MB for root, swap, and cache depending on total size.
mbytes=`expr "$size" / 2048`
if [ "$mbytes" -lt 250 ]; then
	proto_root=30; proto_swap=14; proto_cache=11
else
	proto_root=48; proto_swap=`expr "$memsize" "*" 2`; proto_cache=64
fi
echo ""
echo "You have $mbytes megabytes in your NetBSD partition.  You will now be"
echo "asked to give the sizes of a root subpartition, a swap subpartition, an"
echo "AFS cache subpartition, and a /u1 subpartition (which will"
echo "contain most of the operating system and should be at least 150MB).  You"
echo "may leave space left over at the end for partitions you wish to create"
echo -n "later.  The default sizes for these partitions are ${proto_root}MB"
echo " for the root"
echo -n "subpartition, ${proto_swap}MB for the swap subpartition,"
echo " ${proto_cache}MB for the AFS cache"
echo "subpartition, and whatever is left for the /u1 subpartition."
echo ""
resp=0
while [ "$resp" -le 0 -o "$resp" -ge "$mbytes" ]; do
	echo -n "Number of megabytes for root subpartition? [$proto_root] "
	getresp "$proto_root"; rootsize=`expr "$resp" "*" 2048`
done
mbytes=`expr "$mbytes" - "$resp"`
resp=0
while [ "$resp" -le 0 -o "$resp" -ge "$mbytes" ]; do
	echo -n "Number of megabytes for swap subpartition? [$proto_swap] "
	getresp "$proto_swap"; swapsize=`expr "$resp" "*" 2048`
done
mbytes=`expr "$mbytes" - "$resp"`
resp=0
while [ "$resp" -le 0 -o "$resp" -ge "$mbytes" ]; do
	echo -n "Number of megabytes for cache subpartition? [$proto_cache] "
	getresp "$proto_cache"; cachesize=`expr "$resp" "*" 2048`
done
mbytes=`expr "$mbytes" - "$resp"`
resp=0
while [ "$resp" -le 0 -o "$resp" -gt "$mbytes" ]; do
	echo -n "Number of megabytes for /u1 subpartition? [$mbytes] "
	getresp "$mbytes"; u1size=`expr "$resp" "*" 2048`
done
if [ "$u1size" = "$mbytes" ]; then
	# If we used all the megabytes, tack stray sectors onto /u1.
	u1size=`expr "$size" - "$rootsize" - "$swapsize" - "$cachesize"`
fi

echo ""

echo ""
echo "Please enter an email address where mail to root should be forwarded"
echo "Just press enter if you want root's mail to be spooled locally in"
echo "/var/mail/root."
echo ""
echo -n "To what email address should mail to root be forwarded? "
read rootmail

echo ""
echo "The SIPB would like to track NetBSD installations that used our script;"
echo "as such, we would like to receive automated mail with configuration"
echo "information about your system to help us better account for our"
echo "resources and to see how popular this script is."
echo ""
echo -n "May this script mail your configuration information to the SIPB? "
echo -n "[yes] "
yesno yes; mail="$resp"
if [ "$mail" = "yes" ]; then
	echo ""
	echo "If you would like the above configuration information sent to"
	echo "you as well, enter your username below.  Otherwise, just press"
	echo "return."
	echo ""
	echo -n "Where else would you like the mail sent? "
	read mail_cc
fi

echo ""
echo "Finally, what would you like to have happen when the install finishes?"
echo 'Enter "r" to have your machine reboot automatically, "h" to have your'
echo 'machine halt, or "s" to have your machine exit to a shell prompt.'
echo ""
echo -n "What should your machine do after installing? [halt] "
action=""
while [ "$action" = "" ]; do
	getresp h
	case "$resp" in
		r*)	action=reboot	;;
		h*)	action=halt	;;
		s*)	action=shell	;;
		*)	echo -n 'Please enter "r", "h", or "s": [halt] ' ;;
	esac
done

echo ""
echo "Okay, please confirm your answers to these questions:"
displayvalues
echo -n "Do you accept these values? [yes] "

getresp yes
case "$resp" in
y*)
	;;
*)
	echo "Okay, trying again."
	exec /srvd/install/questions
	;;
esac


cat >> /tmp/vars << EOM
disk="$disk"
start="$start"
size="$size"
disksize="$disksize"
create="$create"
rootsize="$rootsize"
swapsize="$swapsize"
cachesize="$cachesize"
u1size="$u1size"
rootmail="$rootmail"
username="$username"
mail="$mail"
mail_cc="$mail_cc"
action="$action"
EOM
