#! /usr/bin/perl
#
# send_pan.pl
#
# Purpose:
#    This is a function to email a Product Availability Notice to the 
#    Project Data Center. 
#
# Notes:
#

use XML::Simple;

# ---------------------------------------------------------------------
# sub process_pan 
# ---------------------------------------------------------------------
#
# Arguments: 
#    Instrument (CIPS, CDE), action, filename, title, source, mission, 
#    Data Product Type, URL, Data Product Version, Product Format version, 
#    Software version, Software name, Calibration Version, Description, 
#    Generation Date, history, Start Time, End Time, start latitude, end latitude,
#    start longitude, end longitude, start altitude, end altitude, comments
#
# Notes:
#    All agruments must be filled with a value or just "", Instrument, action 
#    and filename cannot be "" or NULL, but any of the other arguments can be the 
#    variable action is either ADD, DEL, UPD to, add, delete or update record 
#    respectively.
#
sub process_pan {

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

   my $local_pan_dir = "/aim/pdc/pans_pending_resp/";
   
   for($i = 0;$i < 22;$i++){
      $aa[$i]= $_[$i];
   }

   my @product =(
	          {ACTION => $aa[1]},
	          {FILENAME => $aa[2]},
              {TITLE => $aa[3]},
	          {SOURCE => $aa[4]},
	          {MISSION => $aa[5]},
	          {DATA_PRODUCT_TYPE => $aa[6]},
	          {URL => $aa[7]},
	          {DATA_PRODUCT_VERSION => $aa[8]},
	          {PRODUCT_FORMAT_VERSION => $aa[9]},
	          {SOFTWARE_VERSION => $aa[10]},
	          {SOFTWARE_NAME => $aa[11]},
	          {CALIBRATION_VERSION => $aa[12]},
	          {DESC => $aa[13]},
	          {GENERATION_DATE => $aa[14]},
	          {HISTORY => $aa[15]},
	          {START_TIME => $aa[16]},
	          {END_TIME => $aa[17]},
	          {LATITUDE => $aa[18]},
	          {LONGITUDE => $aa[19]},
	          {ALTITUDE => $aa[20]},
	          {COMMENTS => $aa[21]}
	        );

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

   #  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);

   my $local_pan = $local_pan_dir .  $aa[2] . ".pan";
   
   # make sure this PAN hasn't already been sent
   # shouldn't happen
   if (-e $local_pan)
   {
      print "Error\: PAN already added for " . $aa[2] . "\n"; 
   }
   else
   {
      # add PAN to PAN file
      print "adding PAN for " . $aa[2] . "...\n";
      my $concat_pan_file = "/aim/pdc/cde_pans.txt";
      open CONCAT_PAN_FILE, ">>", "$concat_pan_file" or die $!;
      print CONCAT_PAN_FILE "$out" or die $!;
      close CONCAT_PAN_FILE or die $!; 
   
      # save PAN to pan_pending_resp directory
      open LOCAL_PAN_FILE, ">", "$local_pan" or die $!;
      print LOCAL_PAN_FILE "$out" or die $!;
      close LOCAL_PAN_FILE or die $!; 
   }
}


#
# ---------------------------------------------------------------------
# update PAN info here
# ---------------------------------------------------------------------
#

my $ins = $ARGV[0];
my $act = $ARGV[1]; 
my $filename = $ARGV[2];
my $title = $ARGV[3];
my $src = $ARGV[4];
my $mission = $ARGV[5];
my $dp_type = $ARGV[6];
my $url = $ARGV[7]; 
my $dp_version = $ARGV[8];
my $pf_version = $ARGV[9];
my $sw_version = $ARGV[10];
my $sw_name = $ARGV[11];
my $calib_version = $ARGV[12];
my $desc = $ARGV[13];
my $gen_date = $ARGV[14];
my $history = $ARGV[15];
my $start_time = $ARGV[16];
my $end_time = $ARGV[17];
my $lat = $ARGV[18];
my $lon = $ARGV[19];
my $alt = $ARGV[20];
my $comments = $ARGV[21];

&process_pan($ins,
	      $act,
	      $filename,
	      $title,
	      $src,
	      $mission,
	      $dp_type,
	      $url,
	      $dp_version,
	      $pf_version,
	      $sw_version,
	      $sw_name,
	      $calib_version,
	      $desc,
	      $gen_date,
	      $history,
	      $start_time,
	      $end_time,
	      $lat,
	      $lon,
	      $alt,
	      $comments
	      );
