# Copyright (c) 1995 by the Student Information Processing Board
# 	  of the Massachusetts Institute of Technology
#  
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice
# appear in all copies and that both that copyright notice and
# this permission notice appear in supporting documentation,
# and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
# used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
# M.I.T. and the M.I.T. S.I.P.B. make no representations about
# the suitability of this software for any purpose.  It is
# provided "as is" without express or implied warranty.
#
#	$Id: disksetup.sh,v 1.1 1995/12/16 22:20:48 ghudson Exp $
#	$Source: /mit/netbsd/dev/install/server/RCS/disksetup.sh,v $

# /tmp/vars outputs:
#	disk		disk device containing NetBSD partition
#	start		starting sector of NetBSD partition
#	size		number of sectors of NetBSD partition
#	cylinders	number of cylinders on $disk
#	sectors		number of sectors per track on $disk
#	heads		number of tracks per cylinder on $disk
#	created		NetBSD partition was created by makepart

. /utils

# Find all disks with a NetBSD partition, as well as their geometries and
# the start and size of the NetBSD partition on the disk.
disks=
for i in wd0 wd1 sd0 sd1 sd2; do

	# Parse the output of fdisk for this disk.
	cylinders=;heads=;sectors=;start=;size=
	eval `fdisk /dev/r${i}d 2>&1 | awk --field-separator="=|,?[ \t]*" '
		/^cylinders=[0-9]*/	{ print "cylinders=" $2;
					  print "heads=" $4;
					  print "sectors=" $6; }
		/^sysid 165/		{ getline;
					  print "start=" $3;
					  print "size=" $5; }'`

	# If we found a geometry and a NetBSD partition, remember the disk
	# and keep track of its geometry in variables like "sd0cylinders".
	if [ -n "$cylinders" -a -n "$start" ]; then
		disks="$disks $i"
		eval "${i}cylinders=$cylinders"
		eval "${i}heads=$heads"
		eval "${i}sectors=$sectors"
		eval "${i}start=$start"
		eval "${i}size=$size"
	fi
done

echo ""
if [ -z "$disks" ]; then
	# Try making a new partition.
	/makepart || exit 1
	exit 0
fi

echo "Found NetBSD partitions on the following disks:$disks"
proto_disk=`echo $disks | awk '{print $1;}'`
cylinders=
while [ -z $cylinders ]; do
	echo -n "Install on which disk? [$proto_disk] "
	getresp "$proto_disk"; disk="$resp"
	eval "cylinders=\${${resp}cylinders}"
	eval "heads=\${${resp}heads}"
	eval "sectors=\${${resp}sectors}"
	eval "proto_start=\${${resp}start}"
	eval "proto_size=\${${resp}size}"
done

cyl=`expr "$proto_start" / "$sectors" / "$heads"`
meg=`expr "$proto_size" / 2048`
echo ""
echo "The master boot record partition table believes that your NetBSD"
echo -n "partition starts at sector $proto_start (in cylinder $cyl)"
echo " and is $proto_size"
echo "sectors long (${meg}MB)."
echo ""
echo -n "What sector does your NetBSD partition start at?"
echo -n " [$proto_start] "
getresp "$proto_start"; start="$resp";
echo -n "How many sectors long is your NetBSD partition?"
echo -n " [$proto_size] "
getresp "$proto_size"; size="$resp"
created=no

cat >> /tmp/vars << EOM
disk="$disk"
start="$start"
size="$size"
cylinders="$cylinders"
sectors="$sectors"
heads="$heads"
created=no
EOM

