#!/usr/bin/perl
# pix2fotohtml.pl -- 
# J C Nash (C) 2018
# 
use strict;
use Cwd; # ??
use Cwd 'realpath'; # ?? 
use File::Basename; # module that parses filenames into path, name, extension etc.
use File::Copy;
# use File::HomeDir; # Need to install libfile-homedir-perl

my ($ii, $jj, $ofn, $tmp, $oldpn);
my ($outpath, $fextn, $fnroot);
my ($tfnam, $numfiles, $pname);
my (@imfiles, @persname, $d8, @ymd, @tfiles);
my ($trln, $titl);

print "pix2fotohtml.pl -- (C) J C Nash 2018\n";
print "\n";
my $numclargs = @ARGV; # get the number of arguments on the command line
if ($numclargs==0) { #if no command line parameters, then display a message about the program
   print "This program was written by John C. Nash\n";
   print "     18 Spyglass Ridge, Stittsville, Ontario, K2S 1R6\n";
   print "       nashjc\@uottawa.ca\n";
   print "\n";
   print "Usage:\n";
   print "     pix2fotohtml.pl outfile\n";
   print "??tries to put captions in myfile into the image files that are present\n";
   print "  in the current directory. Creates a report.txt on success in so doing\n";
   print "\n";
   print "outfile name =";
   $ofn = <STDIN>;
} else { 
   $ofn = $ARGV[0]; # filename root is first parameter
}
chomp $ofn;
print "working with output filename = $ofn\n";
$ofn=realpath($ofn); # expand to full path 
($fnroot,$outpath,$fextn) = fileparse($ofn);

print "full output filename = $ofn\n";
# print "filename root = $fnroot\n";
#print "filename extension is $fextn\n";
print "path is $outpath\n";

# Now get file names of image files -- jpg only (lower case only)!
print "Image files\n";
@imfiles=();
@tfiles=glob("*.jpg");
$numfiles=@tfiles;
print "$numfiles of type .jpg\n";
@imfiles=(@imfiles,@tfiles);
$numfiles=@imfiles;
print "$numfiles total jpg image files\n";	

# sort the files

@tfiles = sort @imfiles;
@imfiles = @tfiles; 
 
# loop over files


# open file for output
open (OFILE, ">>", $ofn);
print "File $ofn is open\n";

if (-f "\@TITLE\@.TXT") { # there is a title -- use it
   open (TFILE, "\@TITLE\@.TXT");
   $titl = <TFILE>;
   print "Title: $titl\n";
   close TFILE;
 } else {
   print "no title";
   $titl = "   ";
 }

print OFILE "$titl\n\n";     

foreach $tfnam (@imfiles) {
      print OFILE "$tfnam ";
      $trln = `rdjpgcom $tfnam`;
      $trln = trim($trln); # remove white space
      $trln =~ s/\\000//g; # get rid of \000 in comments
      if (length($trln) < 1) {
          $trln = "<<No Caption>>";
      }
      $trln =~ tr/\000-\037/\n~~~/;    # best option for speed
      print OFILE "$trln\n";
      print OFILE "\n";
}     

#  $tmp = <STDIN>;

# copy file end to outfile
close OFILE;

die("Done");
#===============================================
# Perl trim function to remove whitespace from the start and end of the string
sub trim {
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
# Left trim function to remove leading whitespace
sub ltrim {
	my $string = shift;
	$string =~ s/^\s+//;
	return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim {
	my $string = shift;
	$string =~ s/\s+$//;
	return $string;
}
#===============================================


