#!/usr/local/bin/perl
#
# Joins all .java files in the specified directory and in a specified package
# into a single file
#
# For improvements, please edit the original in /homes/sep/matt/usr/srcAdm
# Joel Schroeder: Oct96
#

$rmvFlag = "-r";

$usage = "Usage: $0 packagename [$rmvFlag] dir > file \n";
$selfdoc = "
Joins all .java files in the specified package and into file.
The .java files are separated in file by a string of 60 dashes (-).
\tdir	- Where the .java files exist (cd if not specified).  
\t$rmvFlag	- Remove all .java files which are joined into file.
\tfile	- The file to create.
";

$delimiter = "------------------------------------------------------------\n";
$delimiterName = "/tmp/junk.delimiter";

if( $#ARGV < 0 ||$#ARGV > 2 ) {
  print $usage;
  die $selfdoc;
  }

$rmv = 0;
$dir = "./";

#$pkg = shift;
while ($arg = shift) {
    if ($arg eq $rmvFlag) {
	$rmv = 1; }
    if ($arg =~ /^[^-]/) {
	$dir = "$arg/"; }}

chdir( $dir) || die "Can't cd to $dir\n";

# print "dir    : $dir\nrmvFlag: $rmv\npackage: $pkg\n";

open(DFILE,">$delimiterName");
print DFILE $delimiter ;
close(DFILE);

$catFiles = "";
$rmvFiles = "";
while (<*.java>) {
    open(CURRFILE, $_) || die "Can't open $_";
    $currFileName = $_;

    while (<CURRFILE>) {
#	if (/^\s*package\s+$pkg\s*;/) {
	    $catFiles = "$catFiles $currFileName $delimiterName";
	    $rmvFiles = "$rmvFiles $currFileName";
	    last;
#	}
#	else {
#	    print STDERR "$currFileName not correct package ...\n";
#	    last;
#        }
    }
    close(CURRFILE);
}

$protectedDN = $delimiterName;
$protectedDN =~ s/(\W)/\\$1/g;
$catFiles =~ s/$protectedDN$//;

print `cat $catFiles`;

if ($rmv == 1) {
    `/bin/rm $rmvFiles`;
}

`/bin/rm $delimiterName`;
