#!/usr/bin/perl -w
#  
#  Copyright (c) 2002 Steve Slaven, All Rights Reserved.
#  
#  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 of
#  the License, 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#  MA 02111-1307 USA
#  

use strict;
use vars qw( @ftypes $VERSION );

use Getopt::Std;

$VERSION = 0.32;
my %opts;
getopts( 'd:hc:', \%opts );

if( $opts{ h } )
{
	die( qq{
Make Image Sheet v$VERSION
Author: Steve Slaven

Usage: $0 [-d DPI] [-c COLS] [-h] [where]

	-d Set image DPI, default 75 (very blocky)
	-c Set number of columns to use, default 3
	-h This help

Where is a directory, if not passed defaults to current directory.
This is the starting point for the recursive file search for files to
invclude in the final sheet, which will be run through convert to create
jpg's and scaled to the proper DPI settings.

The converted images will be stored in the current directory as "safely"
named images that LaTeX can digest, and the output of the script is
the .tex file you will want to run 'pdflatex' on to create the final image
sheet.  All measurements are handled automagically, and for every file
that is included, a "FILENAME.desc" file will also be looked for.  If
found, the contents of this file are used as the description instead of
the stripped filename, i.e.

 /tmp/foo.jpg
 /tmp/foo.jpg.desc   <-- the description file for foo.jpg

} );
}

my $printwidth = 7.1;
my $cols = $opts{ c } || 3;

my $pbwidth = sprintf( "%3.2f", $printwidth / $cols );
my $imwidth = sprintf( "%3.2f", $pbwidth - ( $cols * .25 ) );

my $where = shift( @ARGV ) || '.';
my $dpi = $opts{ d } || 75;
@ftypes = qw( jpg gif png bmp );

$where =~ s#/$##;

my $itypes = join( ", ", @ftypes );

print STDERR qq{
Remember, the current directory is a scratch directory, use -h for help!

Reading files from: '$where'
Image DPI: $dpi
Image width: $imwidth
Parbox width: $pbwidth
Columns: $cols
File types: $itypes
---
Press <enter>
};

<STDIN>;

print q{
\documentclass[12pt]{article}
\usepackage[pdftex,
	letterpaper,
	headheight=.25in,
	headsep=.15in,
	footskip=.3in,
	margin=.5in]{geometry}

\usepackage{graphicx}
\usepackage{longtable}

\begin{document}
\begin{center}
};

print '\begin{longtable}{' . 'c' x $cols . "}\n";

my @files = recursive_find( $where );

my $cnt = 1;
my $dim = int( $dpi * $imwidth );
my( $safe_file, $doconv, $desc );
for( @files )
{
	$safe_file = $_;
	$safe_file =~ s/[^A-Za-z0-9]/_/g;
	$safe_file .= ".jpg";

	# Check file ages and see if we need to convert
	$doconv = 0;
	if( -f $safe_file )
	{
		$doconv = 0;
	}else
	{
		$doconv = 1;
	}

	if( $doconv )
	{
		print STDERR "Making safe/optomized: $safe_file\n";

		# Reduce to make about 75 dpi, so dims are 75 * imwidth
		system( "convert", "$_", "-sample", "${dim}x${dim}", "$safe_file" );
	}

	if( -f "$_.desc" )
	{
		open( TMP, "$_.desc" );
		$desc = join( "", <TMP> );
		close( TMP );
	}else
	{
		$desc = $_;
		$desc =~ s#^$where/##;
	}

	print qq|\\parbox{${pbwidth}in}{
\\fbox{
\\includegraphics[height=${imwidth}in,width=${imwidth}in,keepaspectratio]{$safe_file}
} 
\\\\ 
$desc}
|;
	#print "\\fbox{\\includegraphics[height=${imwidth}in,width=${imwidth}in,keepaspectratio]{$safe_file}}";

	if( $cnt++ % $cols == 0 )
	{
		print ' \\\\';
		$cnt = 1;
	}else
	{
		print " &";
	}
	print "\n";
}

# Fill in remaining
if( $cnt != 1 )
{
	while( $cnt++ != $cols )
	{
		print " &\n";
	}
	print "\\\\\n";
}

print q{\end{longtable}
\end{center}
\end{document}
};

sub recursive_find
{
	my $dir = shift;

	my( @files, $fname );
	local( *DIR );

	opendir( DIR, $dir );

	for( grep { /^[^.]/ } readdir( DIR ) )
	{
		if( -d "$dir/$_" )
		{
			push( @files, recursive_find( "$dir/$_" ) );
		}else
		{
			$fname = "$dir/$_";
			for( @ftypes )
			{
				if( $fname =~ /${_}$/ )
				{
					push( @files, "$fname" );
				}
			}
		}
	}

	closedir( DIR );

	return( @files );
}
