#!/home/aimpro/utils/perl64/bin/perl -I/home/aimpro/utils/perl64/site/lib
use File::stat;
use Mail::Sender;
#use Date::Manip;

our $dirlist = "";
my $message = "";
my @db_vars;
my $subject = "Test";
my $readyToSend = 0;

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime( time );
$year += 1900;
$yday -= 5;
if( $yday <= 0 )
{
	$year --;
	if( $year % 4 == 0 )
	{
		$yday += 366;
	}
	else
	{
		$yday += 365;
	}
}
if( scalar( @ARGV ) > 0 )
{
	$year = int($ARGV[0] / 1000);
	$yday = $ARGV[0] - ($year * 1000);
	print "$ARGV[0], $year, $yday\n";
}
if( scalar( @ARGV ) > 1 )
{
	$message = $ARGV[1];
}
else
{
	$message = sprintf("Definitive daisy for %d-%03d", $year, $yday);
}
my @files2send;
my $numfiles = 0;
my $season = "south_2014";
my $test = sprintf("/aim/data/cips/anomalous/level_3/ver_04.20/rev_05/cips_sci_3a_%4d-%03d_v04.20_r05.png", $year, $yday);
my $test2 = sprintf("/aim/data/cips/anomalous/level_2/ver_04.20/rev_05/cips_sci_2_orbit_?????_%4d-%03d_v04.20_r05_???.png", $year, $yday);
print "$test\n";
print "$test2\n";

while ($readyToSend == 0)
{
    if (-e $test)
    {
	$readyToSend = 1;
	open(MAIL, "|/usr/sbin/sendmail -t");
	print MAIL "To: james.craft\@lasp.colorado.edu\n";
	print MAIL "cc: jvcraft87\@gmail.com\n";
	print MAIL "From: aimpro\@aimcips2.lasp.colorado.edu\n";
	print MAIL "Subject: DefinitiveDaisy has completed.\n\n";
	print MAIL "The Definitive Daisy has completed. Preparing to send ...\n";
	close(MAIL);

    } else {
	open(MAIL, "|/usr/sbin/sendmail -t");
	print MAIL "To: james.craft\@lasp.colorado.edu, cora.randall\@lasp.colorado.edu, william.barrett\@lasp.colorado.edu\n";
	print MAIL "cc: jvcraft87\@gmail.com\n";
	print MAIL "From: aimpro\@aimcips2.lasp.colorado.edu\n";
	print MAIL "Subject: Definitive Daisy has not completed.\n\n";
	print MAIL "The Definitive Daisy has not completed. Sleeping 30 minutes.\n";
	close(MAIL);
	sleep(1800);
    }
}
while( defined( $daisy = glob($test) ))
{
	$files2send[$numfiles] = $daisy;
	$numfiles ++;
}
while( defined( $orbit = glob($test2) ))
{
	$files2send[$numfiles] = $orbit;
	$numfiles ++;
}

print "Adding $numfiles attachments\n";
if( $numfiles > 0 )
{
	my $subject = sprintf("Definitive daisy for %d-%03d", $year, $yday);
	my $from = "aimpi\@lasp.colorado.edu";
	my $to = "cora.randall\@lasp.colorado.edu\n";
	my $cc = "james.craft\@lasp.colorado.edu, lumpe\@cpi.com, jvcraft87\@gmail.com, william.barrett\@lasp.colorado.edu\n";
	#my $to = "james.craft\@lasp.colorado.edu";
	#my $cc = "jvcraft87\@gmail.com";

	$sender = new Mail::Sender{smtp => 'localhost', from => $from,multipart => 'Mixed', msg => $message, };
 	$sender->OpenMultipart({
			from => $from, 
			to => $to, 
			cc => $cc,
			subject => $subject,
			msg => $message,
			ctype => "text/html",
          	        encoding => "7bit"
 		}) or die $Mail::Sender::Error,"\n";
	print "Adding attachments\n";
	for( $i = 0; $i < $numfiles; $i ++ )
	{
		if( $i == 0 && $message ne "" )
		{
			print "Sending message $message\n";
			$sender->Part({
               	        ctype => 'text/html',
			msg => $message	});
		}
		print "Adding attachment\n";
		$sender->Attach({
                        description => 'aimsds daisy png',
               	        ctype => 'image/png',
                       	encoding => 'base64',
	               	disposition => "inline; filename=$files2send[$i];\r\nContent-ID: <img1>",
                       	file => $files2send[$i]});
	}
	print"\nPreparing to close and send...\n";

	$sender->Close  or die $Mail::Sender::Error,"\n";
	exit;
}
