#!/usr/bin/perl
#
#  Usage: testDoc [-V] -
#
#  compares the list of directories 
#	that are named in the toc.txt file, 
# 	that contain a makefile, and 
#	that contain a paper file. 
#
#  Options: 
#    -v  list intermediate results for debugging
#
# For improvements, please edit the original in /homes/sep/matt/usr/srcAdm
#
# author: Matthias Schwab
#%

# commandline processing 
require "getopts.pl" ;

if ( $#ARGV < 0 ) {
  &usage ;
  }

&Getopts( 'v' ) ;
if ( $opt_v ) {
  $verbose = 'yes' ;
  }

# read and preprocess the listings into proper string lists with ":" seperator

@toc    = `cat toc.txt`;
$toclist = &tocproc(@toc); 
if ($verbose) { print "toclist:\n $toclist    \n"; }

@papers = `makeDocList -p`; 
$paperlist = &fileproc(@papers);
if ($verbose) { print "paperlist:\n $paperlist\n"; }

@make   = `makeDocList -m`; 
$makelist = &fileproc(@make);
if ($verbose) { print "makelist:\n $makelist  \n"; }

@java   = `makeDocList -j`;
$javalist = &fileproc(@java);
if ($verbose) { print "javalist:\n $javalist  \n"; }

# compare the listings (partialdiff returns items of 2nd arg not in 1st arg)

@tocButNotPaper  = &partialdiff($paperlist, $toclist  );
@paperButNotToc  = &partialdiff($toclist,   $paperlist);
@tocButNotMake   = &partialdiff($makelist,  $toclist  );
@paperButNotMake = &partialdiff($makelist,  $paperlist);
@javaButNotMake  = &partialdiff($makelist,  $javalist );

# and print out 

if (@tocButNotPaper) {
	print "tocButNotPaper:\n";
	&printlist(@tocButNotPaper); 
}
if (@paperButNotToc) {
	print "paperButNotToc:\n";
	&printlist(@paperButNotToc); 
}
if (@tocButNotMake) {
	print "tocButNotMake:\n";
	&printlist(@tocButNotMake); 
}
if (@paperButNotMake) {
	print "paperButNotMake:\n";
	&printlist(@paperButNotMake); 
}
if (@javaButNotMake) {
	print "javaButNotMake:\n";
	&printlist(@javaButNotMake); 
}

# the rest are subroutines

sub printlist {
	local (@list) = @_;
	foreach $item (@list) {
		print "$item\n";
	}
}

sub partialdiff {
	local($baseset, $testset) = @_;
	#print "$baseset\n"; 
	@testlist = (); 
	@testset = split(":",$testset);
	foreach $testi (@testset) {
		if (index($baseset,$testi) < 0) {
			#print "I test $testi \n";
			push(@testlist, $testi); 
		}
	}
	return @testlist;
}
 
sub fileproc {
	local(@file) = @_ ;
	foreach $line (@file) {
		chop($line);
		if ($filelist) {
			$filelist = join(":",$filelist,$line);
		}
		else {
			$filelist = $line;
		}
	}
	return $filelist;
}

sub tocproc  {
	local(@toc) = @_ ;
	foreach $toci (@toc) {
		chop($toci);
		if ($toci !~ /^\%/      ) {
		if ($toci !~ /\\section/) {
			$toci =~ s/\%\w*//;
			$toci =~ s/\s//g;
			if ($toclist) {
				$toclist = join(":",$toclist,$toci);
			}
			else {
				$toclist = $toci;
			}
		}}
	}
	return $toclist;
}

sub usage {
  open( THIS, $0 ) ;

  while ( <THIS> ) {
       if ( $_ =~ /^\#/ ) {
         next if $_ =~ /^\#\!/ ;
         exit if $_ =~ /^\#\%/ ;

         print ;
         }
       }

  exit( 0 ) ;
  }
