#/usr/bin/perl -w
#
# This is the FTP Module of the ReOrg.pl program
# part of the CWOP Transmitting program for ESRL's Global Monitoring Division
#
# Basic Funtion:
#
# This module sends the daily minute data file to ESRL's ftp server.
#
# Requirements:
#
# Initial version 05/08/2006 MSO
#
#
#

use strict;

package ftpsend;

use Net::FTP;

my @timedata=localtime(time);

my $host=('awftp.cmdl.noaa.gov');

# [2] = hour
# [3] = mday
# [4] = month
# [5] = year
# [6] = wday
# [7] = yday

my $day=sprintf("%02d",$timedata[3]);
my $month=sprintf("%02d",$timedata[4]+1);
my $year=sprintf("%02d",$timedata[5] % 100);
my $hour = sprintf("%02d",$timedata[2]);
my $minute = sprintf("%02d",$timedata[1]);
my $stn_num = "";
my $ftpdir = "//data/met/";
my $Log_file = "c:\\Intrcept\\log.dat";

print "Enter the Station three letter code : \n";
chomp(my $station = <>);

my $directory = $ftpdir.$station;

print "The directory used is : ",$directory," \n";

print "Enter 1 for Passive Mode or 0 for Active Mode : \n";
chomp(my $passive = <>);

if ( $station = "MLO" ) {
	$stn_num = "031";
}
if ($station = "BRW") {
	$stn_num = "199";
}

my $dailyfile1 = ('c:\\Intrcept\\wxdata\\AR'.$year.$month.$day.".".$stn_num);

open Log,">>$Log_file" || warn "Logon Error $main::Log_file $!\n";
print Log "the directory used is ",$directory," thank you\n";
close Log;

eval {
	  no warnings 'all';

my $ftp=Net::FTP->new($host,Timeout=>240,passive=>$passive,DEBUG =>1);
$ftp->login("anonymous",'-anonymous') or warn "logon error ", $ftp-> message;
$ftp->cwd($directory) or warn "change directory error ", $ftp-> message;
$ftp->pwd or warn "can not list working directory ", $ftp-> message;
$ftp->binary or warn "cannot change to binary ", $ftp-> message;
$ftp->put($dailyfile1) or warn "unable to put file", $ftp-> message;
$ftp->quit;

};
#end eval

if ($@ =~ m/logon/) {
open Log,">>$Log_file" || warn "Logon Error $main::Log_file $!\n";
print Log "$month\\$day\\$year : $hour H $minute M Unable to Logon to Server \n";
close Log;
next;
}#endif

if ($@ =~ m/change/) {
open Log,">>$Log_file" || warn "Error Changeing Directory $main::Log_file $!\n";
print Log "$month\\$day\\$year : $hour H $minute M Unable to Change Directory \n";
close Log;
next;
}#endif

if ($@ =~ m/put/) {
open Log,">>$Log_file" || warn "Put Error $main::Log_file $!\n";
print Log "$month\\$day\\$year : $hour H $minute M Unable to Put file \n";
close Log;
next;
}#endif

1;
