#/usr/bin/perl -w
#
# This is the daily data file move program for the ESRL/GMD UV monitoring
# program.
#
# Basic Funtion:
#
# This module sends the daily data file to ESRL's ftp server.
#
# Requirements:
#
#
# Initial version 05/05/2008 MSO
#
# 15/05/2008 MSO - Added the clu log files
# 28/01/2015 MSO - Added 2nd FTP server and changed to UV3
#

use strict;

package ftpsend;

use Net::FTP;

my @timedata=localtime(time);
my $INI = 'c:/uv3/ftpsend.ini';

if ( -e $INI) {

open (Fileini,$INI);
my @Initiate=<Fileini>;
close(Fileini);

my @HostOne=split(/=/,$Initiate[0]);
my @temp=chomp($HostOne[1]);
$main::host1=($HostOne[1]);
my @HostTwo=split(/=/,$Initiate[1]);
my @temp=chomp($HostTwo[1]);
$main::host2=($HostTwo[1]);

print "Host one is: $main::host1\n";
print "Host two is: $main::host2\n";

} else {

open (Fileini,">$INI");
print Fileini "Hostone=awftp.cmdl.noaa.gov";
print Fileini "Hosttwo=lauder server";
close (Fileini);
die "The Ini File has been created\n";
}

my $directory=('transfer//uvnz');
#my $host1=('awftp.cmdl.noaa.gov');
#my $host2=('72.235.167.35');

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

my $mon_txt='';
my $day=sprintf("%02d",$timedata[3]);
my $month=sprintf("%02d",$timedata[4]+1);
my $year=sprintf("%02d",$timedata[5] % 100);

# If statements for first of year and first of the month movements -------
# January 1st if statement

if ( $day == "01" and $month == "01" ) {
	$month="12";
my $year=sprintf("%02d",($timedata[5] % 100)-1);
}

# If statements for first of every other month of the year----------------

if ( $day == "01" and $month != "01" ) {
	$month=sprintf("%02d",$timedata[4]);
}

#print "this is the month varible",$month;

if ( $month == "01" ) {
	$mon_txt='JAN';
}
if ( $month == "02" ) {
	$mon_txt='FEB';
}
if ( $month == "03" ) {
	$mon_txt='MAR';
}
if ( $month == "04" ) {
	$mon_txt='APR';
}
if ( $month == "05" ) {
	$mon_txt='MAY';
}
if ( $month == "06" ) {
	$mon_txt='JUN';
}
if ( $month == "07" ) {
	$mon_txt='JUL';
}
if ( $month == "08" ) {
	$mon_txt='AUG';
}
if ( $month == "09" ) {
	$mon_txt='SEP';
}
if ( $month == "10" ) {
	$mon_txt='OCT';
}
if ( $month == "11" ) {
	$mon_txt='NOV';
}
if ( $month == "12" ) {
	$mon_txt='DEC';
}

my $dailyfile1=('c:\\uv3\\UV3'.$year.$mon_txt.'.FPN');
my $dailyfile2=('c:\\uv3\\UV3'.$year.$mon_txt.'.LOG');
my $log_file1=('c:\\uv3\\clu\\CLU8config.txt');
my $log_file2=('c:\\uv3\\clu\\UV3clu8log.txt');


my $ftp=Net::FTP->new($main::host1,Timeout=>240,passive=>1,Debug => 0) or die "Cannot connect to: $@";
$ftp->login("anonymous",'-anonymous@') or die "Cannot Login", $ftp-> message;
$ftp->binary;
$ftp->cwd($directory) or die "Cannot change directory", $ftp-> message;
$ftp->put($dailyfile1);
$ftp->put($dailyfile2);
$ftp->put($log_file1);
$ftp->put($log_file2);
$ftp->quit;

my $ftp=Net::FTP->new($main::host2,Timeout=>240,passive=>1,Debug => 0) or die "Cannot connect to: $@";
$ftp->login("lauder","guavas") or die "Cannot Login", $ftp-> message;
$ftp->binary;
$ftp->cwd($directory) or die "Cannot change directory", $ftp-> message;
$ftp->put($dailyfile1);
$ftp->put($dailyfile2);
$ftp->put($log_file1);
$ftp->put($log_file2);
$ftp->quit;

1;
