# Go through list of files and make sure everything is in place in the
# /home/harris41/LON-CAPA-topdir_for_build/SOURCES/TemporaryPackaging/SourceRoot
# directory.

# This is a temporary script which needs to be replaced by a more comprehensive
# build process.

open IN, "<../../../SPECS/LON-CAPA-base-3.1.spec" or die "can't open base spec file";

$changes=0;
$total=0;

while (<IN>) {
    chop;
    if (!/\%dir/ && /\%attr/) {
	$total++;
	@stuff=split(/\s+/);
	$file=$stuff[$#stuff];
	$sourceroot="../SourceRoot/$file";
	$name=$file; $name=~s/.*\/(.*)$/$1/;
	unless (-e "$sourceroot" || ($name=~/\*/)) {
	    print "XXXXXXXX     FILE MISSING: $file\n";
	}
	elsif ($name=~/\*/) {
	    print "--------     no globbing : $file\n";
	}
	else {
	    $md5=`md5sum $sourceroot | cut -d ' ' -f1`; chop $md5;
	    @results=`find ../../loncom -name $name`;
	    unless (@results) {
		@results=`find ../../rat -name $name`;
	    }
	    $match=0;
	    RES: foreach $res (@results) {
		chop $res;
		$rmd5=`md5sum $res | cut -d ' ' -f1`; chop $rmd5;
		if ($md5 eq $rmd5) {
		    print "*** $file match with $res\n";
		    $match=1;
		    last RES;
		}
	    }
	    unless ($match) {
		$changes++;
		print "\n\n\n";
		print "Change for $file from: " . join(",",@results) . "\n";
#		if (@results==1) {
		    print `diff $sourceroot $results[0] | grep '> '`;
		    `cp $results[0] $sourceroot`;
		    print "\n\n\n";
#		}
	    }
	}

    }
}

print "Changes: $changes\n";
print "Total files: $total\n";
