#!/usr/bin/perl
use strict;
use File::Copy;
# Config variables.
my %cfgv;
my $config = ".autothumb";
my $thumbprefix = "_";
my $htaccess = ".htaccess";
my $gimp = "/usr/bin/gimp";
#my $gimp = "/usr/local/software/gimp1.2.5/bin/gimp";
$gimp .= " --batch-interpreter plug_in_script_fu_eval";
# Strings inserted in all generated files.
my $lock = "==This page was automatically generated by AutoThumb==";
my $lock1 = "==This index page was automatically generated by AutoThumb==";
my $version = "Autothumb perl v1.0 ";
# Globalish variables.
my $css = "";
my $body_opts = "";
my $state = "init";
my $cols = 0;
my $errors = "";
my $colred="[01;31m";
my $colgreen="[01;32m";
my $colnorm="[0m";
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Main program.
# The original autothumb accepted this usage, but it is no longer supported:
# Usage:
# autothumb [x-max-size y-max-size JPGquality columns] [JPG files]
my $file = $ENV{"HOME"} . "/" . $config;
&ConfigDefaults();
&ConfigRead($file);
&ConfigRead($config);
&ProcessDescriptions();
print "\n";
print "$colgreen" . "Page title:$colnorm \t$cfgv{\"TITLE\"}\n";
print "$colgreen" . "Page subtitle:$colnorm \t$cfgv{\"SUBTITLE\"}\n";
print "$colgreen" . "Index page:$colnorm \t$cfgv{\"INDEX\"}\n";
print "$colgreen" . "CSS file:$colnorm \t$cfgv{\"CSS_FILE\"}\n";
print "$colgreen" . "Uses subpages:$colnorm \t$cfgv{\"SUBPAGES\"}\n";
print "$colgreen" . "Subpage prefix:$colnorm \t$cfgv{\"SUBPAGE_PREFIX\"}\n";
print "$colgreen" . "Is an overview:$colnorm \t$cfgv{\"OVERVIEW\"}\n";
print "$colgreen" . "Description file:$colnorm $cfgv{\"DESCRIPTIONS\"}\n";
if ($cfgv{"CHARSET"})
{
print "$colgreen" . "Character set:$colnorm \t$cfgv{\"CHARSET\"}\n";
}
if ($cfgv{"EXIF_CMD"})
{
print "$colgreen" . "EXIF command:$colnorm \t$cfgv{\"EXIF_CMD\"}\n";
}
if ($errors)
{
&Error("\nThe following error(s) have occurred:\n$errors");
}
print "All done!\n";
exit;
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Returns 1 if the fie was generated by autothumb, 0 otherwise.
# If the file does not exist, then 1 is returned.
sub CheckGeneratedFile($)
{
if (! -e $_[0])
{
return 1;
}
if (!open(HTMLIN, "<$_[0]"))
{
&Error("The file $_[0] cannot be read! Not overwriting!\n");
return 0;
}
my $buffy;
while ($buffy = )
{
chop $buffy;
if ($buffy =~ /$lock/)
{
close(HTMLIN);
return 1;
}
if ($buffy =~ /$lock1/)
{
close(HTMLIN);
return 1;
}
}
close(HTMLIN);
&Error("The file $_[0] was not generated by autothumb! " .
"Not overwriting!\n");
return 0;
}
#------------------------------------------------------------------------------
sub OpenHTML($)
{
my $file = "/dev/null";
local *FH;
if (&CheckGeneratedFile($_[0]))
{
$file = $_[0];
}
open(FH, ">$file");
# Print a header in each file to mark it as generated.
print FH <<"EOF";
EOF
# Return the filehandle.
return *FH;
}
#------------------------------------------------------------------------------
sub CloseHTML($)
{
close($_[0]);
}
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
sub PrintIndexHead($)
{
my $fh = shift;
my $charset = "iso-8859-1";
if ($cfgv{"CHARSET"})
{
$charset = $cfgv{"CHARSET"};
}
# Print HTML header.
print $fh <<"EOF";
$css
$cfgv{"TITLE"}
EOF
# Determine what titles are defined, and output appropriate lines.
my $h1a = '
\n";
}
}
#------------------------------------------------------------------------------
sub PrintIndexEnd($)
{
my $fh = shift;
print $fh <<"EOF";
EOF
}
#------------------------------------------------------------------------------
# PrintData($fh, $link, $image, $thumb, $comment)
sub PrintData($$$$$)
{
my $fh = shift;
my $link = shift;
my $image = shift;
my $thumb = shift;
my $comment = shift;
# Strip all html tags from the caption and use it for the alt text.
# If no comment is available, use the image filename as the alt text.
my $alt_text = $comment;
$alt_text =~ s/<[^>]*>//g;
if (!$alt_text)
{
$alt_text = $image;
}
# Use ImageMagick to determine the size of the thumbnail.
my $width = &System("identify -format \"%w\" $thumb");
my $height = &System("identify -format \"%h\" $thumb");
chop $width;
chop $height;
print $fh <<"EOF";
$comment
EOF
}
#------------------------------------------------------------------------------
sub PrintTableStart($)
{
my $fh = shift;
if (($state eq "table") || ($state eq "tr"))
{
return;
}
my $cs = $cfgv{"CELLSPACING"};
my $cp = $cfgv{"CELLPADDING"};
# Print HTML header.
print $fh <<"EOF";
EOF
# Update state.
$state = "table";
$cols = 0;
}
#------------------------------------------------------------------------------
# PrintTableData($fh, $link, $image, $thumb, $comment)
sub PrintTableData($$$$$)
{
my $fh = $_[0];
my $maxw = $cfgv{"WIDTH"} + 10;
&PrintTableStart($fh);
print $fh "\n";
# Start a new row in the index file if necessary.
if (($state ne "tr") && ($cols == 0))
{
print $fh "
\n";
$state = "tr";
}
print $fh "
\n";
&PrintData(@_);
print $fh "
\n";
# End table row if necessary.
$cols++;
if ($cols == $cfgv{"HTMLCOLS"})
{
print $fh "
\n";
$state = "table";
$cols = 0;
}
}
#------------------------------------------------------------------------------
sub PrintTableEnd($)
{
my $fh = shift;
if ($state eq "tr")
{
print $fh " \n";
$state = "table";
}
if ($state eq "table")
{
print $fh "
\n";
print $fh "
\n\n";
$state = "none";
}
}
#------------------------------------------------------------------------------
# $prev, $curr, $next, $image, $comment
sub PrintSubpage()
{
my $prev = shift;
my $curr = shift;
my $next = shift;
my $image = shift;
my $comment = shift;
# If no current subpage, then return.
if (!$curr)
{
return;
}
my $delay;
if ($cfgv{"DELAY"})
{
$delay = ' ';
$prev_text .= $cfgv{"PREV_TEXT"};
$prev_text .= ' ';
}
my $next_text;
if ($next)
{
$next_text = ' ';
$next_text .= $cfgv{"NEXT_TEXT"};
$next_text .= '';
}
my $fh = &OpenHTML($curr);
my $charset = "iso-8859-1";
if ($cfgv{"CHARSET"})
{
$charset = $cfgv{"CHARSET"};
}
my $exif = "";
if ($cfgv{"EXIF_CMD"})
{
my $fmto = "";
my $fmtc = "";
if ($cfgv{"EXIF_FORMAT"})
{
$fmto = $cfgv{"EXIF_FORMAT"};
my @strs = split "<", $fmto;
my $i;
for($i=@strs-1; $i>0; $i--)
{
my $str = $strs[$i];
$str =~ s/ .*/>/;
$str = "$str";
$str =~ s###i;
$fmtc .= $str;
}
$fmto .= "\n";
$fmtc .= "\n";
}
my $command = $cfgv{"EXIF_CMD"};
$command =~ s//$image/g;
$exif = &System($command);
chop $exif;
$exif = "\n$fmto$exif$fmtc";
}
# Print subpage.
print $fh <<"EOF";
$delay$css
$image