#!/usr/bin/perl
#
#  Usage: makeDocList [-p -m -j -d dir] > filelist 
#
#  Runs a find command to similar to 
#
#       find dir -name makefile -print 
#
#  Options: 
#    -d dir  sets the root directory for the find search.
#            the default is the current directory ".".
#
#    -m      lists all directories with makefiles. Runs a find 
#            commmand similar to
#
#            find dir -name "[Mm]akefile" -print       
#
#    -p      lists all directories with paper.tex files. Runs a find 
#            commmand similar to
#
#            find dir -name "paper.tex" -print  
#    
#    -j      lists all directories with java files. Runs a find 
#            command similar to 
#
#            find dir -name "*.java" -print 
#
#    -c      lists all directories with class files.
#
# For improvements, please edit the original in /homes/sep/matt/usr/srcAdm
#   author: Matthias Schwab
#%

require "getopts.pl" ;

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

&Getopts( 'mpjcd:' ) ;

$dir = '.';
if ( $opt_d ) {
  $dir = $opt_d ;
}
#print "dir is $dir\n";

  $make = "no" ;
if ( $opt_m ) {
  $make = "yes";
}

  $papr = "no" ;
if ( $opt_p ) {
  $papr = "yes";
}

  $java = "no" ;
if ( $opt_j ) {
  $java = "yes";
}
  $clas = "no" ;
if ( $opt_c ) {
  $clas = "yes";
}
use File::Find;

# find list of files based on several search criteria

if    ($make eq "yes" && $java eq "yes") {
    find(\&wanted0, "$dir");
} 
elsif ($make eq "yes") {
    find(\&wanted1, "$dir");
} 
elsif ($java eq "yes") { 
    find(\&wanted2, "$dir");
}
elsif ($clas eq "yes") { 
    find(\&wanted4, "$dir");
}
elsif ($papr eq "yes") {
    find(\&wanted3, "$dir");
}
else {
    print "makeDocList: Error flag not recognized ..\n";
}

# here are the search criteria:
# I probably should have a criteria for lower letter dirs with java and make

# any directory with lower letter path and a makefile 
sub wanted1 { 
    $dir = $File::Find::dir;
    $dir !~ /\/[A-Z]/ &&
    /^[Mm]akefile$/ &&
	print "$File::Find::dir\n";
}
# any directory with lower letter path and a java file 
sub wanted2 {
    $dir = $File::Find::dir;
    $dir !~ /\/[A-Z]/ && 
    /\.java$/ &&
    $File::Find::dir ne $buffer && 
    ($buffer = $File::Find::dir) && 
	print "$File::Find::dir\n";
}
# any directory with lower letter path and a class file 
sub wanted4 {
    $dir = $File::Find::dir;
    $dir !~ /\/[A-Z]/ && 
    /\.class$/ &&
    $File::Find::dir ne $buffer && 
    ($buffer = $File::Find::dir) && 
	print "$File::Find::dir\n";
}

# any directory with lower letter path and a paper.tex file  
sub wanted3 {
    $dir = $File::Find::dir;
    $dir !~ /\/[A-Z]/ &&
    /^paper\.tex$/ &&
	print "$File::Find::dir\n";
}

sub usage {
  open( THIS, $0 ) ;

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

         print ;
         }
       }

  exit( 0 ) ;
  }
