#!/usr/bin/perl

# Load the "all_files" stuff...

open(ALL, "<all_files") or die "can't open all_files";

%approvals = ();
%issueps = ();
%approvedps = ();

while ($line = <ALL>) {

	next if $line =~ /^#/ ;
	next if $line =~ /^\s*$/ ;

	$dir = $file = $ext = $issuep = $approvedp = $approval = undef;

	if ($line =~ /^(\S+)\/(\S+)\.(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+$/ ) {
		$dir = $1;
		$file = $2;
		$ext = $3;
		$issuep = $4;
		$approvedp = $5;
		$approval = $6;
	} elsif ($line =~ /^(\S+)\/(\S+)\.(\S+)\s+(\S+)\s+(\S+)\s+$/ ) {
		$dir = $1;
		$file = $2;
		$ext = $3;
		$issuep = $4;
		$approvedp = $5;
	}

	if (exists($approvals{"$dir/$file"}) and $approvals{"$dir/$file"} ne $approval) {
		print "ERROR: inconsistend approval status for $dir/$file\n";
	}
	$approvals{"$dir/$file"} = $approval;
	if (exists($approvedps{"$dir/$file"}) and $approvedps{"$dir/$file"} ne $approvedp) {
		print "ERROR: inconsistent approval for $dir/$file\n";
	}
	$approvedps{"$dir/$file"} = $approvedp;
	if (exists($issueps{"$dir/$file"}) and $issueps{"$dir/$file"} ne $issuep) {
		print "ERROR: inconsistend issue status for $dir/$file\n";
	}
	$issueps{"$dir/$file"} = $issuep;

}

close(ALL);

#print "These are test cases with a currently undetermined status:\n";
#foreach $dirfn (sort keys %approvals) {
#	next if $approvals{$dirfn} ;
#	print "Directory: $dirfn\n";
#}
#print "These are the approved test cases I know about:\n";
#foreach $dirfn (sort keys %approvals) {
#	next unless $approvals{$dirfn} ;
#	print "Directory: $dirfn approved = $approvals{$dirfn}\n";
#}

# OK, we're missing a HUGE bunch of approvals. Never-the-less:
# On the command-line, we expect a list of directory names to make manifest files into.

while ($dir = $ARGV[0]) {
	shift;

	print "Handling directory $dir\n";

	@files = glob("$dir/*.*");

	open (MANIFEST, ">$dir/Manifest.rdf") or die "Can't open the manifest file in $dir";
	# Preamble...
	open (HEADER, "<skeleton/manifestHead.rdf") or die "Can't open header";
	while (<HEADER>) { print MANIFEST; }
	close (HEADER);

	foreach $fn (@files) {

		#print "file to take care of: $fn\n";
		next unless $fn =~ /^([^\s\/]+)\/([^\s\.]+)\.(\S+)$/;

		$d = $1; $filebase = $2; $ext = $3;

		if ($filebase =~ /^test/i && $ext =~ /^rdf$/i) {

			# We've got a parser test file, I expect...
			&handle_positive_parser_test($dir, $filebase, $ext);

		} elsif ($filebase =~ /^warn/i && $ext =~ /^rdf$/i) {

			# We've got a parser test file with warning
			&handle_positive_parser_test($dir, $filebase, $ext);

		} elsif ($filebase =~ /^error/i && $ext =~ /^rdf$/i) {

			# We've got a parser test file, I expect...
			&handle_negative_parser_test($dir, $filebase, $ext);

		} elsif ($filebase =~ /^test/i && $ext =~ /^nt$/i) {
			# do nothing, should be handled elsewhere
		} elsif ($filebase =~ /^warn/i && $ext =~ /^nt$/i) {
			# do nothing, should be handled elsewhere
		} else {

			print "Warning! no idea how to handle file: $dir/$filebase.$ext\n";

		}

	}

	open (FOOTER, "<skeleton/manifestTail.rdf") or die "Can't open header";
	while (<FOOTER>) { print MANIFEST; }
	close (FOOTER);
	close (MANIFEST);

}

sub handle_positive_parser_test($$$) {
	my ($dir, $filebase, $ext) = @_;

	# Check the .nt files exist!
	if (not -e "$dir/$filebase.nt") {
		print "WARNING! $dir/$filebase.nt nonexistant: corresponding .rdf file is on its own\n";
		return;
	}

	open (PPT, "<skeleton/positiveParserTest.rdf") or die "Can't open skeleton/positiveParserTest.rdf!";
	&spit_out_stuff($dir, $filebase, $ext);
	close(PPT);

}

sub spit_out_stuff($$$) {
	my ($dir, $filebase, $ext) = @_;

	# Is this an approved test?
	my ($approved) = ($approvedps{"$dir/$filebase"});
	my ($approval) = ($approvals{"$dir/$filebase"});
	my ($issuep) = ($issueps{"$dir/$filebase"} =~ /^ISSUE/i );
	my ($issue);

	if ($issueps{"$dir/$filebase"} =~ /^ISSUE/i ) {
		$issue = $dir;
	} elsif ($issueps{"$dir/$filebase"} =~ /^ISSUE=(.*)$/i ) {
		$issue = $1;
	}

	my $line;
	while ($line = <PPT>) {

		# Do we write this line?
		if ($line =~ /^@@\?([^@]+)@@(.*)$/) {
			$line = $2 . "\n";
			my $test = $1;

			next if ($test eq 'ISSUE' && not $issuep) ;
			next if ($test eq 'STATUS' && not $approved) ;
			next if ($test eq 'APPROVAL' && not $approval) ;
			next if ($test eq 'WARNING' && not $filebase =~ /^warn/i ) ;

		}

		# make any changes required to the line.
		$line =~ s/\@\@DIRNAME\@\@/$dir/eg;
		$line =~ s/\@\@FILEBASE\@\@/$filebase/eg;
		$line =~ s/\@\@STATUS\@\@/$approved/eg;
		$line =~ s/\@\@APPROVAL\@\@/$approval/eg;
		$line =~ s/\@\@ISSUE\@\@/$issue/eg;

		print MANIFEST $line;

	}

}


sub handle_negative_parser_test($$$) {
	my ($dir, $filebase, $ext) = @_;

	# Is this an approved test?
	my ($approved) = ($approvedps{"$dir/$filebase"});
	my ($approval) = ($approvals{"$dir/$filebase"});
	my ($issuep) = ($issueps{"$dir/$filebase"} eq 'ISSUE');
	my ($issue) = ($dir);

	open (PPT, "<skeleton/negativeParserTest.rdf") or die "Can't open skeleton/negativeParserTest.rdf!";
	&spit_out_stuff($dir, $filebase, $ext);
	close(PPT);

}
