#!/usr/bin/perl

# ----------------------------------------------------------------------
# Author:       Stuart P. Robinson
# Date:         19 December 2002
# Description:  This script takes a directory of .JPG files and creates 
#               reasonable-sized versions of the pictures as well as 
#               thumbnails. The new filenames differ from the originals 
#               in two ways:
#               1) thumbnails have the prefix tn_. 
#               2) suffix is .jpg (NOT .JPG)
#
#  Copyright (C) 2003 Stuart P. Robinson
#  
#  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.
# ----------------------------------------------------------------------

use Getopt::Long;
use strict;
use English;
use vars qw( $help $debug $quiet );

main();

# ---------------------------------------------------
# Subroutines
# ---------------------------------------------------

sub main {
    handle_options();
    my $dir = get_args();
    my @pics = get_pics($dir);
    create_thumbnails($dir, \@pics, "150x150");
    convert_images($dir, \@pics, "500x500");
    print "\nDone!\n";
}

sub handle_options {
    # Get options
    GetOptions('q' => \$quiet,
	       'h' => \$help,
	       'd' => \$debug);
}

sub get_args {
    my $dir = $ARGV[0] or usage();
    unless ($dir =~ /^\//) {
	error("Use absolute paths (e.g., /home/user/pics/).");
    }
    print "Processing directory `$dir'...\n" unless $quiet;
    return $dir;
}

sub get_pics {
    my $dir = shift;
    opendir(DIR, $dir) or die "Can't open `$dir' for reading.";
    my @pics = grep { /\.JPG$/ } readdir(DIR);
    closedir(DIR);
    error("No pics found. Do images end with .JPG?") unless @pics > 1;
    return @pics;
}

sub convert_images {
    my $dir = shift;
    my $rPics = shift;
    my $size = shift;

    # Loop over files in directory and convert every .jpg
    print "\nConverting images...\n" unless $quiet;
    for my $filename (@$rPics) {
	print "Processing $filename...  " unless $quiet;
	my $old = "$dir/$filename";
	my $new = "$dir/$filename";
	$new =~ s/\.JPG$/\.jpg/;
        print "Creating `$new'...\n";
	my $command = "convert -resize $size $old $new";
	print "$command\n" if $debug;
	`$command`;
    }
    print "Finished shrinking .JPG files!\n" unless $quiet;
}

sub create_thumbnails {
    my $dir = shift;
    my $rPics = shift;
    my $size = shift;

    # Loop over files in directory and convert every .jpg
    print "\nCreating thumbnails...\n" unless $quiet;
    for my $filename (@$rPics) {
	print "Processing $filename...  " unless $quiet;
	my $old = "$dir/$filename";
	my $new = "$dir/tn_$filename";
	$new =~ s/\.JPG$/\.jpg/;
        print "Creating `$new'...\n";
	my $command = "convert -resize $size $old $new";
	print "$command\n" if $debug;
	`$command`;
    }
    print "Finished thumbnails!\n" unless $quiet;
}

sub error {
    my $msg = shift;
    die "ERROR: $msg\n";
}

sub usage {
    die "$PROGRAM_NAME [-q] <ABSOLUTE DIRPATH>\n";
}

