# 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: makepart.sh,v 1.9 1995/12/16 22:16:20 ghudson Exp $
#	$Source: /usr/src/distrib/i386/server/RCS/makepart.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

echo "Your disks contain the following blocks of unused sectors:"
echo ""
best_size=0;
for i in wd0 wd1 sd0 sd1 sd2; do
	eval `fdisk /dev/r${i}d 2>&1 \
		| awk -F "=|,?[ \t]*" -v disk=$i -v best=$best_size '
		BEGIN			{ n = 0; }
		/^cylinders=[0-9]*/	{ cyl = $2; heads = $4; sect = $6; }
		/, size [1-9]/		{ start[n] = $3; size[n] = $5; n++; }
		END {
			if (n >= 4)
				exit 0;

			# Sort by starting sector.
			for (i = 1; i < n; i++) {
				tstart = start[i]; tsize = size[i];
				for (j = i - 1; j >= 0; j--) {
					if (tstart > start[j])
						break;
					start[j + 1] = start[j];
					size[j + 1] = size[j];
				}
				start[j + 1] = tstart;
				size[j + 1] = tsize;
			}

			# Output list of free sections.
			csect=sect - 1;
			best_size=best;
			for (i in start) {
				if (size[i] <= 0)
					continue;
				if (start[i] - csect < 20480) {
					if (csect < start[i] + size[i])
						csect = start[i] + size[i];
					continue;
				}
				printf "echo \"%s: start %d, size %d (%dMB)\";",
					disk, csect, start[i] - csect,
					(start[i] - csect) / 2048;
				if (start[i] - csect > best_size) {
					best_start = csect;
					best_size = start[i] - csect;
				}
				csect = start[i] + size[i];
			}
			total = cyl * heads * sect;
			if (total - csect > 20480) {
				printf "echo \"%s: start %d, size %d (%dMB)\";",
					disk, csect, total - csect,
					(total - csect) / 2048;
			}
			if (total - csect > best_size) {
				best_start = csect;
				best_size = total - csect;
			}

			if (best_size > best) {
				printf "best_disk=%s;", disk;
				printf "best_start=%d;", best_start;
				printf "best_size=%d;", best_size;
			}
		}'`
done

if [ "$best_size" -lt 20480 ]; then
	echo "Didn't find any NetBSD partitions or blocks of free"
	echo "sectors.  You must free up space on your hard disk"
	echo "before installing."
	exit 1
fi

echo ""
echo "If you would like to create a NetBSD partition in any of the"
echo "unused blocks of sectors, enter the disk, starting sector, and"
echo "number of sectors below (or accept the default values to use the"
echo "largest block of free sectors).  If you don't want to create a"
echo "NetBSD partition, type ^C and 'halt'."
echo ""
echo -n "Create a partition on what disk? [$best_disk] "
getresp "$best_disk"; disk="$resp"
echo -n "What starting sector? [$best_start] "
getresp "$best_start"; start="$resp"
echo -n "How many sectors? [$best_size] "
getresp "$best_size"; size="$resp"

# Find a free partition on the selected disk.
eval `fdisk /dev/r${disk}d 2>&1 | awk '
	/^The data for partition/	{ n = $5; }
	/<UNUSED>|size 0/			{ printf "freepart=%d;", n; exit 0; }'`
if [ -z "$freepart" ]; then
	echo "Couldn't find a free partition on ${disk}."
	exit 1
fi

# Set the free partition to the given values.
echo "n" > /tmp/fdiskcmd
yes "n" | head -${freepart} >> /tmp/fdiskcmd
echo "y" >> /tmp/fdiskcmd
echo "165" >> /tmp/fdiskcmd
echo "${start}" >> /tmp/fdiskcmd
echo "${size}" >> /tmp/fdiskcmd
echo "n" >> /tmp/fdiskcmd
echo "y" >> /tmp/fdiskcmd
yes "n" | head -`expr 3 - ${freepart}` >> /tmp/fdiskcmd
echo "n" >> /tmp/fdiskcmd
echo "y" >> /tmp/fdiskcmd
if `fdisk -u /dev/r${disk}d < /tmp/fdiskcmd > /dev/null`; then
else
	exit 1
fi
echo ""

# Make one more pass over the fdisk output to pick up the geometry.
eval `fdisk /dev/r${disk}d 2>&1 | awk --field-separator="=|,?[ \t]*" '
	/^cylinders=[0-9]*/	{ print "cylinders=" $2;
				  print "heads=" $4;
				  print "sectors=" $6; }'`

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

