#! /usr/bin/perl
#
# process_pan_res.pl
#
# AUTHOR: Lon Riesberg
#
# DATE: March 20, 2006
#
# PURPOSE:
#   Parses a PAN response (PAN RES) from the PDC and 
#   verifies that the PAN was correctly received.  In 
#   the case of an error the PAN is forwarded to the
#   sds manager for analysis. 
#   
# NOTES:
#   1) This script is called by .procmailrc after it's 
#      determined that an incoming email is a PAN RES 
#      from the PDC.  The body of the PAN RES is piped 
#      here.
#   2) PAN files waiting for a PAN RES are stored in 
#      the pans_pending_resp directory.  Once correct
#      receipt of a PAN has been verified, the PAN is 
#      moved to the processed_pans directory.
#
# REFERENCES:
#   AIM Science Data System Interface Control Document
#   pdc_interface diagram
#   
# SUPPORTING DATABASE TABLES OR FILES:
#   ProductVersionMetadata
#

use XML::Simple;
use File::Copy;

# .procmailrc pipes the PAN RES to this routine. 
# the following 2 lines put the PAN RES into a var  
$/=UNDEF;
my $pan_res = <STDIN>; 


# debug help 
#&send_email("testing", $pan_res);


# init vars
my $pans_pending_resp_dir = "/aim/pdc/pans_pending_resp/";
my $processed_pans_dir = "/aim/pdc/processed_pans/";


# parse PAN RES
$xml = new XML::Simple;
$response = $xml->XMLin($pan_res);
%response_hash = map { %{$_} } @{$response};


# corresponding PAN 
$orig_pan = $pans_pending_resp_dir . $response_hash{'FILENAME'} . ".pan";


# if PAN RES is not ok, forward to sds manager for analysis  
if ($response_hash{'VALID'} ne "ok")
{
   # look for corresponding PAN 
   if (-e $orig_pan) 
   {
      my $subject = "PAN RES = " .  $response_hash{'VALID'} . "\:  " . $response_hash{'FILENAME'};
      &send_email($subject, $pan_res);
   }else 
   {
      my $subject = "PAN RES = " . $response_hash{'VALID'} . "\:  no matching PAN";
      &send_email($subject, $pan_res);
   }   
} 
  
# else if pan response is ok, move PAN from pans_pending_resp/ to processed_pans/  
else  
{    
   # look for corresponding PAN 
   if (-e $orig_pan) 
   {
      $moved_pan_file = $processed_pans_dir . $response_hash{'FILENAME'} . ".pan";
      
      # make sure this PAN isn't already in the $processed_pans_dir
      # shouldn't happen
      if (-e $moved_pan_file)
      {
         my $subject = "PAN RES ERROR:  file already exists";
         &send_email($subject, $pan_res);    
      }
      else
      {
         move($orig_pan, $moved_pan_file) or die "move failed: $!";
      }
   }
   #else
   #{
   #   my $subject = "PAN RES = " . $response_hash{'VALID'} . "\:  no matching PAN";
   #   &send_email($subject, $pan_res);
   #}
}


# ---------------------------------------------------------------------
# sub send_email 
# ---------------------------------------------------------------------
#
# PURPOSE:
#   Forwards the PAN RES to the SDS manager.  This is called when
#   a pan_res may need further analysis.
#
# ARGUMENTS:
#   $subject - the subject of the forwarded email.
#   $message - the message received from the PDC. 
#
# NOTES:
#   This message gets sent to aim.sds@lasp where it's routed by
#   .procmailrc to the AIM SDS manager
#
sub send_email 
{ 
   $subject = "Subject: " . $_[0] . "\n\n";
   $message = $_[1];
      
   unless(open (MAIL, "|/usr/sbin/sendmail -t")) {      print "error.\n";      warn "Error starting sendmail: $!";
   }else{      
      print MAIL "From: aim.sds\@lasp.colorado.edu\n";
      print MAIL "To: aim.sds\@lasp.colorado.edu\n";
      print MAIL $subject;
      print MAIL $message;
      close(MAIL) || warn "Error closing mail: $!";
   }
}

