#!/usr/local/bin/perl
#
# loads images recursively into the defined database
#
# $Id: load-imaverage-files,v 1.3 2000/06/26 18:57:44 nemesis Exp $
#
# Copyright (C) 2000 Cornelius Cook
# cook@cpoint.net, http://collective.cpoint.net/
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html

require "sql-attach.pl";

my $dbh = DBI->connect($SQL_dsn, $SQL_user, $SQL_password, { RaiseError => 1 } )
        || die "Can't connect to $dsn: $DBI::errstr";

Load($ARGV[0]);

print "Images loaded: $COUNT\n";


$dbh->disconnect;


sub Load {
	my($path)=@_;
	my($DIR,$file,$item);
	$DIR="FH$path";

#	warn "Searching '$path'...\n";

	opendir($DIR,"$path") || warn "Cannot opendir '$path': $!\n";
	foreach $item (grep(!/^\.{1,2}$/,readdir($DIR))) {
		$file="$path/$item";
		if ($file =~ /'/) {
			$new=$file;
			$new=~s/'//g;
			rename($file,$new) || die "Cannot rename '$file' to '$new': $!\n";
			$file=$new;
		}
#		warn "saw '$file'\n";
		Load("$file") if (-d $file);
		Record("$file") if (-f $file);
	}
}

sub Record {
	my($file)=@_;

#	warn "Recording '$file'...\n";

	$dbh->do("INSERT INTO Images (path) values ('$file')");
	$COUNT++;
}
