#!/usr/local/bin/perl -w
#
# mkcontact
#
# This is a simple program to make a simple web page which
# shows all of the pictures.
#
# If there is already an index.html file quit unless the
# --force flag is used. Quit if the index.html file is
# more current than any image file. Note that if we quit
# because an index.html file is present we don't even make
# the thumbnails as I assume that someone is already working
# on that.
#
# Make a thumbnail for every photo which doesn't have a
# thumbnail, or the original photo is more current than its
# thumbnail.
#
# This uses webc (see http://www.ao.com/~regan/Webc/) to generate
# the web page. It is a simple web page processor.
#
#
# The home for this package is at:
# http://mordred.ao.com/mkcontact/
#
# Dave Regan
# 8 May 2000
# regan@ao.com
#
use strict;
use Getopt::Long;
use Image::Size;
use vars qw(%Args $ExitCode $Version);
%Args =
(
"force" => 0,
"limit" => 150,
"noindex" => 0,
"convert" => '/usr/X11R6/bin/convert', # Convert program
"template" => "/home/regan/public_html/mkcontact/mkcontact.wc",
);
$ExitCode = 0;
$Version = 'mkcontact v0.07 regan@ao.com';
###
### Main program
###
my($current, $dir);
GetOptions(\%Args, "force!", "noindex!", "limit=i", "convert=s",
"template=s");
if (defined($ARGV[0]))
{
# Save current directory
for $dir (@ARGV)
{
$dir =~ s#/$##;
ProcessDirectory($dir);
}
}
else
{
ProcessDirectory(".");
}
exit($ExitCode);
###
### ProcessDirectory
###
sub ProcessDirectory
{
my($dir) = @_;
my($data, $file, @files, $makeit, $thumbnail, $title);
# See if we have an index file
$makeit = 1;
if (-f "$dir/index.html")
{
return unless ($Args{'force'});
$makeit = 0;
}
#print "Processing $dir\n";
# Find the image files. If there is reason to make a thumbnail,
# then we need to make the index.html file.
if (!opendir(DIR, $dir))
{
print STDERR "Cannot open $dir: $!\n";
$ExitCode = 1;
return;
}
@files = sort(grep(!/\.[mt]\....$/, grep(/\.(gif|jpg)$/, readdir(DIR))));
closedir DIR;
$data = "";
for $file (@files)
{
($thumbnail = $file) =~ s/(.*)\.(.*)/$1.t.$2/;
$data .= qq|
|;
if ((! -f "$dir/$thumbnail") ||
(-M "$dir/$thumbnail" > -M "$dir/$file"))
{
$makeit = 1;
if (! -f "$dir/$thumbnail")
{
print "$dir/$thumbnail doesn't exist, build it\n";
}
elsif (-M "$dir/$thumbnail" > -M "$dir/$file")
{
print "$dir/$thumbnail is older than $dir/$file\n";
print "$dir/$thumbnail: ", -M "$dir/$thumbnail",
"$dir/$file: ", -M "$dir/$file", "\n";
system("ls -l $dir/$thumbnail $dir/$file");
}
CreateThumbnail("$dir/$file", "$dir/$thumbnail");
}
}
# If we need to create an index file, do so.
if ($makeit && !$Args{'noindex'})
{
if (!open(WEBC, ">$dir/index.wc"))
{
print STDERR "Cannot create $dir/index.wc: $!\n";
return;
}
($title = $dir) =~ s#.*/##;
$title =~ s#^(\d+)\.(.+)#$1 / $2#;
print WEBC "#define _TITLE_ $title\n";
print WEBC "#define _DATA_ $data\n";
if (open(TEMPLATE, "<$Args{'template'}"))
{
while ()
{
print WEBC $_;
}
close TEMPLATE;
}
close WEBC;
system("webc $dir/index.wc");
chmod(0644, "$dir/index.html");
}
}
###
### CreateThumbnail
###
### Create a thumbnail of the given image.
###
sub CreateThumbnail
{
my($orig, $thumb) = @_;
my($percent, $x, $y);
($x, $y) = imgsize($orig);
if (!defined($x))
{
print STDERR "Image size problems for $orig\n";
return;
}
$percent = int($Args{'limit'} * 100 / $x);
print "Creating $thumb\n";
system(qq|$Args{'convert'} -geomentry ${percent}%x${percent}% "$orig" "$thumb"|);
chmod(0644, $thumb);
}