#!/usr/bin/perl
#
# album.pl -- make photo albums on your webpage!
#
# Copyright (C) 2001, Robert Harlander. All rights reserved.
#
# Comments, bug reports, and suggestions to 
# robert.harlander@cern.ch
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#
#- {{{ RCS

# $Id: album.pl,v 1.13 2003/12/17 15:18:51 rharland Exp $
# $Log: album.pl,v $
# Revision 1.13  2003/12/17 15:18:51  rharland
# Minor changes.
#
# Revision 1.12  2003/12/17 15:18:01  rharland
# Minor changes.
# Help pages split.
# -c option added.
#
# Revision 1.11  2003/12/17 14:24:42  rharland
# www address changed.
#
# Revision 1.10  2003/11/28 21:43:25  rharland
# -cr option added.
#
# Revision 1.9  2003/11/20 16:58:37  rharland
# Left-Up-Right buttons included.
# Need to define $ICONNEXT, $ICONUP, $ICONPREVIOUS,
# otherwise icons will be replaced by text: "next", "index", "previous".
#
# Revision 1.8  2003/11/11 22:06:35  rharland
# html-files for pics are moved to a directory.
#
# Revision 1.7  2003/11/10 21:39:58  rharland
# Now clicking on large image gives image in ORIGINAL SIZE!
#
# Revision 1.6  2003/11/01 12:19:16  rharland
# further improvements.
#
# Revision 1.5  2003/10/31 20:03:05  rharland
# Improved frame version.
#
# Revision 1.4  2003/10/31 19:23:27  rharland
# With frames!
#
# Revision 1.3  2003/10/29 10:53:59  rharland
# Minor changes.
#
# Revision 1.2  2003/10/29 10:49:09  rharland
# Resize introduced.
# If titles.txt exists, only pictures appearing in titles.txt are resized.
#

#- }}}
$VERSION="0.8.0";

#- {{{ sub setdefaults:

sub setdefaults {
    #
    # default settings:
    #
#    @MYNAME = split(/:/,`ypcat passwd|grep \$USER`);
#    $MYNAME = $MYNAME[4];
    $MYNAME = "Robert Harlander";
    @FILETYPES = ("jpg","gif","tif","JPG");
    $VERTICAL = 0;        #  thumb-bar vertical(=1) or horizontal(=0)
    $THUMBPOS = "down";   #  where to put the thumb-bar: "up" or "down"
    $THUMBTITLE = 0;      #  include titles in the thumb-list?
    $ICONNEXT = "icons/rightx.gif";
    $ICONUP = "icons/upx.gif";
    $ICONPREVIOUS = "icons/leftx.gif";
    $COLUMNS = 5;         #  number of columns for index pages
    $SIZE = "600x600";    #  adjust size of pictures
    $WIDTH = "80";        #  size of thumbnails
    $HEIGHT = "100";      #  size of thumbnails
    $BGCOLOR="white";
    $TEXTCOLOR="black";
    $LINKCOLOR="blue";
    $VLINKCOLOR="darkblue";
    $ALINKCOLOR="red";
    $BORDER = 0;
    $TABLEBORDER=0;
    $TITLES = "titles.txt";
    $HEADER = "header.txt";
    $FOOTER = "footer.txt";
    $ALIGN = "center";
    $CONVERT = "convert";
    $HTMLDIR = "html";
    $THUMBDIR = "thumbs";
    $TITDIR = "captions";
    $PICORIG = "pics";
    $PICDIR = "pics.resize";
    if ($ENV{'TMPDIR'}) {
	$TMPDIR = $ENV{'TMPDIR'};
    } else {$TMPDIR = "/tmp"}
}

#- }}}
setdefaults();

$NoResize = argproc("noresize",0);
$Force = argproc("f(orce)?",0);
$Forcethumbs = argproc("ft",0);
$Forcepics = argproc("fp",0);
$Version = argproc("v(ersion)?",0);
$Help = argproc("h(elp)?",0);
$Custom = argproc("c(ustom)?",0);
$Cleanthumbs = argproc("ct",0);
$Cleanresize = argproc("cr",0);
($dum,$Albumtitle) = argproc("t(itle)?",1);

if ($Force) {$Forcethumbs = 1; $Forcepics = 1}
if ($Help) { printusage(); 
	     printbasic();
	     printoptions();
	     printtodo();
	     printacknowledge();
	     exit; }
if ($Custom) { printusage();
	       print("\nFor a list of options, see 'album.pl -h'.\n");
	       printcustom();
	       exit; }
if ($Version) { printversion(); exit; }
if ($Cleanthumbs) { cleanthumbs(); exit; }
if ($Cleanresize) { cleanresize(); exit; }
if ((!$Albumtitle) && (-e $TITLES)) {
    $Albumtitle = albumtitle($TITLES);
}
if (!$Albumtitle) {
    printusage();
    exit;
}
if ($NoResize) {$PICDIR = $PICORIG}


%errors = dochecks();
subalbum(".");

#- {{{ sub subalbum:

sub subalbum {
    #
    # subalbum($dir)
    #
    # create an album entry for all the pictures in $dir.
    #
    my(@files,$dir,$prevpic,$nextpic,%titles,$title);
    $dir = $_[0];
    #
    # find all pictures and their titles:
    #
    @files = getpics($dir);
    %titles = gettitles($dir,$TITLES,@files);
    @files = keys(%titles);
    #
    # generate pictures of uniform size:
    #
    unless ($NoResize) {
	unless (exists($errors{"convert"})) {
	    makepics($dir,@files);
	}
	else {
	    print("WARNING: 'convert' not found. No thumbnails generated.\n");
	}
    }
    #
    # generate the thumbnails:
    #
    unless (exists($errors{"convert"})) {
	makethumbs($dir,$THUMBDIR,@files);
    }
    else {
	print("WARNING: 'convert' not found. No thumbnails generated.\n");
    }
    #
    #
    writethumbs({%titles});
    overview({%titles});
    #
    #
    picpage({%titles});
    writeindex();
    header();
}

#- }}}
#- {{{ sub header:

sub header {
    #
    # header({%titles});
    #
    # uses the global variables: 
    # $HEADER, $FOOTER, $TITDIR, $PICORIG, $Albumtitle
    #
    open(PIC,">ahead.html");
    print {PIC} ("<base target=\"mainframe\">\n");
    print {PIC} ("<html>\n");
    print {PIC} ("<body>\n");
    print {PIC} ("<a href=\"./overview.html\">\n");
    print {PIC} ("<center>\n");
    print {PIC} ("<h2>$Albumtitle</h2></a>\n");
    print {PIC} ("</center>\n");
    print {PIC} ("</body>\n");
    print {PIC} ("</html>\n");
}

#- }}}
#- {{{ sub writeindex:

sub writeindex {
    #
    # writeindex();
    #
    # uses the global variables: 
    # $HEADER, $FOOTER, $TITDIR, $PICORIG, $Albumtitle
    #
    # start with the first picture:
    open(PIC,">index.html");
    print {PIC} ("<! INDEX -- generated by album.pl, v$VERSIOM>\n");
    print {PIC} ("<! on ",`date`," >\n");
    print {PIC} ("<html><head>\n");
    print {PIC} ("<title>$MYNAME: $Albumtitle</title>\n");
    print {PIC} ("</head>\n");
    if ($VERTICAL) {
	print {PIC} ("<frameset frameborder=1 rows=\"10%,90%\">\n");
	print {PIC} ("   <frame src=\"ahead.html\">\n");
	print {PIC} ("   <frameset frameborder=0 cols=\"18%,82%\">\n");
	print {PIC} ("    <frame name=\"thumbs\" src=\"thumbs.html\">\n");
	print {PIC} ("    <frame name=\"mainframe\" src=\"overview.html\">\n");
	print {PIC} ("   </frameset>\n");
	print {PIC} ("</frameset>\n");
    } else {
	if ($THUMBPOS eq "up") {
	    print {PIC} ("<frameset frameborder=1 rows=\"7%,23\%,70%\">\n");
	    print {PIC} ("   <frame src=\"ahead.html\">\n");
	    print {PIC} ("   <frame name=\"thumbs\" src=\"thumbs.html\">\n");
	    print {PIC} ("   <frame name=\"mainframe\"".
			 " src=\"overview.html\">\n");
	    print {PIC} ("   </frameset>\n");
	} else {
	    print {PIC} ("<frameset frameborder=1 rows=\"7%,73%,20%\">\n");
	    print {PIC} ("   <frame src=\"ahead.html\">\n");
	    print {PIC} ("   <frame name=\"mainframe\"".
			 " src=\"overview.html\">\n");
	    print {PIC} ("   <frame name=\"thumbs\" src=\"thumbs.html\">\n");
	    print {PIC} ("   </frameset>\n");
	}
    }
    print {PIC} ("</body>\n");
    print {PIC} ("</html>\n");
}


#- }}}
#- {{{ sub picpage:

sub picpage {
    #
    # picpage({%titles});
    #
    # uses the global variables: 
    # $HEADER, $FOOTER, $TITDIR, $PICORIG, $Albumtitle
    #
    my(%titles);
    %titles = %{$_[0]};
    mkdir("$HTMLDIR",0755);
    @piclist = sort(keys(%titles));
    foreach $i (0..$#piclist) {
	$pic = $piclist[$i];
	($picroot = $pic) =~ s/.[^.]*$//;
	@title = pictitle($pic,{%titles});
	$title = "@title";
	open(PIC,">$HTMLDIR/$picroot.html");
	print {PIC} ("<! PICPAGE -- generated by album.pl, v$VERSIOM>\n");
	print {PIC} ("<! on ",`date`," >\n");
	print {PIC} ("<html><head>\n");
	print {PIC} ("<title>$MYNAME: $title</title>");
	print {PIC} ("</head>\n");
	print {PIC} ("<body bgcolor=$BGCOLOR text=$TEXTCOLOR ");
	print {PIC} ("link=$LINKCOLOR vlink=$VLINKCOLOR alink=$ALINKCOLOR>\n");
	print {PIC} ("<center>\n");
	print {PIC} ("<h2>$title</h2>\n");
	if (-e "$PICORIG/$pic") {
	    print {PIC} ("<a href=\"../$PICORIG/$pic\">".
			 "<img src=../$PICDIR/$pic align=\"center\" ".
			 "border=\"0\"></a><br>\n");
	} else {
	    print {PIC} ("<img src=../$PICDIR/$pic align=\"center\"><br>\n");
	}	    
	if (-e "$TITDIR/$picroot.txt") {
	    print {PIC} ("<br>\n");
	    open(SUBTITLE,"$TITDIR/$picroot.txt");
	    while (<SUBTITLE>) {
		print {PIC} $_;
	    }
	    close(SUBTITLE);
	}
	print {PIC} ("<br><br>\n");
	if ($i > 0) {
	    ($previouspic = $piclist[$i-1]) =~ s/\.[^.]*//;
	    print {PIC} ("<a href=\"$previouspic.html\">");
	    if (-e "$ICONPREVIOUS") {
		print {PIC} ("<img src=\"../$ICONPREVIOUS\" border=0 alt=previous>");
	    }
	    else {
		print {PIC} ("[previous]");
	    }
	    print {PIC} ("</a>\n\&nbsp;\&nbsp;\&nbsp;\&nbsp;\n");
	}
	($previouspic = $piclist[$i-1]) =~ s/\.[^.]*//;
	print {PIC} ("<a href=\"../overview.html\">");
	if (-e "$ICONUP") {
	    print {PIC} ("<img src=\"../$ICONUP\" border=0 alt=up>");
	}
	else {
	    print {PIC} ("[up]");
	}
	print {PIC} ("</a>\n\&nbsp;\&nbsp;\&nbsp;\&nbsp;\n");
	if ($i < $#piclist) {
	    ($nextpic = $piclist[$i+1]) =~ s/\.[^.]*//;
	    print {PIC} ("<a href=\"$nextpic.html\">");
	    if (-f "$ICONNEXT") {
		print {PIC} ("<img src=\"../$ICONNEXT\" border=0 alt=next>");
	    }
	    else {
		print {PIC} ("[next]");
	    }
	    print {PIC} ("</a>\n\&nbsp;\&nbsp;\&nbsp;\&nbsp;\n");
	}
	print {PIC} ("<br>\n");
	print {PIC} ("</center>\n");
	print {PIC} ("<br><br>\n");
	print {PIC} ("</body></html>");
	close(PIC);
    }
}

#- }}}
#- {{{ sub makepics:

sub makepics {
#
# makepics($dir,@files)
#
# bring the pictures to a uniform size
# such that they fit in the browser
#
    my($dir,@files);
    $dir = shift;
    @files = @_;
    mkdir("$dir/$PICDIR",0755);
    foreach $pic (@files) {
	if(!(-e "$dir/$PICDIR/$pic") || $Forcepics) {
	    print("$PICORIG/$pic  -->  $PICDIR/$pic\n");
	    system("$CONVERT $dir/$PICORIG/$pic -resize ".
		   "$SIZE $dir/$PICDIR/$pic\n");
	}
    }
}

#- }}}
#- {{{ sub makethumbs:

sub makethumbs {
    my($dir,$subdir,@files);
    $dir = shift;
    $subdir = shift;
    @files = @_;
    mkdir("$dir/$subdir",0755);
    convimages("$dir/$subdir",$CONVERT,$WIDTH,$HEIGHT,@files);
}

#- }}}
#- {{{ sub getpics:

sub getpics {
    #
    # getpics($dir)
    #
    # move pictures found in $dir to $PICORIG
    # and - AFTER THAT - return a list with all the filenames 
    # of pictures found in $PICORIG
    #
    my($dir,@files);
    $dir = $_[0];
    unless (-e $PICORIG) {mkdir($PICORIG,0755)}
    foreach $ext (@FILETYPES) {
	system("mv $dir/*.$ext $dir/$PICORIG > album.log 2>&1");
    }
    opendir(DIR,$PICORIG);
    @files = sort(readdir(DIR));
    close(DIR);
    $" = "|";
    @files = grep(/\.(@FILETYPES)/,@files);
    $" = " ";
    return(@files);
}

#- }}}
#- {{{ sub writethumbs:

sub writethumbs {
    my(%info);
    %info = %{$_[0]};
    open(THUMBS,">thumbs.html");
    print {THUMBS} ("<html><head>\n");
    print {THUMBS} ("<title>$MYNAME: $Albumtitle.thumbs</title>");
    print {THUMBS} ("</head>\n");
    print {THUMBS} ("<base target=\"mainframe\">\n");
    print {THUMBS} ("<body bgcolor=$BGCOLOR text=$TEXTCOLOR ");
    print {THUMBS} ("link=$LINKCOLOR vlink=$VLINKCOLOR alink=$ALINKCOLOR>\n");
    print {THUMBS} ("<center>\n");
    print {THUMBS} ("<table cellspacing=10 frame=\"void\" border=1>\n");
    foreach $pic (sort {ordernum($a,{%info}) <=> ordernum($b,{%info}) }
		  (keys(%info))) {
	($picroot = $pic) =~ s/\.[^.]*$//;
	if ($VERTICAL) { print {THUMBS} ("<tr>\n") }
	print {THUMBS} ("<th valign=\"top\" align=\"$ALIGN\">");
	print {THUMBS} ("<a href=\"$HTMLDIR/$picroot.html\">");
	print {THUMBS} ("<img src=\"thumbs/thumb$pic\" border=$BORDER>\n");
	print {THUMBS} ("</a>\n");
	if ($THUMBTITLE) {
	    print {THUMBS} ("<br>\n");
	    @pictitle = pictitle($pic,{%info});
	    print {THUMBS} ("@pictitle\n");
	}
    }
    print {THUMBS} ("</table>\n");
    print {THUMBS} ("</center>\n");
    if (-e "$FOOTER") {
	open(FOOTER,"$FOOTER");
	while (<FOOTER>) {
	    print {THUMBS} $_;
	}
	close(FOOTER);
	print {THUMBS} ("<br>\n");
	print {THUMBS} ("<hr>\n");
    }
    print {THUMBS} ("</body></html>\n");
    close(THUMBS);
}

#- }}}
#- {{{ sub overview:

sub overview {
    my(%info);
    %info = %{$_[0]};
    open(INDEX,">overview.html");
    print {INDEX} ("<html><head>\n");
    print {INDEX} ("<title>$MYNAME: $Albumtitle</title>");
    print {INDEX} ("</head>\n");
    print {INDEX} ("<body bgcolor=$BGCOLOR text=$TEXTCOLOR ");
    print {INDEX} ("link=$LINKCOLOR vlink=$VLINKCOLOR alink=$ALINKCOLOR>\n");
    print {INDEX} ("<center>\n");
    print {INDEX} ("<table cellspacing=50 width=30\% border=$TABLEBORDER>\n");
    $count = 1;
    foreach $pic (sort {ordernum($a,{%info}) <=> ordernum($b,{%info}) }
		  (keys(%info))) {
	if ($count > $COLUMNS) {
	    print {INDEX} ("<tr>\n");
	    $count = 1;
	}
	$count++;
	($picroot = $pic) =~ s/\.[^.]*$//;
	print {INDEX} ("<th valign=\"top\" align=\"$ALIGN\">");
	print {INDEX} ("<a href=$HTMLDIR/$picroot.html>");
	print {INDEX} ("<img src=\"thumbs/thumb$pic\" border=$BORDER>\n");
	print {INDEX} ("</a>\n");
	print {INDEX} ("<br>\n");
	@pictitle = pictitle($pic,{%info});
	print {INDEX} ("@pictitle\n");
    }
    print {INDEX} ("</table>\n");
    print {INDEX} ("</center>\n");
    print {INDEX} ("<hr>\n");
    if (-e "$FOOTER") {
	open(FOOTER,"$FOOTER");
	while (<FOOTER>) {
	    print {INDEX} $_;
	}
	close(FOOTER);
	print {INDEX} ("<br>\n");
	print {INDEX} ("<hr>\n");
    }
    print {INDEX} ("<em>Created on ",`date`,"\n");
    print {INDEX} ("using\n");
    print {INDEX} ("<a href=\"http://www.robert-harlander.de/software\"\n",
	"target=\"\_top\">album.pl</a></em>, ");
    print {INDEX} ("&copy by ");
    print {INDEX} ("<a href=\"http://www.robert-harlander.de/\"\n",
	"target=\"\_top\">Robi</a>\n");
    print {INDEX} ("</body></html>\n");
    close(INDEX);
}

#- }}}
#- {{{ sub convimages:

sub convimages {
    my(@files,$convert,$width,$height,$newwidth,$newheight,@size);
    
    $dir = shift;
    $convert = shift;
    $width = shift;
    $height = shift;
    @files = @_;
    
    foreach $file (@files) {
	if(!(-e "$dir/thumb$file") || $Forcethumbs) {
	    @size = getsize($file);
	    if ( ($size[0] - $size[1]) > 0 ) {
		# Querformat:
		$newwidth = $size[0]*$height/$size[1];
		$geomopt = "-geometry $size[0]"."x"."$height";
		$cropopt = "-crop $width"."x"."$height"."+".
		    int(($newwidth-$width)/2)."+0";
	    }
	    else {
		# Hochformat:
		$newheight = $size[1]*$width/$size[0];
		$geomopt = "-geometry $width"."x"."$size[1]";
		$cropopt = "-crop $width"."x"."$height"."+".
		    "0+".int(($newheight-$height)/2);
	    }
	    print("generating thumbnail for $file...\n");
	    system("$convert $geomopt $cropopt $PICORIG/$file ".
		   "$dir/thumb$file\n");
	}
    }

}

#- }}}
#- {{{ sub getsize:

sub getsize {
    my($pic,$size);
    $pic = shift;
    $convout = `$CONVERT -verbose $PICORIG/$pic \"$TMPDIR/album.$pic\"`;
    unlink("$TMPDIR/album.$pic");
    @convout = split(/ /,$convout);
    ($size) = grep(/\d+x\d+/,@convout);
    @size = split(/[x+]/,$size);
    return(@size);
}

#- }}}
#- {{{ sub gettitles:

sub gettitles {
    #
    # gettitles($dir,$titles)
    #
    # looks for a titles-file in directory $dir and reads its entries
    # 
    # returns a hash of the form
    # picname1 => title1, picname2 => title2,...
    #
    # The titles file must have the form
    # <picname1> <title1>
    # <picname2> <title2>
    # ...
    # where <picname1> etc is the filename of the picture, and
    # <title1> etc is the picture title.
    #
    my(%info,$dir,$titles,@files);
    $dir = shift;
    $titles = shift;
    @files = @_;
    %info = ();
    if (-e "$dir/$titles") {
	open(TIT,"$dir/$titles");
	$count = -1;
	while (<TIT>) {
	    $count++;
	    if (/^ *$/ || /^TITLE /) {next};
	    chop;
	    @infotmp = split(/ /,$_);
	    if ($#infotmp == 0) {@infotmp = ($infotmp[0],$infotmp[0])}
	    $key = shift(@infotmp);
	    %info = (%info, $key => [($count,@infotmp)]);
	}
	close(TIT);
    }
    else {
	grep(%info = (%info, $files[$_] => [($_,$files[$_])]),(0..$#files));
    }
    #
    # remove titles of non-existing pictures 
    # (for example, old entries in the titles file)
    #
    grep(delete($info{$_}),diff([@files],[keys(%info)]));
    return(%info);
}

#- }}}
#- {{{ sub albumtitle:

sub albumtitle {
    my($titles,$title);
    $titles = $_[0];
    open(TIT,$titles);
    while (<TIT>) {
	if (m/^TITLE (.*)$/) {
	    $title = $1;
	}
    }
    close(TIT);
    return ($title);
}

#- }}}
#- {{{ sub cleanthumbs:

sub cleanthumbs {
    opendir(TDIR,$THUMBDIR);
    @thumbs = readdir(TDIR);
    shift(@thumbs);
    shift(@thumbs);
    close(TDIR);
    foreach $thumb (@thumbs) {
	($file = $thumb) =~ s/^thumb//;
	if (!(-e "$PICORIG/$file")) {
	    print("removing $THUMBDIR/$thumb\n");
	    unlink("$THUMBDIR/$thumb");
	}
    }
}

#- }}}
#- {{{ sub cleanresize:

sub cleanresize {
    opendir(TDIR,$PICDIR);
    @resize = readdir(TDIR);
    shift(@resize);
    shift(@resize);
    close(TDIR);
    foreach $pic (@resize) {
	if (!(-e "$PICORIG/$pic")) {
	    print("removing $PICDIR/$pic\n");
	    unlink("$PICDIR/$pic");
	}
    }
}

#- }}}
#- {{{ sub ordernum:

sub ordernum {
    my(%info,$key);
    $key = shift;
    %info = %{$_[0]};
    return(${$info{$key}}[0]);
}

#- }}}
#- {{{ sub pictitle:

sub pictitle {
    my(%info,$key,@dum);
    $key = shift;
    %info = %{$_[0]};
    @dum = @{$info{$key}};
    shift(@dum);
    return(@dum);
}

#- }}}
#- {{{ sub dochecks:

sub dochecks {
    my(%errors,$convert);
    chop($convert = `which convert`);
    unless (-x $convert) {%errors = ("convert" => 1)};
    return(%errors);
}

#- }}}
#- {{{ sub printusage:

sub printusage {
    print("\nUsage: album.pl [-t <title>] [-options]\n");
}

#- }}}
#- {{{ sub printbasic:

sub printbasic {
    print("\n");
    print("GETTING STARTED:\n");
    print("----------------\n");
    print("* Copy all your pictures to a separate directory.\n");
    print("* In that directory, type:  album.pl -t <title>\n");
    print("  where <title> is the name you want to give to your album.\n");
    print("* THAT'S ALL!\n\n");
    print("* Watch what's happening:\n");
    print("  - album.pl resizes all your pictures and moves them\n");
    print("    to a subdirectory 'pics.resize'.  The originals are kept\n");
    print("    in the subdirectory 'pics'.\n");
    print("  - thumbnails are created from all your pictures and copied\n");
    print("    to the subdirectory 'thumbs'.\n");
    print("  - an index.html file is generated in the main directory.\n");
    print("  - for every picture, an html-file is generated in the main\n");
    print("    directory.\n");
    print("* Look at the index.html with your web-browser,\n");
    print("  and click on the thumbnails.\n");
    print("\n");
    print("To learn how to customize your album, see 'album.pl -c'\n");
}

#- }}}
#- {{{ sub printcustom:

sub printcustom {
    print("\n");
    print("CUSTOMIZATION:\n");
    print("--------------\n");
    print("* GIVING TITLES TO YOUR PICTURES:\n");
    print("  Create a file called 'titles.txt' in the main directory\n");
    print("  which has the following content:\n");
    print("    <filename1> <pictitle1>\n");
    print("    <filename2> <pictitle2>\n");
    print("    ...\n");
    print("  For example, if you have a picture named 'pamela.jpg',\n");
    print("  you may want to have a line in your 'titles.txt' like this:\n");
    print("    pamela.jpg Pamela's new swimsuit.\n");
    print("  Note that your titles should be short to retain a nice\n");
    print("  format of your index.html.\n");
    print("  For longer descriptions of your pictures, see the\n");
    print("  DESCRIPTIONS item below.\n\n");
    print("  **IMPORTANT**: If titles.txt exists, and a picture is NOT\n");
    print("                 listed there, it will NOT be displayed!\n\n");
    print("  **HINT**: A good strategy is to first create a `raw'\n");
    print("            titles.txt by typing  `/bin/ls -1 > titles.txt',\n");
    print("            and then to modify the order and to add\n");
    print("            descriptions according to your wishes.\n\n");
    print("");
    print("");
    print("* GLOBAL TITLE:\n");
    print("  If you include a line in 'titles.txt' of the form:\n");
    print("    TITLE <albumtitle>\n");
    print("  then album.pl won't require the -t option (see above).\n");
    print("  On the other hand, giving the -t option overrides the\n");
    print("  TITLE entry in 'titles.txt'.\n\n");
    print("* DESCRIPTIONS:\n");
    print("  You can add more elaborate descriptions to your pictures\n");
    print("  by creating a file 'captions/<fileroot>.txt',\n");
    print("  where <fileroot> is the root name of the particular picture.\n");
    print("  For example, if you have a picture called pamela.jpg,\n");
    print("  then the contents of a file called captions/pamela.txt will\n");
    print("  become visible when clicking on the icon for pamela.jpg.\n\n");
    print("* HEADER AND FOOTER (temporarily disabled!):\n");
    print("  You can include a personal header and/or footer in your html\n");
    print("  pages by putting the corresponding html code to the files\n");
    print("  header.txt and footer.txt.\n\n");
    print("* NAVIGATION ICONS:\n");
    print("  album.pl lets you navigate through your pictures with\n");
    print("  [previous], [up], and [next] buttons. You can modify these\n");
    print("  by defining $ICONPREVIOUS, $ICONUP, and $ICONNEXT in\n");
    print("  'sub setdefaults'.\n\n");
    print("* SORTING:\n");
    print("  If you don't have a titles.txt file, the order in which\n");
    print("  the pictures appear is what you would get by typing\n");
    print("  `/bin/ls -1' in the pictures directory.\n");
    print("  If you DO have a titles.txt file, the order in which\n");
    print("  the pictures are listed there determines the order in which\n");
    print("  they are displayed.\n\n");
}

#- }}}
#- {{{ sub printoptions:

sub printoptions {
    print("\n");
    print("OPTIONS:\n");
    print("--------\n");
    print("-t <title> -- define global album title.\n");
    print("-fp -- force the resize of pics. Without this option,\n");
    print("       resized pics are only generated if they don't already\n");
    print("       exist. Size is controlled by global variable \$SIZE.\n");
    print("-ft -- force the generation of thumbnails. Without this option,\n");
    print("       thumbnails are only generated if they don't already\n");
    print("       exist.\n");
    print("-f  -- same as setting both -fp and -ft\n");
    print("-ct -- clean thumbs: delete unused thumbnails.\n");
    print("-cr -- clean resize: delete unused resized pictures.\n");
    print("-h  -- print these help pages.\n");
    print("-c  -- how to customize your album.\n");
    print("-v  -- print version number.\n\n");
    print("");
}

#- }}}
#- {{{ sub printtodo:

sub printtodo {
    print("\n");
    print("TODO:\n");
    print("  Well, this is Version $VERSION, so plenty of things have to\n");
    print("  be done. There is a lot of customization possible, and\n");
    print("  I think I have to clean things up a little, and make them\n");
    print("  more transparent (documentation!).\n\n");
}

#- }}}
#- {{{ sub printacknowledge:

sub printacknowledge {
    print("\n");
    print("ACKNOWLEDGMENTS:\n");
    print("  I got the idea for this from the `album' script by\n");
    print("  MarginalHacks (http://MarginalHacks.com/).\n");
    print("  However, when I tried this one out, it didn't really work\n");
    print("  (most probably my fault), and the result I got was kind of\n");
    print("  ugly. But the main reason for writing my own album.pl was\n");
    print("  the challenge...\n\n");
    print("  Further thanks go to:\n");
    print("    - Sabine Lammers for being the first user(!)\n");
    print("    - Zhen Xie for getting album.pl to run under cygwin,\n");
    print("      and many suggestions for improvement.\n\n");
}

#- }}}
#- {{{ sub printversion:

sub printversion {
    print("\n");
    print("album.pl, Version $VERSION.\n");
    print("Copyright March 2001 by R.V. Harlander.\n\n");
}

#- }}}
#- {{{ sub argproc:

sub argproc {
    #
    # get the options from the argument list
    #
    # Usage: argproc($optkey,$optnum)
    #
    #  $optkey is the name of the option (may be a pattern)
    #  $optnum is the number of arguments for the option
    #
    # Example: argproc("d(efault)?",1) together with the program call
    #          'program -d test1 test2'  returns the list (-d,test1).
    #          @ARGV is (test2) afterwards.
    #
    my($optkey,$optnum,$val,@optlist);
    $optkey = $_[0];
    $optnum = $_[1];
    
    foreach $val (0..$#ARGV) {
	if ($ARGV[$val] =~ m/^-$optkey$/) {
	    @optlist = splice(@ARGV,$val,$optnum+1);
	    last;
	}
    }
    return(@optlist);

}

#- }}}
#- {{{ sub diff:

sub diff {
#
# diff([@ARRAY1],[@ARRAY2]) returns the difference of @ARRAY1 and @ARRAY2
#
    my(@IN1,@IN2);
    my($i1,$i2,$j1,$j2,$k);
    @IN1 = @{$_[0]};
    @IN2 = @{$_[1]};

    $j1 = 0;
    foreach $i1 (0..$#IN1) {
      LDIFF: foreach $i2 (0..$#IN2) {
          if ("$IN1[$i1-$j1]" eq "$IN2[$i2]") {
              splice(@IN1,$i1-$j1++,1,());
              splice(@IN2,$i2,1,());
              last LDIFF;
          }
      }
    }
    return(@IN1,@IN2);
}

#- }}}





