#! /usr/bin/perl
#
# send_product_specs.pl
#
# Purpose:
#    This is a function to email product specifcations and CDL files 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.
#
#    3)  See Table C - 14 in the Interface Control Document for full description of fields
#

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


# ---------------------------------------------------------------------
# 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  $att_path = $_[2];
   my  $att_file = $_[3];
   my  $from_Address = "aim.sds\@lasp.colorado.edu";
   my  $to_Address = "aimsds\@aim.hamptonu.edu";
   my  $enc = "";
  
   $enc = "base64";
   $type = "application/cdl";
   print "$att_path\n";
  
      
   my $message = MIME::Lite->new(
  				 From      => "$from_Address",
				 To        => "$to_Address",
				 Subject   => "$subject",
				 Type	   => 'text/plain',
				 Encoding  => '7bit',
  				 Data	   => "$body"
                                );

   $message->attach(
  		    Type     => "$type",
		    Encoding => "$enc",
		    Path     => "$att_path",
		    Filename => "$att_file"
		   );

		   
   MIME::Lite->send('smtp','localhost', Timeout => 20);
   $message->send();								
}


# ---------------------------------------------------------------------
# sub process_spec
# ---------------------------------------------------------------------
#
sub process_spec {

   my $xml = new XML::Simple ( RootName=>'PRODUCT_SPECIFICATION');
   my $specs = new XML::Simple( RootName=>'PRODUCT_SPECIFICATION');
   my $out;
   my @aa;

   for($i = 0;$i < 29;$i++){
      $aa[$i]= $_[$i];
   }

   my @specs=(
	       {PATH => $aa[1]},
	       {FILENAME => $aa[2]},
           {DP_TYPE => $aa[3]},
	       {DP_SOURCE => $aa[4]},
	       {NAME_CONVENTION => $aa[5]},
	       {DESCRIPTION => $aa[6]},
	       {DP_VERSION => $aa[7]},
	       {GEN_CLASS => $aa[8]},
	       {CONTENT_CLASS => $aa[9]},
	       {FREQUENCY => $aa[10]},
	       {FILE_SIZE => $aa[11]},
	       {FILE_TYPE => $aa[12]},
	       {CONTENTS => $aa[13]},
	       {RECIPIENT => $aa[14]},
	       {COMMENTS => $aa[15]},
	       {DP_VERSION_DESC => $aa[16]},
	       {DP_FORMAT_DESC => $aa[17]},
	       {SW_VERSION_DESC => $aa[18]},
	       {CAL_VERSION_DESC => $aa[19]},
	       {DQ_CONFIDENCE => $aa[20]},
	       {DQ_VERSION => $aa[21]},
	       {DQ_REVIEW => $aa[22]},
	       {DQ_USES => $aa[23]},
	       {PARAMETERS => $aa[24]},
	       {INST_MODE => $aa[25]},
	       {MAX_ALT => $aa[26]},
	       {MIN_ALT => $aa[27]},
	       {REFERENCE => $aa[28]}
	       	       
	     );

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

   #  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("$aa[0]", "$out", $aa[1], $aa[2]);
}



#
# ---------------------------------------------------------------------
# update file info here
# ---------------------------------------------------------------------
#
# Notes:
#    See Table C - 14 in the Interface Control Document for full description of fields
#

my $path = "/aim/pdc_interface/doc/L1A_CIPS_STRUCT.ATT"; # full path to file
my $filename = "L1A_CIPS_STRUCT.ATT"; 
my $dp_type = "Level1 Test"; 
my $dp_source = "CIPS DPC";
my $name_convention = "L1_Test_yyyy_mm_doy.nc";
my $description = "Test File";
my $dp_version = "0.01";
my $gen_class = "on-demand"; 
my $content_class = "support";
my $frequency = "yearly";
my $file_size = "1M";
my $file_type = "Other";
my $contents = "stuff";
my $recipient = "me";
my $comments = "Just to test the interface";
my $dp_version_desc = "Test File";
my $dp_format_desc = "Ad-Hoc";
my $sw_version_desc = "0.01";
my $cal_version_desc = "N/A";
my $dq_confidence = "Low";
my $dq_version = "Test";
my $dq_review = "None";
my $dq_uses = "Quicklook";
my $parameters = "none";
my $inst_mode = "off";
my $max_alt = "0";
my $min_alt = "0";
my $reference = "N/A";


&process_spec("PRODUCT_SPECIFICATION",
	      $path,
	      $filename,
	      $dp_type, 
	      $dp_source,
	      $name_convention,
	      $description,
 	      $dp_version,
 	      $gen_class, 
 	      $content_class,
 	      $frequency,
 	      $file_size,
 	      $file_type,
 	      $contents,
 	      $recipient,
 	      $comments,
 	      $dp_version_desc,
 	      $dp_format_desc,
 	      $sw_version_desc,
	      $cal_version_desc,
	      $dq_confidence,
	      $dq_version,
 	      $dq_review,
	      $dq_uses,
	      $parameters,
	      $inst_mode,
 	      $max_alt,
 	      $min_alt,
 	      $reference    
	      );

