#!/usr/bin/perl -w
#--------------------------------------------------------#
# This script makes an html page of all pictures         #
# found in the minicaptain directory tree, and           #
#   - makes a thumbnail,                                 #
#   - a clickable reference to the picture,              #
#   - references to web pages where the picture is used  #
#                                                        #
# August 2016 Hubert van Hecke                           #
#                                                        #
#--------------------------------------------------------#
use File::stat;
use Time::localtime;

open FILE, ">all_pics.html";        # output file, print HTML header etc:
print FILE ("
<html>           
<head><title>all minicaptain pictures</title><head>
<body bgcolor=white>
<br>
<center>
<font color=red size=\"+2\">MiniCaptain pictures</font>
<br><br>
<hr noshade size=1>
</center>
This page was produced by running 
<a href=\"all_pics.pl\">this Perl script</a>.
<br>
<hr noshade size=1>
<br><br>
<center>
<table border=1 cellpadding=0 cellspacing=0>
  <tr>
    <th> Thumbnail
    <th> Full path (starting at ...WWW/fvtx/)
    <th> Used in page:
");

$command = "rm *_thumb.jpg";                 # get rid of the old thumbnails
system($command);                            #
                                             # list of picture extensions:
@exts = ("jpg","JPG","jpeg","JPEG","png","gif","GIF");
$nexts = scalar(@exts);                      # array length
#$nexts = 1;                                  # for short test runs
for ($jex=0; $jex<$nexts; $jex++) {          # loop over icture types
  $nskip = 0;                                # counters for 
  $skip  = 0;                                # quick tests
                                             # grab al list of files:
  $command = "find ../ -name \"*.$exts[$jex]\" |";
  open GRAB, $command;                       # 
  while (<GRAB>) {                           # loop over all files of this type

    if ($skip==0) {                          # this is for bailing out early
      #if ($nskip>5) { $skip=1 }             # activate for test
      $fullname = $_;                        # full name, includes <CR>
      $fullname =~ s/\s+$//;                 # remove trailing spaces
      print (" SEEN: $fullname \n");
      if (index($fullname,"all_pics")<1  &&  # avoid self-reference
          index($fullname,"no_thumb")<1) {   # too many references to this jpg
        $nskip++;                            # bailout counter
        $thumb = $fullname;                  # make name for thumb
        $i = 1;                              # Skip forward to the last '/'
        while ($i) {                         #
          $i = index($thumb,"/") + 1;        # find /
          $thumb = substr($thumb,$i);        # remaining substring
        }                                    # end search last /
        $thumb_jpg = $thumb;
        $thumb = substr($thumb,0,-4);        # remove 3-letter extension
        $localname = $thumb."_thumb.jpg";    # add _thumb
        #print ("$fullname, $localname \n\n");
        $fullesc = $fullname;                # for use in 'command',
        $fullesc =~ s/ /\\ /g;               # escape spaces in the file name
        $localesc = $localname;              # same for localname
        $localesc =~ s/ /\\ /g;              # 
        
        $command = "convert ".$fullesc." -format jpg -thumbnail 100x100 ".$localesc;
        #print ("$command \n");
        system($command);                    # produce the thumbnail
                                             # add this picture to the table:
        print FILE ("
  <tr>
    <td> <img src=\"$localname\">
    <td> &nbsp; <a href=\"$fullname\">$fullname</a>
    <td> ");
                                             # search for files where this picture is used:
        $command = "find ../ -name \'*.html\' -exec grep -H \'$thumb_jpg\' {} \\; |";
        #print ("command: $command \n");
        $nlinks = 0;
        open USEDIN, $command;               # get the list of references
        while (<USEDIN>) {                   # loop over references
          $nlinks++;                         # count them
          $path = $_;
          $j = index($path,":");             # remove everything past the ":"
          $path = substr($path,0,$j);        #
          if (index($path,"all_pics")<0) {   # avoid self-reference
            #print ("PATH: $path \n");
            print FILE ("&nbsp;<a href=\"$path\">$path</a><br>");
          }                                  # exclude self-reference to all_pics
        }                                    # loop over links
        close USEDIN;                        # none found:
        if ($nlinks<=1) { print FILE ("&nbsp; (not used)"); }
        print FILE ("&nbsp;");               # 'not used' test fails for duplicate names
      }                          # no self-reference
    }                            # no skip (test only)
  }                              # while grab  - main loop over found files
  close GRAB;
  #print ("finished with GRAB\n");
}                                # loop over file types
                                 # finish the HTML code:
  print FILE ("
    </td>
  </tr>
</table>
</body>
</html>
");                              # 
  close FILE;                    # close html



















