#! /usr/bin/perl -I../../../maint -Imaint
#
# Read files, looking for MPIU_Param_register( ... ).  Extract the 
# arguments: name, environment-variable-name, and description.
# 
require "../maint/parse.sub";

$debug = 0;
$showfiles = 0;

# Check for special args
@files = ();
foreach $arg (@ARGV) {
    if ($arg =~ /^-showfiles/) { $showfiles = 1; }
    elsif( $arg =~ /-debug/) { $debug = 1; }
    else {
	print "Adding $arg to files\n" if $debug;
	$files[$#files+1] = $arg;
    }
}
# Process the definitions
foreach $file (@files) {
    print "$file\n" if $showfiles;
    &ProcessFile( $file );
}

&OutputRegistered;

# ---------------------------------------------------------------------------

sub ProcessFile { 
    my $filename = $_[0];
    open (FD, "<$filename" ) || die "Could not open $filename\n";

    while (<FD>) {
	# Skip the definition of the function
	if (/int\s*MPIU_Param_register/) { next; }
	while (/MPIU_Param_register\s*(\(.*$)/) {
	    ($leader, $remainder, @args ) = &GetSubArgs( FD, $1 );
	    if ($debug) {
		foreach $arg (@args) {
		    print "|$arg|\n";
		}
	    }
	    $name = $args[0];
	    $envname = $args[1];
	    $descript = $args[2];
	    $_ = $remainder;
	    print "Parameters %name:$envname:$descript\n";
	    #
	    # Add to a hash so that we can output the 
	    # appropriate definition file
	    $parms{$envname} = "string";
	    $parmsval{$envname} = "";
	}
    }		
    close FD;
}


sub OutputRegistered  {
    foreach my $envname (sort keys(%parms)) {
	my $kind = $parms{$envname};
	my $defval = $parmsval{$envname};
	print "{ \"$envname\", $kind, $defval }\n";
    }
}
