# Copyright 2005-2007 Interchange Development Group and others
# 
# 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.  See the LICENSE file for details.

CodeDef imagedir Widget 1
CodeDef imagedir Description Image listing
CodeDef imagedir ExtraMeta <<EOM
{
	_order => [ qw/ follow_symlinks / ],
	follow_symlinks => {
		widget => 'yesno',
		label => 'Follow Symlinks',
		help => 'Set to yes if you want to list all files, even following symbolic links',
	},
}
EOM

CodeDef imagedir Routine <<EOR
use File::Find;
sub {
	my ($opt) = @_;
	my $dir = delete $opt->{dir}	|| delete $opt->{outboard};
	my $suf = delete $opt->{suffix} || delete $opt->{options};
	return undef unless -d $dir;
#::logDebug("passed suf=$suf");
	$suf = '\.(GIF|gif|JPG|JPEG|jpg|jpeg|png|PNG)'
		unless $suf;

	if($suf and $suf !~ /[\.|]/) {
		my @types = grep /\S/, split /[,\s\0]+/, $suf;
		$suf = '\.(' . join("|", @types) . ')';
	}

	my @names;
	my $regex;
	eval {
		$regex = qr{$suf$};
	};
	return undef if $@;
	my $wanted = sub {
					return undef unless -f $_;
					return undef unless $_ =~ $regex;
					my $n = $File::Find::name;
					$n =~ s:^$dir/?::;
					push(@names, $n);
				};
	find({ wanted => $wanted, follow => $opt->{follow_symlinks} }, $dir);
	$opt->{passed} = [ '=None', sort @names ];
	$opt->{type} = delete $opt->{variant} || 'combo';
	return Vend::Form::display($opt);
}
EOR
