#! /usr/bin/perl
#
# send_reports.pl
#
# Purpose:
#    This is a function to email documents and abstracts as attachments  
#    to an XML email. 
#
# Notes:
#    1)  Sending data using this script does not result in any kind of 
#        response that indicates the data was received.  A web-based 
#        form on the AIM Science Data System website 
#        (http://aim.hamptonu.edu/sds/ under "Data Services/Admin")
#        provides the same functionality as this script and indicates
#        success or failure.  
#    2)  This is hard coded to GATS right now.  After the server is moved 
#        to Hamton U the variable $toAdress will need to be changed.
#

use XML::Simple;
use MIME::Lite;
use Net::SMTP;

# ---------------------------------------------------------------------
# sub send_email 
# ---------------------------------------------------------------------
#
# Arguments:
#   $subject - the subject of the email
#   $body
#   $type
#   $att_path - full path to the file
#   $att_file - filename
#
sub send_email {
   my  $subject = $_[0];
   my  $body = $_[1];
   my  $type = $_[2];
   my  $att_path = $_[3];
   my  $att_file = $_[4];
  
   my  $from_Address = "aim.sds\@lasp.colorado.edu";
   my  $to_Address = "aimsds\@aim.hamptonu.edu";
   my  $enc = "";
  
   if ($type eq 'TEXT'){
      $enc ="8bit";
      $type='text/plain';
   }
   if ($type eq 'DOC'){
      $enc ="base64";
      $type='application/doc';
   }
   if($type eq 'PDF'){
      $enc ="base64";
      $type='application/pdf';
   }
  
   my $message = MIME::Lite->new(
  				 From      => "$from_Address",
				 To        => "$to_Address",
				 Subject   => "$subject",
				 Type	   => 'multipart/mixed',
                                );
   $message->attach(
		    Type     => "text/plain",
                    Data     => "$body"
   );

   $message->attach(
  		    Type     => "$type",
		    Encoding => "$enc",
		    Path     => "$att_path",
		    Filename => "$att_file",
		    Disposition => "attachment"
		   ) or die "Error adding $att_file: $!\n";		   
   
   MIME::Lite->send("sendmail");
   $message->send();							
}


# ---------------------------------------------------------------------
# sub process_report 
# ---------------------------------------------------------------------
#
# Arguments: 
#    Instrument (CIPS, CDE), full path to file, filename, ReportName, 
#    source, description, document type, generation date, comments.  All 
#    agruments must be filled with a value or just "", Instrument, path and 
#    filename cannot be "" or NULL, but any of the other arguments can be.
#
sub process_report {

   my $xml = new XML::Simple ( RootName=>'REPORT');
   my $prod = new XML::Simple( RootName=>'REPORT');
   my $out;
   my $subject = $_[0];
   my $path = $_[1];
   my $filename = $_[2];
   my $report = $_[3];
   my $src = $_[4];
   my $desc = $_[5];
   my $dp_type = $_[6];
   my $gen_date = $_[7];
   my $data_date = $_[8];
   my $comments = $_[9];

   my @report =(
	          {PATH => "$path"},
	          {FILENAME => "$filename"},
              {REPORTNAME => "$report"},
	          {SOURCE => "$src"},
	          {DESCRIPTION => "$desc"},
	          {DATA_PRODUCT_TYPE => "$dp_type"},
	          {GENERATION_DATE => "$gen_date"},
	          {DATA_DATE => "$data_date"},
	          {COMMENTS => "$comments"},	       
               );

   $out = $xml->XMLout(\@report);

   #  Debug help.  Uncomment the following lines to see what is being produced.
   #print "$out\n";
   #open(XML,">pan_xml.txt");
   #print XML "$out";
   #close(XML);

   &send_email("$subject $report","$out",$dp_type,$path,$filename);   
}



#
# ---------------------------------------------------------------------
# update report info here
# ---------------------------------------------------------------------
#
my $path = "aim/pdc_interface/doc/testing.txt"; # full path to file
my $filename = "testing.txt";   
my $report = "Mission Status Report";       # Either Mission Status or Data Quality Report
my $src = "LASP";
my $desc = "PMC Abstract";
my $dp_type = "TEXT";        # Choices are TEXT, DOC or PDF
my $gen_date = "4-3-2006";  # DATES Must be in this exact format
my $data_date = "3-14-2006";
my $comments = "This is just for testing";

&process_report("CIPS",
	      $path,
	      $filename,
	      $report,
	      $src,
	      $desc,
	      $dp_type,
	      $gen_date,
	      $data_date,
	      $comments
	      );


