#!/Perl
# AIM: To check and test the Perl FTP interface ... 2006.06.27 - geoff.
# ftpfetch01.pl
use Net::FTP;

my $LF;
my $write_log = 0;
require "logfile.pl" or die "Log file code not avaialable ...\n";

my $outfile = 'temp'.$0.'.txt';
my $site = "ftp.geoffmclane.com";
my $name = 'geoffmc';
my $pwd = '*******';
my $dir = 'logs';
my $pf = 'public_html';
my $do_folder = 0;
my @folders = ();
my @pubs = ();
my $item = '';
open_log($outfile);

prt( "$0 running ...\n" );

$ftp = Net::FTP->new($site) or mydie( "Can not connect to $site ...\n" );

$ftp->login($name, $pwd) or mydie( "Can not login ...\n" );

prt( "Getting files and folders ...\n" );
@folders = $ftp->ls();

my $cnt = scalar @folders;
prt( "Got count of $cnt items ...\n" );
$cnt = 0;
foreach $item (@folders) {
	$cnt++;
	prt( "$cnt $item ...\n" );
	if ($item eq $pf) {
		prt( "Got public folder ... setting do_folder ...\n" );
		$do_folder = 1;
	}
}

$cnt = 0;
if ($do_folder) {
	$ftp->cwd($pf) or mydie( "ERROR: Unable to change to $pf ...\n" );
	@pubs = $ftp->ls();
	$cnt = scalar @pubs;
	prt( "Got $cnt items in $pf ...\n" );
}

if ($cnt) {
	$cnt = 0;
	foreach $item (@pubs) {
		$cnt++;
		prt( "$cnt $item ...\n" );
	}
}

###NOT $ftp->Quit();, but
$ftp->close();
close_log($outfile, 1);
exit(0);

#############################################
sub do_the_rest {
$ftp->cwd($folders[0]);
@logfiles = $ftp->ls();
foreach $file (@logfiles)	{	
	# check remote size
	my $fileSize = $ftp->size($file);
	# check local size
	my ($dev, $ino, $mode, $nlink, $uid, $gid, 
		$rdev, $size, $aatim, $mtime, $ctime,
		$blksize, $blocks) = stat("d:\\wwwroot\\rtfmlogs\\$file");
	print "fetching file : $file ($fileSize) local size : ($size)\n";	
	# compare local and remote size
	if($fileSize ne $size )	{
		$ftp->get($file, "d:\\wwwroot\\rtfmlogs\\$file") or
				 print "error $!";
		print "file fetched\n";
	}	else	{
		print "file skipped\n";
	}
}

print "operation successful";

}

### eof - ftpget01.pl
