#!/usr/bin/perl

# metapixel-prepare --- prepare images for metapixeling.

# Copyright (C) 1999-2004 Mark Probst
# Copyright (C) 2004 Jake Di Toro
# Copyright (C) 2006 Stefan Soeffing

# Maintainer: Mark Probst <schani@complang.tuwien.ac.at>

# 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 this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.

use strict;

use Getopt::Long;
use File::Basename;
use IO::Handle;

sub disambiguate_filename {
    my $filename = shift;
    my $suffix = shift;

    return "$filename$suffix" if !-e "$filename$suffix";

    my $ctr = 1;

    while (-e "$filename.$ctr$suffix") {
	++$ctr;
    }

    return "$filename.$ctr$suffix";
}

sub usage {
    print STDERR "Usage: $0 [OPTION]... <srcdir> <destdir>

Prepares all images in <srcdir> for use as small images in
photomosaics. The scaled versions and the table file are
stored in <destdir>.

  --help             display this help and exit
  --width=WIDTH      specify width of small images
  --height=HEIGHT    specify height of small images
  -r, --recurse      recurse through directories
  --debug            print out debugging info
";
    exit(1);
}

my ($width, $height, $destdir) = split /\s+/, `metapixel --print-prepare-settings`;

my $do_recurse;
my $DEBUG;

if (!GetOptions("help", \&usage,
		"width=i", \$width,
		"height=i", \$height,
		"recurse|r", \$do_recurse,
		"debug", \$DEBUG)) {
    usage();
}

if (!$width || $width <= 0 || !$height || $height <= 0) {
    print "$0: Width and height of the prepared images must be specified.\n";
    exit(1);
}

my $opts = "--width=$width --height=$height";

if ($#ARGV != 0 && $#ARGV != 1) {
    usage();
}

my $srcdir = $ARGV[0];

$destdir = $ARGV[1] if $#ARGV > 0;

if (! -d $srcdir || ! -r $srcdir) {
    print "$0: Source directory $srcdir does not exist or is unreadable.\n";
    exit(1);
}

unless ($destdir) {
    print "$0: A destination directory must be specified.\n";
    exit(1);
}
if (! -d $destdir) {
    print "$0: Destination directory $destdir does not exist.\n";
    exit(1);
}

STDOUT->autoflush(1);

sub process_dir {
    my $pdir = shift;
    my $do_recurse = shift;

    print "Processing dir: $pdir\n" if $DEBUG;

    if (opendir DIR, $pdir)
    {
	my @files = grep !/^\.\.?$/, readdir DIR;

	closedir DIR;

	foreach my $filename (@files) {
	    my $fullname = "$pdir/$filename";

	    print "Testing file: $fullname\n" if $DEBUG;

	    while (-l $fullname) {
		print "Following symlink: $fullname\n" if $DEBUG;
		$fullname = readlink($fullname);
	    }

	    if (-f $fullname && -r $fullname) {
		my ($name, $path, $suffix) = fileparse($fullname);

		print "Processing: $fullname\n" if $DEBUG;

		my $thumbname = disambiguate_filename("$destdir/$name$suffix", ".png");
		
		`metapixel $opts --prepare "$fullname" "$thumbname" "$destdir/tables.mxt"`;
		if ($? != 0) {
		    print "Error running metapixel - skipping file $fullname\n"
		    }
		else
		{
		    print "." if !$DEBUG;
		}
	    }
	    elsif (-d $fullname && -r $fullname && $do_recurse) {
		process_dir($fullname, $do_recurse);
	    }
	}
    } else {
	print "Error: cannot open directory $pdir\n";
    }
}

process_dir($srcdir, $do_recurse);
