#!/usr/bin/perl -w
# This script makes the drawing index html file, and thumbnails.
# August 2011 Hubert van Hecke
# 
use File::stat;
use Time::localtime;

open FILE, ">index.html";        # output file, print HTML header etc:
print FILE ("
<html>           
<head><title>PHENIX FVTX engineering drawings</title><head>
<body bgcolor=white>
<br>
<center>
<font color=red size=\"+2\">FVTX engineering drawings</font>
<br><br>
<hr noshade size=1>
</center>
This page was produced by running 
<a href=\"make_index.pl\">this Perl script</a>.
<br>
Full file names are \'111-PHX-xx-yyyy (description).pdf\' (or .PDF), where xx = 01 or 02, and
yyyy is the drawing number.
<hr noshade size=1>
<br><br>
<center>
<table border=0 cellpadding=0 cellspacing=0>
  <tr>
    <th> Drawing<br>number
    <th> Description
    <th> Thumbnail <br> image
    <th> pdf file date
  <tr><td colspan=4><hr noshade size=1>
");
$skip = 0;                                 # grab al list of pdf files:
  open GRAB, 'ls -1 *.PDF *.pdf |';        # pdf and PDF, one per line
  while (<GRAB>) {                         # loop over all files
    if ($skip==0) {                        # this is for bailing out early
      $bigpdf = $_;                        # full name, includes <CR>
      $name = "&nbsp;".substr($bigpdf,26,-6)."&nbsp;";   # description
      $paren = index($bigpdf,"(");         # drawing number is between char 11 
      $number = substr($bigpdf,11,$paren-11);   #          and the first paren
      $number =~ s/\s+$//;                 # remove trailing spaces
      $thumb = $number.".jpg";             # thumbnail file names are 1234.jpg
      $thumb =~ s/ /\\ /;                  # escape spaces in the thumb name
      chop($bigpdf);                       # remove <CR>
      $date = ctime(stat($bigpdf)->mtime); # get file date
      $date = "&nbsp;".substr($date,3);    # strip weekday
      $bigpdf_esc = $bigpdf;               # we need a copy for 'command'
      $bigpdf_esc  =~ s/ /\\ /g;           # escape the spaces and parens
      $bigpdf_esc  =~ s/\(/\\(/g;          # in the file name
      $bigpdf_esc  =~ s/\)/\\)/g;          #
      $command = "convert ".$bigpdf_esc." -crop 300x300+1800+300 -resize 100x100 -border 2 ".$thumb ;
      print "$command \n";
      system($command);                    # make the thumbnail
      $thumb0 = $thumb;                    # Some pdf's have multiple pages
      $thumb0 =~ s/.jpg/-0.jpg/;           # named xxx-0, xxx-1 etc.
      if (-e $thumb0) {                    # Only use the first one
        $command="mv $thumb0 $thumb";      # and rename it
        system($command);                  #
      }                                    #
      if ( -s $thumb <620) {               # small thumbs turn out to be
        $thumb = "no_thumb.jpg";           # blanks (because of convert errors)
      }                                    #
                                           # ready to write a HTML table entry:
     print FILE ("
  <tr>
    <td>  <a href=\"$bigpdf\">$number</a> 
    <td> $name \n
    <td> <img border=0 src=\"$thumb\">
    <td> $date
");
     #if ( index($number,"5207")>=0 ){ $skip = 1; }

    }  # no skip
  }    # while grab  - main loop

  close GRAB;
  print FILE ("
</table>
</body>
</html>

");
  $command = "rm *-0.jpg *-1.jpg *-3.jpg";   # keep the -2's
  system($command);
  print ("finished with GRAB\n");
  close FILE;



















