#!/usr/bin/perl -T

# Doreva Belfiore/John Joergensen
# Rutgers University Law Library
# Camden, NJ

# multivp2.cgi  = #2 of 2

#N.B. – This file was edited by John Joergensen to reduce redundancy and improve functionality. 
#The author wishes to thank Mr. Joergensen for his assistance with this script.


# This script allows user (Law Library staff member) to navigate between
# pages of a multi-volume document and then enter pagination data
# to be submitted to a final CGI that writes the data out to a text file.

# Revision date:  11/14/2011


#1 Set environment, path, & remote directories

$ENV{"PATH"} = "/bin:/usr/bin:/usr/local/bin";
use CGI qw(param);
use strict;


#set remote directories and parameters
my $bdir = "/ INSERT SERVER PATH HERE /gdoc/PROBLEM";
my $rdir = "/ INSERT SERVER PATH HERE /gdoc/MULTIV";
my $gdir = "/ INSERT SERVER PATH HERE/gdoc";
my $doc = param("lccn");
my $pnum  = param("preface");
my $firstpage = param("firstpage");
my $lastpage = param("lastpage");
my $preface = param("preface");
my $action = param("action");
my $npage = param("npage");
my $ppage = param("ppage");
my ($filey, $fnumber);
my $page = param("page");
my $fpage = param("fpage");
my $lpage = param("lpage");
my $confirm  = param("confirmation");


#######example test params#############
#$doc = "70601813d";
#$firstpage = 1;
#$lastpage = 773;
#$pnum = 4;
#$action = "SUBMIT";

#############################

# Checking the parameters
if($npage =~ /(\d+)/){
  $npage = $1;
}
if($ppage =~ /(\d+)/){
  $ppage = $1;
}
if($action =~ /(\w+)/){
 $action = $1;
}
if($firstpage =~ /(\d+)/){
$firstpage = $1;
}
if($lastpage =~ /(\d+)/){
$lastpage = $1;   
}
if($preface =~ /(\d+)/){
$preface = $1;   
}
if($doc =~ /(\w+)/){
$doc = $1;
}

# action determinations
if($action eq "SUBMIT"){
&write_out;
exit;
}elsif($action =~ /Next/i){
  $page = $npage;
}elsif($action =~ /First/i){
  $page = "0001";
}elsif($action =~ /Prev/i){
  $page = $ppage;
}elsif($action =~ /Last/i){
  $page = $lpage;
}

#2 set parameters and variables
# source file = multivp.cgi

if($confirm =~ /[0-9]{1}/){
  $confirm = $1;
  }
  
$page = sprintf("%04d", $page);

#2 opens directory and greps the files
opendir DIR2, "$rdir/$doc";
my @files = grep /\.tif/, readdir DIR2;
closedir DIR2;
@files = sort(@files);
$fnumber = @files; 
      
$filey = $files[0];


#3 Page calculations         

if ($ppage =~ /(\d+)/) {
$ppage = $1;
}
if ($page == 1) {
   $ppage = $page;                         
}else{
   $ppage = ($page - 1);
}                  
$ppage = sprintf("%04s", $ppage);
$npage = ($page + 1);
#print "npage after addition = $npage \n";
$npage = sprintf("%04s", $npage);
$fpage = 1;                   
$fpage = sprintf("%04s", $fpage);
$lpage = $fnumber;
$lpage = sprintf("%04s", $lpage);                                                                                           

  
if ($confirm == 0) {
&convert1;
&countpage;
}elsif ($confirm == 1) {
&goback;
}


# subroutines

sub countpage {
 
print <<END_of_HTML;
Content-type: text/html


<HTML>
 <HEAD>
   <TITLE>Government Document Quality Control Check</TITLE>
    </HEAD>
<BODY>
<img src= “INSERT YOUR OWN BANNER HERE” align="top" width ="404"</a>


<IFRAME SRC="../MULTIV/$doc/$doc-$page.jpg" align="right" WIDTH="880"
height="300%"></IFRAME>
<table width="20%">  
<tr><td>
<H2>Government Document Quality Check</H2>
<H3>Multivolume Page Confirmation Check</H3>
<P>
<tr><td>      
<font size=+1>Please confirm the pages for: $doc:</font>
<hr>
<form method="POST" action=" INSERT SERVER PATH HERE /multivp2.cgi">
<table>
<tr><td>
 <input type="hidden" name="lccn" value="$doc">
 <input value="$fpage" type="hidden" name="fpage"> <!-- first page form-->
 <input value="$ppage" type="hidden" name="ppage">  <!--prev page form-->
 <input value="$npage" type="hidden" name="npage">  <!--next page form-->
 <input value="$lpage" type="hidden" name="lpage">  <!--last page form-->  
<table>
<tr><td>Pages of prefatory material<BR>
(Enter arabic numeral!):</td><td> <input name="preface" value="$preface" size=4>
</td></tr>
<tr><td>Page number, FIRST numbered page:</td>
<td><input name="firstpage" value="$firstpage" SIZE=4>
</td></tr>
<tr><td>
Page number, LAST numbered page:</td>
<td><input name="lastpage" value="$lastpage" size=4>
</td></tr>
<tr><td>
<input type="submit" value="SUBMIT" NAME="action">
<INPUT TYPE="reset" value="RESET FORM">
</td></tr>
</table>
<table>
<tr><td><span align="center"><input value="First_Page" type="submit" name="action"></span></td>
<td> <span align="center"><input value="Prev_Page" type="submit" name="action"></span></td>
<td><span align="center"><input value="Next_Page" type="submit" name="action"></span></td>
<td><span align="center"><input value="Last_Page" type="submit" name="action"></span></td>
</tr>
</table>
</form>
<p>

<!-- Manual Page Selection -->

<table>
<tr><td>
<form method="get"
action="/ INSERT YOUR SERVER PATH HERE /multivp2.cgi">
<p>

Go to page:
 <input name="page" size=4>
 <input value="$doc" type="hidden" name="lccn">
 <span align="center">
 <input value="Go!" type="submit">
 </p>
 </form>
 <br />
 

</table>

</BODY>
              
</HTML>
              
END_of_HTML

}  #end sub countpage


sub goback {

#move bad directory
system "mv $rdir/$doc $bdir/$doc";

#move to next directory to process
print <<END_of_HTML;
Content-type: text/html


<HTML>
 <HEAD>
   <TITLE>Government Document Quality Control Check</TITLE>
    </HEAD>

<img src= “INSERT YOUR OWN BANNER HERE” align="top" width ="404"</a>

<table>
<tr><td>

     <BODY>
      <H2>Government Document Quality Control Check</H2>
      <H3>Multivolume Page Confirmation Check</H3>
        <P>
<tr><td>

<font size=+1>Skipping to next multipage-set document.</font>

<form method="LINK" action="/INSERT SERVER PATH HERE/multivp.cgi">
<input type="submit" VALUE="CONTINUE">

<p>
<p>

END_of_HTML



} #end sub goback


sub convert1 {

chdir "$rdir/$doc";
   
#Calls tifftopnm converter program to convert TIFF images to JPG images on the fly
# for web presentation   
#Set quality to 50% to save server space and processing time
    
   system "tifftopnm '$doc-$lpage.tif' |pnmtojpeg -quality=50 >'$doc-$lpage.jpg'";
   system "tifftopnm '$doc-$npage.tif' |pnmtojpeg -quality=50 >'$doc-$npage.jpg'";
   system "tifftopnm '$doc-$ppage.tif' |pnmtojpeg -quality=50 >'$doc-$ppage.jpg'";
   system "tifftopnm '$doc-$fpage.tif' |pnmtojpeg -quality=50 >'$doc-$fpage.jpg'";
   
   } #end sub convert1
   
   
   
sub write_out {
open(PAGER,">$rdir/$doc/$doc.out") || die "could not open out file:$!\n";
my $govdir = "/INSERT SERVER PATH HERE/gdoc";
print PAGER "$doc,";
print PAGER "$pnum,";
print PAGER "$firstpage,";
print PAGER "$lastpage";  
print PAGER "\n";
close PAGER;

#chdir "$govdir/$doc";
system "rm $rdir/$doc/*.jpg";
#chdir "..";
system "mv $rdir/$doc $govdir/$doc";
 
 #says thanks
print <<END_of_HTML;
Content-type: text/html


<HTML>
<HEAD> 
<TITLE>Government Document Quality Control Check</TITLE>   
</HEAD>    
<body>
<img src=”INSERT YOUR OWN BANNER HERE” align="top" width ="404"</a>
<table width="50%">  
<tr><td>
<H2>Government Document Quality Control Check</H2>
<H3>Multivolume Page Confirmation Check</H3>
<P>
<table width = "50%">
<tr><td>
<font size=+1>Thank you for your assistance with document $doc.</font>
<p>
<p>
<form method="LINK" action="/INSERT SERVER PATH HERE/multivp.cgi">
<input type="submit" VALUE="CONTINUE TO NEXT DOCUMENT">
</P>
</BODY>
</HTML>
END_of_HTML

} # end sub write_out.
