#!/usr/local/bin/perl

# reads the output from report_hierarchy
# and generates a Makefile with the design dependencies

# expects to see a '1' as the last line in the file!

# Steve Golson -- Trilobyte Systems -- sgolson@trilobyte.com
# @(#)make_hier	1.1 02/26/01 15:22:31

# skip the preamble until you get to a single word
while (<>) {
    ($spaces, $module, $therest) = /^(\s*)(\w*)(.*)$/;
    if (length($module) != 0 and length($therest) == 0) { last ; }
}

push @{$names[0]}, $module;
print "top: ${module}.db\n\n";

while (<>) {
    ($spaces, $module, $therest) = /^(\s*)(\w*)(.*)$/;

    if (length($therest) != 0) { next ; }

    # get the indentation amount
    $indent = length($spaces) / 4;

    if ($indent == $last_indent) {
	# push onto existing list
	push @{$names[$indent]}, $module;
    }
    elsif ($indent > $last_indent) {
	# push onto new list
	push @{$names[$indent]}, $module;
    }
    else {
	# pop all lists lower than current one
	for ($i=$last_indent; $i>$indent; $i--) {
	    print "$names[$i-1][-1].db:";
	    while (@{$names[$i]}) {
		$sub = shift(@{$names[$i]});
		print " ${sub}.db";
	    }
	    print "\n\n";
	}
	push @{$names[$indent]}, $module;
    }

    $last_indent = $indent;
}
