#!/usr/local/bin/perl
# -*- perl -*-

# Copyright (c) 1999 by Jeff Weisberg
# Author: Jeff Weisberg <jaw @ tcp4me.com>
# Function: calculate partition table params


use Getopt::Std;
getopts("bc:h:s:vu");

sub usage {
    print <<U;
usage:
	-c cyl
	-h heads
	-s sects
	-b    netbsd syle [optional, default is SunOS style]
	-v    verbose [optional]
	-u    show usage info
U
    ;
    exit 0;
}

usage() if( $opt_u );
$cyl = $opt_c || usage();
$hds = $opt_h || usage();
$sec = $opt_s || usage();

$nsect = $cyl * $hds * $sec;
$megs  = int( $nsect * 512 / 1024 / 1000 );

print STDERR "$megs MB, $cyl cyl, $hds heads, $sec sec, $nsect sectors\n" if $opt_v;


$total = 0;
$remain = $megs;
@parts = ('a'..'h');

foreach $p ( @parts ){
    next if $p eq 'c';
    print STDERR "$p size ($remain remain)? ";
    chop( $s = <> );

    $total += $s;
    $remain -= $s;
    $desired{$p} = $s;
}

$k = $cyl / $megs;

print "\n";
$start = 0;
foreach $p (@parts){

    if( $p ne 'c' ){
	$d = $desired{$p};

	if( $p eq 'h' ){
	    $c = $cyl - $start;
	}else{
	    $c = int( $d * $k + .99 );
	}
	$a = sprintf "%.2f", $c / $k;
	$s = $c * $hds * $sec;
	$o = $start * $hds * $sec;
	$st = $start;
	$type = ($p eq 'b')? 'swap' : '4.2BSD';
	
	print "$p: start = $st, ncyl = $c, nsec = $s, $a MB\n" if $opt_v;

	$start += $c;
    }else{
	$s = $nsect;
	$c = $cyl;
	$a = $c / $k;
	$o = 0;
	$st = 0;
	$type = 'unused';
    }
    
    if( $opt_b ){
	print "  $p: $s\t$o\t$type\t0 0\t# (ncyl $c \@ $st) $a MB\n";
    }else{
	printf "  partition $p - starting cyl %8d, # blocks %9d  ($c/0/0)\n", $st, $s;
    }

}

