#!/usr/bin/perl 
#
# File: lab-test-response.pl
# Purpose: Repond to the downloaded lab report
# Author: Philip Poon
# Date: September 21, 2013
# usage: lab-test-response.pl  -ifile=YOURHL7FILEPATHNAME/XXXXXX.HL7  -ofile=YOURCURHST0_PATHNAME/CURHST.0
#
use strict;

# Declare global variables
my ($dryrun, $ifile, $ofile);
my @str_months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my ($msh_lab_date, $msh_id_prefix, $msh_id_suffix, $msh_lab_code);
my ($pid_id, $pid_name, $pid_sex, $pid_dummy, $pid_foo);

# Check input arguments
my $num_args = $#ARGV + 1;
if ($num_args < 1 || $num_args > 2) {
   &printUsage();
} else {
   while ( my $argv = shift(@ARGV)) {
      chomp;
      my ($key, $value) = split(/=/, $argv);
      if ($key =~ /^-ifile/i) {
          $key =~ s/^-//;
          if ($value =~ /^$/) {
              &printUsage();
          } else {
              $ifile = $value;
          }
      } elsif ($key =~ /^-ofile/i) {
          $key =~ s/^-//;
          if ($value =~ /^$/) {
              &printUsage();
          } else {               
              $ofile = $value;
          }
      } else {
          &printUsage();
      }
   }
}

if ($ifile =~ /^$/) {
    &printUsage();
} else {
    open HL,"< $ifile" || die "Can't open input file\n";
}

if ($ofile =~ /^$/) {
    $dryrun = 1;
} else {
    $dryrun = 0;
    open CURHST,"> $ofile" || die "Can't open output file\n";
}

while (<HL>) {
    if ($_ =~ /^(msh|pid)/i ) { 
       my @fields = split('\|', $_);
       if ($fields[0] =~ /msh/i) {
          $msh_lab_date = $fields[6];
          my $str_month = $str_months[substr($msh_lab_date, 4, 2)-1];
          $msh_lab_date =~ s/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/\3-$str_month-\1  \4:\5:\6/;
          ($msh_id_prefix, $msh_id_suffix, $msh_lab_code) = split('-', $fields[9]);
          $msh_lab_code =~ tr/145679/SXCHAM/; 
       } else { 
          my $foo;
          ($foo, $pid_id) = split('\-', $fields[3]);
          if ($msh_id_suffix =~ $pid_id) {
             my ($last, $first, $middle) = split('\^', $fields[5]);
             $pid_name = "$last, $first $middle";
             $pid_sex = $fields[8];
             $pid_foo = $fields[19];
             $pid_foo =~ s/.(.*)\r\n/\1/;
             $pid_foo =~ s/ /  /;
             my @ts = split(' ', scalar localtime(time));
             my $output_buffer = "$ts[2]-$ts[1]-$ts[4]  $ts[3]";
             $output_buffer .= "  REC  F";
             $output_buffer .= "  $msh_lab_code  $msh_id_suffix  $pid_foo";
             $output_buffer .= "  $pid_name $msh_id_prefix  $msh_lab_date";
             if ($dryrun == 1) {
                 print "$output_buffer\n";
             } else {
                 print CURHST "$output_buffer\n";
             }
          } else {
              print "id not match\n";
          }
       }
    }
}

close HL;
if ($dryrun == 1) {
    close CURHIST;
}

exit;

sub printUsage() {
    print "\nUsage: $0 -ifile=<input filename> [-ofile=<output filename>]\n";
    print "Where -ifile = input file\n";
    print "      -ofile = output file [optional]\n";
    exit;
}