#!/usr/bin/perl
use File::Basename; 
use File::Copy; 
# NEW version for single directory storehouse of all TVZ files that can be made into thumbnails;
#  now include .txt .djvu .pdf converted to .png; make jpg thumbnails of all png gif and jpg files
chdir "./clncombined";
#chdir "./temp";
my $jpqual_thmb=68; my $jpqual_int=84;                   # set jpg quality values
my $width_thmb=260; my $width_int=800;                   # set image widths
my $count = 0;
my $pfile = "init.pdf";
my $tfile = "init.pdf";
my $pngfile = "init.png";
my @types = qw(png gif jpg);
#my @types = qw(png);

foreach my $typ(@types) {

unlink "./*.t.$typ";                                              # clean up existing files
unlink "./*.html";                                              # clean up existing files
#print "<HTML><HEAD><TITLE>$1</TITLE></HEAD><BODY><A HREF=../>BACK</A> &nbsp;&nbsp;<FONT SIZE=+2>test</FONT> - click on photo for larger version<BR><TABLE><TR>\n" >"index.html";
#for file in `ls *.jpeg`; do (( count+=1 ))                    # jpgs->thumb and med images
opendir(GDIR, "./") || die("cannot open current dir");
foreach $gfile (readdir(GDIR)) {
# want to check if same file pdf or txt exists; if so delete the png version after we make the thumb file
  if ($gfile =~ /\.png/) {
    $pfile = $gfile;
    $tfile = $gfile;
    $pngfile = $gfile;
    $pfile =~ s/\.png/\.pdf/;
    $tfile =~ s/\.png/\.txt/;
  }
  next unless $gfile =~ /.*$typ$/;
  next if $gfile =~ /\.t\.$typ$/;
  #next unless $gfile =~ /1969/;
  $count++;
  my $name = basename("$gfile",".$typ"); 
  print "                 working: ./$gfile > $name.t.$typ\n";
 if ($typ =~ /jpg/) { 
  open (THUMBS, "| cat $gfile | /usr/bin/djpeg | /usr/bin/pamscale -xsize $width_thmb | /usr/bin/cjpeg -smooth 10 -qual $jpqual_thmb -prog > $name.t.$typ") || die "cannot open fifo: $!";
  }
 elsif ($typ =~ /png/) { 
    if (-e $pfile && -e $gfile) {
      print "  found both $pfile and $gfile\n";
     open (THUMBS, "| cat $gfile | /usr/bin/pngtopnm | /usr/bin/pamscale -xsize $width_thmb | /usr/bin/cjpeg -smooth 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!";
      sleep(0.2);
    }
    elsif (-e $tfile && -e $gfile) {
      print "  found both $tfile and $gfile\n";
      open (THUMBS, "| cat $gfile | /usr/bin/pngtopnm | /usr/bin/pamscale -xsize $width_thmb | /usr/bin/cjpeg -smooth 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!"; 
    }
    elsif (-e $gfile) {
      open (THUMBS, "| cat $gfile | /usr/bin/pngtopnm | /usr/bin/pamscale -xsize $width_thmb | /usr/bin/cjpeg -smooth 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!";
    }
  }
  else {  # lonely gif file 
  open (THUMBS, "| cat $gfile | /usr/bin/giftopnm | /usr/bin/pamscale -xsize $width_thmb | /usr/bin/cjpeg -smooth 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!";
  }
  print THUMBS "stuff\n";
  close THUMBS;
  sleep 0.3;
    if (-e $pfile && -e $pngfile) {
      my $done = `rm $pngfile`;
      print "  $done deleting:$pngfile pdf version exists\n";
    }
    elsif (-e $tfile && -e $pngfile) {
      my $done = `rm $pngfile`;
      print "  $done deleting:$pngfile pdf version exists\n";
    }
  }
}
