#!/usr/bin/perl

# pictor: a web application for sharing, viewing, and organizing pictures
# thumbs: a script that recursively creates thumbnails of found images
#
# Copyright (C) 2000-2009 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use Getopt::Long;

sub get_owner() {
	`id www-data`;
	$rc = $?;
	if ($rc == 0) {
		return "www-data:www-data";
	}
	`id apache`;
	$rc = $?;
	if ($rc == 0) {
		return "apache:apache";
	}
	print("ERROR: could not find web user\n");
	exit 1;
}

sub thumbs($) {
	my $dir = shift;
	my $dh;
	if ( -d $dir ) {
		if ( ! -f "$dir/description.txt" ) {
			if ($VERBOSE) {
				print("Creating '$dir/description.txt'\n");
			}
			`touch "$dir/description.txt"; chown $OWNER "$dir/description.txt"; chmod 644 "$dir/description.txt"`;

		}
		opendir($dh, $dir);
		while ( my $obj = readdir($dh) ) {
			if ( $obj !~ /^\./ ) {
				if ( -d "$dir/$obj" ) {
					thumbs("$dir/$obj");
				} elsif ( -f "$dir/$obj" && $obj =~ /\.jp[e]{0,1}g$/i ) {
					if ( ! -d "$DESTDIR$dir/.thumbnails" ) {
						`mkdir -p "$DESTDIR$dir/.thumbnails"`;
					}
					if ( ! -e "$DESTDIR$dir/.thumbnails/$obj" || $FORCE ) {
						$geometry = $WIDTH . "x" .$WIDTH;
						if ($VERBOSE) {
							print("convert -thumbnail $geometry \"$dir/$obj\" \"$DESTDIR$dir/.thumbnails/$obj\"\n");
						}
						`jhead -autorot "$dir/$obj"`;
						`convert -thumbnail $geometry "$dir/$obj" "$DESTDIR$dir/.thumbnails/$obj"`;
					}
				}
			}
		}
		closedir($dh)
	}
	return 0;
}

sub contains(@) {
	my @array = @_;
	my $element = pop(@array);
	foreach my $i (@array) {
		if ($i eq $element) {
			return 1;
		}
	}
	return 0;
}

sub perms($) {
	my $dir = shift;
	`find "$dir" -type f -name description.txt | xargs -i chown $OWNER "{}"`;
	`find "$dir" -type f -name description.txt | xargs -i chmod 644 "{}"`;
}

sub usage() {
	print("Usage: thumbs [-f|--force] [-v|--verbose] [-d|--dest DEST] DIRECTORY\n");
	print("\tThis program recursively generates thumbnail jpg images.\n");
	print("\tOnly one DIRECTORY accepted.\n");
	print("\tAny other parameters will be ignored\n");
	exit 0;
}

########
# main #
########

$FORCE = 0;
$VERBOSE = 0;
$WIDTH = 150;

$OWNER = get_owner();

$opt = GetOptions ("force" => \$FORCE,
		   "verbose"   => \$VERBOSE,
		   "dest=s" => \$DESTDIR,
		   "width=s" => \$WIDTH);

my $dir = $ARGV[@ARGV-1];

if (!$dir) {
	usage();
}

if (!$DESTDIR) {
	$DESTDIR = "";
} else {
	$DESTDIR = "$DESTDIR/";
}

thumbs($dir);
perms($dir);
