#!/usr/bin/perl
use File::Basename; 
use File::Copy; 
# Q&D webphotoalbum:conv dir of jpgs & movs to thumbnail index page, med size
#cd ${1:?"sorry we need a directory name: $0 dirname" }; # provide help if no dir
#chdir $ARGV[1]; # provide help if no dir
#chdir "./gigs";
#opendir(GDIR, "./") || die("cannot open current dir");
#foreach $gfile (readdir(GDIR)) {
#  next unless $gfile =~ /.*\.jpg$/;
#  my $clnfile = $gfile;
#  $clnfile =~ s/\s+/_/g;
#  $clnfile =~ s/\s/_/g;
#  $clnfile =~ s/@/at/g;
#  $clnfile =~ s/&/and/g;
#  $clnfile =~ s/\#/_/g;
#  copy("./$gfile","../clngigs/$clnfile");
#}
#exit;
#chdir "./clnpost";
#chdir "./clnpics";
#chdir "./clnoth";
#chdir "./clngigs";
#chdir "./clnarts";
chdir "./clnset";
#$ARGV[1]; # provide help if no dir
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 $typ = "png";
#my $typ = "jpg";
#my $typ = "gif";
#rm *.?.jpeg                                             # clean up existing files
#rm *.?.JPG                                              # clean up existing files
#unlink "./*.i.jpg";                                              # clean up existing files
#unlink "./*.t.jpg";                                              # clean up existing files
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)) {
  #next unless $gfile =~ /.*jpg$/;
  #next if $gfile =~ /\.t\.jpg$/;
  next unless $gfile =~ /.*$typ$/;
  next if $gfile =~ /\.t\.$typ$/;
  $count++;
  #for file in `ls ./*.jpg`; do (( count+=1 ))
  # jpgs->thumb and med images
  my $name = basename("$gfile",".$typ"); 
  print "                 working: ./$gfile > $name.t.jpg\n";
  #print "working::::$name::::\n";       # get working filename
   #`djpeg "./$gfile" | pnmscale -xsize $width_thmb | cjpeg -smoo 10 -qual $jpqual_thmb -prog` >"./$name.t.jpg";  # make a thumbnail
  #system("/usr/bin/djpeg $gfile | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > $name.t.jpg");  # make a thumbnail
  #@args= ("/usr/bin/djpeg","\"./$gfile\"", "|", "/usr/bin/pnmscale", "-xsize", "$width_thmb", "|", "/usr/bin/cjpeg", "-smoo", "10", "-qual", "$jpqual_thmb", "-prog", ">", "\"./$name.t.jpg\"");  # make a thumbnail
  #open THUMBS, "/usr/bin/djpeg ./$gfile | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > ./$name.t.jpg" or die "nope no thumbs:$!";  # make a thumbnail
  #open THUMBS, "/usr/bin/djpeg ./$gfile > ./$name.pnm";
 if ($typ =~ /jpg/) { 
  open (THUMBS, "| cat $gfile | /usr/bin/djpeg | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > $name.t.$typ") || die "cannot open fifo: $!";
  }
 elsif ($typ =~ /png/) { 
  open (THUMBS, "| cat $gfile | /usr/bin/pngtopnm | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!";
  }
  else { 
  open (THUMBS, "| cat $gfile | /usr/bin/giftopnm | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > $name.t.jpg") || die "cannot open fifo: $!";
  }
# | /usr/bin/pnmscale -xsize $width_thmb | /usr/bin/cjpeg -smoo 10 -qual $jpqual_thmb -prog > ./$name.t.jpg" or die "nope no thumbs:$!";  # make a thumbnail
  print THUMBS "stuff\n";
  close THUMBS;
  sleep 1;
  #system(@args) == 0 or warn "system @args failed: $?";
  #$shell = '/bin/sh';
  #`echo "@args"`
  #exec $shell @args ;
# == 0 or warn "system @args failed: $?";
   #`djpeg "./$gfile" | pnmscale -xsize $width_int | cjpeg -smoo 10 -qual $jpqual_int -prog` >"./$name.i.jpg";    # make a med size image
   print "<HTML><BODY><A HREF=\"index.html\">BACK</A> - click on photo for original full size version<BR><A HREF=./$gfile>\n" > "./$name.html";  # compose med image page
   #print "<IMG SRC=\"./$name.i.jpg\" ALT=\"./$gfile\"></A><BR>$count \"./$gfile\"</BODY></HTML>\n" >> "./$name.html";  # med image cont.
   print "<TD><A HREF=\"./$name.html\"><IMG SRC=\"./$name.t.jpg\" ALT=\"./$gfile\"></A>\n" >> "index.html";   # add item to HTML index page
   print "<BR>$count \"./$name\"</TD>\n" >> "index.html";             # HTML index page cont.
   if ( $count % 4 < 1 ) {
           print "</TR><TR>\n" >> "index.html";   # add new row
   }
}
print "</TR></TABLE></BODY></HTML>\n" >> "index.html";  # finish off index page
