#!/usr/bin/perl
use strict;
use Net::FTP;
use Net::SFTP;
use File::Stat;
#my $host= 'axn.staging.amphire.com';
my $host= 'gw.procurement.itradenetwork.com';

my $user = 'ftpdma207';
my $pass = 'faWrudr5';
my $dir = '/';
my $fdir = '/home/sites/clm.dmadelivers.com/www/clmapp/public/files/opf/';
my $file = $fdir . $ARGV[0];
# ftp://axn.staging.amphire.com / ftpdma207 / faWrudr5



my $stat = new File::Stat($file);
print "UPLOAD: $file " . scalar localtime( $stat->mtime ) . " " . $stat->size . "bytes\n";

# exists, not zero-bytes, and is less than 1 minute old
if(-e $file && -s $file){   # && time() < $stat->mtime + 60 
	my $ftp = Net::FTP->new($host, Passive => 1, Debug => 1) or die "Cannot connect to some.host.name: $@";
#	my $ftp = Net::FTP->new($host, passive => 1, debug => 1, port => 2322, ssh_args => { port => '2322' }) or die "Cannot connect to some.host.name: $@";
	
	$ftp->login($user,$pass) or die "Cannot login ", $ftp->message;
	$ftp->cwd($dir) or die "Cannot change working directory ", $ftp->message;
	my $uploaded = $ftp->put($file) or die "put failed ", $ftp->message;
	$ftp->quit;
	
}else{
	print "ERROR: File $file does not exist or is zero bytes or is too old.\n";
}
