# Album Plugin: images/autorot
# For info: 'album -plugin_info images/autorot'
# For usage:  'album -plugin_usage images/autorot'
use strict;		# Not required, but recommended

my $DESCRIPTION = << 'DESCRIPTION';
Autorotates images based on EXIF captions.
Uses 'jhead' program, and does rotation the first time it sees the image.
Idea credit:  Fridtjof Busse, who is too lazy to type jhead himself.  :)

DESCRIPTION

sub start_plugin {
	my ($opt) = @_;

	my $ret = {
		author      => 'David Ljung Madison',
		href        => 'http://MarginalHacks.com/',
		version     => '1.0',
		description => $DESCRIPTION,
	};


	# Setup the hooks
 	album::hook($opt, 'thumbnail', \&thumbnail);

	return $ret;
}


sub thumbnail {
	my ($opt, $hook,  $dir, $obj, $file, $full_path, $default) = @_;

	# If the thumbnail exists, don't rotate.
	return undef if -f $default;

	# Do we have jhead?
	return undef unless $opt->{jhead};

	# Quasi-bug - if we erase the thumbnail, then we'll call jhead again,
	# but this is correct if inefficient since jhead fixes the EXIF orientation.
	system($opt->{jhead},'-autorot',"$dir/$file");
	album::perror($opt,"Couldn't autorot image? [$dir/$file]\n") if $?;

	# Still return undef, we haven't actually generated the thumbnail.
	return undef;
}


# Plugins always end with:
1;
