#!/usr/pkg/bin/perl
# ====================================================================
# populate_master
#
# Copyright (c) 2000 David Burren.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The end-user documentation included with the redistribution,
#    if any, must include the following acknowledgment:
#       "This product includes software developed by David Burren."
#    Alternately, this acknowledgment may appear in the software itself,
#    if and wherever such third-party acknowledgments normally appear.
#
# 4. The name "David Burren" must not be used to endorse or promote
#    products derived from this software without prior written
#    permission. For written permission, please contact david@burren.cx.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.  IN NO EVENT SHALL DAVID BURREN BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================
# $Id: populate_master,v 1.7 2000/09/05 02:14:35 davidb Exp $
#
# Make TIFF files in master/ from all the files in original/
#
# If run a second time, only new files are processed.
# If an original/ file is newer than its partner in master/,
# don't blindly overwrite it - complain.
#
# If the argument "-gz" is passed, ".tiff.gz" files will be produced.

@tours = (
	{
		"active"	=> 0,
		"name"		=> "VGA",
		"dir"		=> "VGA.tur",
		"vert_size"	=> 300,
		"description"	=> "images sized for 640x480 browsers"
	},
	{
		"active"	=> 1,
		"name"		=> "SVGA",
		"dir"		=> ".",
		"vert_size"	=> 405,
		"description"	=> "images sized for 800x600 browsers"
	},
	{
		"active"	=> 1,
		"name"		=> "XGA",
		"dir"		=> "XGA.tur",
		"vert_size"	=> 580,
		"description"	=> "images sized for 1024x768 browsers"
	},
	{
		"active"	=> 0,
		"name"		=> "SXGA",
		"dir"		=> "SXGA.tur",
		"vert_size"	=> 840,
		"description"	=> "images sized for 1280x1024 browsers"
	},
	{
		"active"	=> 1,
		"name"		=> "P815",
		"dir"		=> "P815.tur",
		"vert_size"	=> 976,
		"description"	=> "images sized for 1600x1200 browsers"
	},
	{
		"active"	=> 0,
		"name"		=> "Best",
		"dir"		=> "Best.tur",
		"vert_size"	=> 0,
		"description"	=> "best image sizes available"
	}
);

mkdir "master", 0777 if (! -d "master/.");
mkdir "config", 0777 if (! -d "config/.");

$gzip = ($ARGV[0] =~ /gz/);

if (defined $ENV{"IM_CACHE"}) {
	$im_cache = $ENV{"IM_CACHE"};
	print "Limiting ImageMagick RAM cache to $im_cache MB\n";
} else {
	$im_cache = 80; # ImageMagick default
}

# Create or append config/setup, based on the contents of master/ and original/.
# Does not verify the existing contents of config/setup.

%master = %no_master = @notes = ();
if (-r "config/setup") {
	$images_file = "config/setup";
} elsif (-r "config/notes") {
	# Deal with old versions of the file
	$images_file = "config/notes";
} else {
	$images_file = "config/setup";
}
if (open(NOTES, "<$images_file")) {
	while (<NOTES>) {
		push(@notes, $_);
		s/^#.*$//;
		if (/^(\S+)/) {
			my $file = $1;

			next if ($file =~ /^Section/i);
			next if ($file =~ /^Title/i);
			if ($file =~ /^(.+),(.+)$/) {
				if ($1 ne $2) {
					$no_master{$1} = $no_master{$2} = 1;
				} else {
					$master{$1} = 1;
				}
			} else {
				$master{$file} = 1;
			}
		}
	}
	close(NOTES);
}
foreach $name (keys %master) {
	if ($no_master{$name}) {
		delete $no_master{$name};
	}
}
foreach $file (<original/*>) {
	next if ($file =~ /\.txt$/);
	next if ($file =~ /\.exif$/);
	next if ($file =~ /\.crw$/);
	next if ($file =~ /\.thm$/);
	($name = $file) =~ s/^original\///;
	$name =~ s/\..*$//;
	next if ($no_master{$name});
	$new = "master/$name.tiff";
	$other = $new.".gz";
	if ($gzip) {
		$other = $new;
		$new = $new.".gz";
	}
	if (-f $other) {
		if (-M $file < -M $other) {
			print "$file newer than $other - possible problem!\n";
		}
	} elsif (! -f $new) {
		print "making $new\n";
		if (system("convert", "-cache", $im_cache,
				"+compress", $file, $new) != 0) {
			my $err = $?;
			unlink $new;
			die "convert $file $new: $err";
		}
	} elsif (-M $file < -M $new) {
		print "$file newer than $new - possible problem!\n";
	}
}

if (! -f "config/tours") {
	if (open(TOURS, ">config/tours")) {
		print TOURS "#name	dir		vert limit\n";
		foreach $r (@tours) {
			printf TOURS "%-11s %-11s %-8s %s\n",
				($r->{"active"} ? "" : "#").$r->{"name"},
				$r->{"dir"},
				$r->{"vert_size"},
				$r->{"description"};
		}
		close(TOURS);
	}
}

$updated = 0;
foreach $file (<master/*>) {
	($name = $file) =~ s/^master\///;
	$name =~ s/\..*$//;
	next if ($no_master{$name});
	if (!$master{$name}) {
		$master{$name} = 1;
		if (!$updated) {
			push(@notes, "# new tags\n");
		}
		$updated = 1;
		push(@notes, "$name\t\n");
	}
}
if ($updated && open(NOTES, ">$images_file")) {
	print NOTES @notes;
	close(NOTES);
}
