# Pick directory thumbnail using captions.txt
sub start_plugin {
	my ($opt) = @_;

	album::hook($opt,'dir_thumb',\&dir_thumb);
  album::add_option(1, 'caption', album::OPTION_STR, default=>'dir_thumb',
                    usage=>'The caption string to look for');

	return {
		author => 'David Madison',
		href => 'http://MarginalHacks.com/',
		version => '1.0',
		description => <<'DESC'};
Pick directory thumbnail based on captions.txt file.

In captions.txt you can add a "caption" line:

dir_thumb	<file>

Where <file> is the image you want to use to select the directory thumbnail.
You can also specify a directory (relative paths only), in that case we'll
recursively look there for the thumbnail, using album dir_thumb behavior
(or possibly this plugin again)

Based on ideas from Dave Hansen <http://www.sr71.net/>
DESC
}

sub dir_thumb {
	my ($opt,$data,$hookname, $child, $child_data, @images) = @_;

	my $capname = album::option($opt,'caption');

	my $dir = $data->{paths}{dir};
	my $child_path = "$dir/$child";

	# BUG: If the captions setting changes in a sub-album, we won't notice.
	#      That's pretty minimal.

	my $caps = album::read_captions($opt,$child_path);
	my $thumb = $caps->{$capname}{name};

	return undef unless $thumb;

	my $path = "$child_path/$thumb";

	# An image
	return ($child_path,$child,$path,$thumb) if -f $path;

	# Allow for absolute or relative path (which could lead us
	# outside the album path!  Hope that 
	return album::dir_info($opt, $child_data, $thumb) if -d $path;

	# Oops.
	print STDERR "Couldn't find thumbnail [$thumb] for $child_path\n";
	return undef;

}

# Plugins always end with
1;

