#!/usr/bin/perl

# gdocprecheck.pl

# Checks for empty directories in staging folder (called GDOC) that students
# may have created by mistake. Also checks that no congressional documents have been
# scanned as PDF's by mistake (This has happened in the past if a scan
# setting is mistakenly changed.) Finally, checks for hyphenation problems.

# Doreva Belfiore
# Rutgers Camden Law Library
# Camden, NJ

# revision date: 9/21/2011

#Based on original script from John Joergensen

$rdir = "../gdoc";

open (ERR, ">>gdocprecheck.txt");

print ERR "List of empty directories that will be deleted.\n";

opendir DIR, "$rdir";
@list = grep /\d{8}/, readdir DIR;
closedir DIR;
@list = sort(@list);

MAIN: foreach $doc (@list){

opendir(DIR, "$rdir/$doc");
	@files = grep /tif|pdf|txt|html|rdf/, readdir DIR;
	closedir DIR;
	$number = @files;
	if($number == 0){
		
		print "$doc is empty! - Moving it to PROBLEM directory. \n";
		print ERR "$doc - empty and moved to RESCAN\n";
		system ("mv $rdir/$doc ../gdoc/RESCAN");
		
	}else {
		print "$doc has $number of live files \n";
	}

}

MAIN2: foreach $doc (@list){

       opendir(DIR, "$rdir/$doc");
       @files = grep /pdf/, readdir DIR;
	 closedir DIR;
	 $number = @files;
	 if($number == 0){
                                        
	 print "No problem. No miscanned PDFS here. \n";
                                                        
	 }else {
		print "$doc has $number of PDFS. Sent it to be rescanned. \n";
		print ERR "$doc has $number of PDFS. Sent it to be rescanned. \n";
		system ("mv $rdir/$doc ../gdoc/RESCAN");
		
		}
                                                                                                                                                                                                    
        }  #end MAIN2
        
        
MAIN3:  foreach $doc (@list){

        opendir(DIR, "$rdir/$doc");
        @files = grep /\d{8}-\d{4}.tif/, readdir DIR;
        closedir DIR;
        $number = @files;
        if($number == 0){
                                        
        print "$doc has a potential problem with hyphenation. \n";
        print ERR "$doc - hyphenation? \n";
        system ("mv $rdir/$doc ../gdoc/PROBLEM");
            
            }else {
                  print "$doc is OK. \n";
                                                                                                
              }
                                                                                                                
    } #end MAIN3
