#! /usr/bin/perl
#
# send_ground_based_obs_data.pl
#
# Purpose:
#    This is a function to email Ground Based Observation (GBO) data as attachments.  
#
# 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 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  $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 = "";
    
   $enc ="base64";
   $type='binary';
     
   my $message = MIME::Lite->new(
  				 From      => "$from_Address",
				 To        => "$to_Address",
				 Subject   => "$subject",
				 Type	   => 'multipart/mixed',
				 Encoding  => '7bit',
  				 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('smtp','localhost', Timeout => 60);
   $message->send();							
}



# ---------------------------------------------------------------------
# sub process_xfer
# ---------------------------------------------------------------------
#
sub process_xfer {

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

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

   my @xfer=(
           {PATH => $aa[1]},
	       {FILENAME => $aa[2]},
           {U_SOURCE => $aa[3]},
	       {DP_SOURCE => $aa[4]},
	       {CONTENT_CLASS => $aa[5]},
	       {CONTENTS => $aa[6]},
	       {GBO_LOCATION => $aa[7]},
	       {LOOK_DIRECTION => $aa[8]},
	       {COMMENTS => $aa[9]},
	       {START_DATETIME => $aa[10]},
	       {END_DATETIME => $aa[11]},
	       {START_LATITUDE => $aa[12]},
	       {END_LATITUDE => $aa[13]},
	       {START_LONGITUDE => $aa[14]},
	       {END_LONGITUDE => $aa[15]},
	       {START_ALTITUDE => $aa[16]},
	       {END_ALTITUDE => $aa[17]},
	       );

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

   #  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[6],$aa[1],$aa[2]);
}



#
# ---------------------------------------------------------------------
# update file info here
# ---------------------------------------------------------------------
#

my $path = "/home/horsehead/aimsds/sds/cips/docs/CIPS_ATBD_081005.doc"; # full path to file
my $filename = "CIPS_ATBD_081005.doc";   # File name
my $u_source = "CIPS SDS Manager"; 
my $dp_source = "CIPS DPC";
my $content_class = "Collaborative";   # See Table C - 16 in the Interface Control Document
my $contents = "Test File";
my $gbo_location = "-54.0:20.1:90.12";
my $look_direction = "45.12,121.23"; 
my $comments = "Testing...";
my $start_datetime = "2006-04-2 03:33:21";
my $end_datetime   = "2006-04-2 03:33:27";
my $start_latitude = "-54.0";
my $end_latitude = "0.10";
my $start_longitude = "20.1";
my $end_longitude = "21.3";
my $start_altitude = "87.5";
my $end_altitude = "91.3";


&process_xfer("DATA_TRANSFER",
	      $path,
	      $filename,
	      $u_source,
	      $dp_source,
	      $content_class,
	      $contents,
	      $gbo_location,
	      $look_direction,
	      $comments,
	      $start_datetime,
	      $end_datetime,
	      $start_latitude,
	      $end_latitude,
	      $start_longitude,
	      $end_longitude,
	      $start_altitude,
	      $end_altitude	      
	      );


