#!/usr/bin/perl -T

# Doreva Belfiore
# Rutgers University Law Library
# Camden, NJ

# multivp.cgi  = #1 of 2

# This script finds multi-volume documents that need manual pagination
# since MARC records only show set-level pagination. If found, the script
# presents first document to user (Law Library staff member) for review.

# Revision date:  9/27/2011


$ENV{"PATH"} = "/bin:/usr/bin:/usr/local/bin";
use CGI qw(param);
use strict;


#1 Define directories and grep multi-part volumes only

my $rdir = "../MULTIV";

#open the MULTIV directory
opendir (DIR, "$rdir");
#grep line will fetch all those directories with parts a-az
my @lccns = grep /([0-9]{1,8})([a-z]{1,2})/, readdir DIR;
closedir DIR;

@lccns = sort(@lccns);         

my $doc = $lccns[0];
if($doc =~ /.*?(\w+).*/){
 $doc = $1;
  }

#2 Open directories and grep the files

opendir DIR2, "$rdir/$doc";
my @files = grep /\.tif/, readdir DIR2;
closedir DIR2;
@files = sort(@files);
my $fnumber = @files;
         
my $filey = $files[0];                  
#if($filey =~ /.*?(\w+).*\.tif/){
#$filey = $1;
#  }


#3 Calculate pagination

my $page = 1;
$page = sprintf("%04d",$page);  


#4 See if there are available documents to process                                         
MAIN: {      

# tests if LCCN (document) array has been defined or not
# to test if documents are present in the /MULTIV directory to work on

if (@lccns) {

	foreach $doc (@lccns) {
		&convert1;
		&form1;

	} # end foreach loop

    } #end if loop


else {

&form2;

}


} #end MAIN


# SUBROUTINES SECTION

sub form1 {

#form1 prompts for pagination of multi-part documents
#Can be customized with local website graphics
print <<END_OF_HTML;
Content-type: text/html


<html>
   <TITLE>Government Document Quality Control Check</TITLE>
    </HEAD>


<img src=”ENTER YOUR OWN BANNER HERE” align="top" width ="404"</a>


<IFRAME SRC="../MULTIV/$doc/$doc-$page.jpg" align="right" WIDTH="288" 
height="90%"></IFRAME>
<table width="50%">
<tr><td>

     <BODY>
      <H2>Government Document Quality Check</H2>
      <H3>Multivolume Page Confirmation Check</H3>


<font size=+1>Is this file the correct document?: $doc:</font>

<p>
<p>

<form method="POST" action="/INSERT SERVER PATH HERE/multivp2.cgi">        
<table>
<tr><td>
<input type="radio" name="confirmation" value="0">The file is correct. Continue.<br><br>
</tr></td>
<tr><td>
<input type="radio" name="confirmation" value="1">The file is bad. Skip it.<br><br><br>
</tr></td>
<tr><td>
<input type="submit" value="SUBMIT"> <input type="reset" value ="RESET FORM">
<input type="hidden" name="lccn" value="$doc">
<input type="hidden" name="page" value="$page">
<br><br>
<br><br>
</tr></td>

</table>
</form>
</body>
</html>

END_OF_HTML

next MAIN;

} # end sub form1


sub form2 {

#form2 tells the end-user that there are no documents to process
            
print <<END_OF_HTML;
Content-type: text/html

<html>
   <TITLE>Government Document Quality Control Check</TITLE>
    </HEAD>

<img src=”ENTER YOUR OWN BANNER HERE” align="top" width ="392"</a>

<table>
<tr><td>  

     <BODY>
      <H2>Government Document Quality Check<H2>
      <H3>Multivolume Page Confirmation Check</H3>


<font size=+1>There are currently no multi-volume set documents to process.</font>

<p>
<p>

</table>
</body>
</html>

END_OF_HTML

next MAIN;

} # end sub form2
      

sub convert1 {

chdir "$rdir/$doc";

#Tifftopnm converts tiff to jpg for web presentation in any browser without the need for plugins
#Changed quality to 50% to save processing time and server space

system "tifftopnm '$doc-$page.tif' |pnmtojpeg -quality=50 >'$doc-$page.jpg'";

}
