You are on page 1of 11

#!

/usr/bin/perl
# $Header:
# Use a pragma that restricts use of unsafe constructs, such as symbolic
# references, barewords, and undeclared variables.
# This pragma is part of the standard Perl library.
use strict;
# Use a module that allows English-like words to be used in place of
# Perl's special variables aka punctuation variables.
# This module is part of the standard Perl library.
use English;
# Use a module for file copying.
# This module is part of the standard Perl library.
use File::Copy qw(cp mv);
# Use a module for parsing file specifications into useful pieces.
# This module is part of the standard Perl library.
use File::Basename;

# Use a module provides basic email functionality.


# This module was downloaded from CPAN.
#use Net::SMTP;
use POSIX qw(strftime);
my %CONFIG_PARAMS;
#set -x
# declare a few useful subroutines, this allows us to use them like builtin func
tions
sub Println($);
sub Translate(%);
my $error_flag = 'n';
my $st = strftime('%H%M', localtime);
#Println ($st);
my $sd = strftime('%Y%m%d', localtime);
#Println ($sd);
my $debug = 1;
our $LOGS;
my $inout = shift @ARGV;
unless ( $inout eq '-i' or $inout eq '-o' ) {
Println("Usage : $0 -i|-o");
exit 1;
}

if ( $inout eq '-i' ) {
$CONFIG_PARAMS{"INBOUND"}="true";
} else {
$CONFIG_PARAMS{"INBOUND"}="false";
}
unless (open FILE , "< $0.conf") {
Println ("ERROR : Could not open $0.conf configuration file");
exit 1;
}
# parse the config file
while (<FILE>) {
# remove leading spaces
$_ =~ s/^\s+//g;
# remove trailing spaces
$_ =~ s/\s+$//g;
# ignore comments
next if $_ =~ /^#/;
my @params = split /=/ , $_;
$CONFIG_PARAMS{$params[0]}=$params[1];
}
my $date = strftime '%Y%m%d', localtime;
my $logdir = $CONFIG_PARAMS{'LOG_DIR'};
$logdir = $logdir . "/" if $logdir !~ /\/$/;
my $log = $logdir . $date . '.log';
open LOGS , ">> $log" or die "ERROR : Couldn't open $log for writing/appending.
$!\n";
my $AI_QUEUEID = $ENV{'OT_QUEUEID'};
my $OT_DIR = $ENV{'OT_DIR'};
unless (Translate(\%CONFIG_PARAMS)) {
Println "Translation was not successful, check logs for more details";
exit 1;
}
close LOGS;

# Translate inbound/outbound files


sub Translate(%) {
local $ARG;
my $CONFIG_PARAMS = shift @ARG;
my $attach;
my $aicommand;
my $dir;
if ($CONFIG_PARAMS->{'INBOUND'} eq 'true') {
$dir = $CONFIG_PARAMS->{'INBOUND_IN_DIR'};
Println "Inbound dir set to $dir" if $debug;
} else {
$dir = $CONFIG_PARAMS->{'OUTBOUND_IN_DIR'};
Println "Outbound dir set to $dir" if $debug;
}
unless (opendir DIR , $dir) {
Println "ERROR : Couldn't open dir $dir , " . "$!";
return;
}

my @files = readdir(DIR);

if (scalar @files - 2 == 0) {
Println "ERROR : No files exist in $dir , no translation performed"; retur
n 1;
}
Println "Total files found " . scalar @files - 2 if $debug;
# archive date time stamp
my $archdts = strftime('%Y%m%d%H%M%S', localtime);
if ($CONFIG_PARAMS->{'INBOUND'} eq 'true') {

Println "Processing inbound files" if $debug;


# cat file name
my $catfile = "$dir/in.$$";
# cat all the inbound files
#system ("cat $dir/* | grep -v directory >> $catfile");
# inbound attachment
$attach = "OTRecogn.att";
my @sessionnumbers = ();
foreach my $file (@files) {
$file = $dir ."/". $file;
next if $file =~ /^\./ or -d $file;
my $tempfile = $file . 'tmp';
unless (cp $file , $tempfile) {
Println "ERROR : Could not copy $file to $tempfile, $!";
return;
}
$aicommand = qq{"$ENV{'OT_DIR'}/bin/inittrans" -cs $ENV{'OT_QUEUEID'}
-at $attach -DINPUT_FILE=$file};
Println "Translating file $catfile";
my $sessionnumber = callAI($aicommand , \%CONFIG_PARAMS);
push @sessionnumbers, $sessionnumber;
if ($sessionnumber != 0) {
my $originalname = basename $file;
#$file = $dir ."/". $file;
my $arch_dir = $CONFIG_PARAMS->{'INBOUND_ARCH_DIR'};
Println "Inbound Archive dir set to $arch_dir" if $debug;
my $arch_filename = $arch_dir . "/" . $originalname . "." .
$sessionnumber;
unless (cp $tempfile , $arch_filename) {
Println "ERROR : Could not copy $file to $arch_file
name, $!";
return;
}
Println "Archived file $file to $arch_filename";
unlink $tempfile;
}
}
#if ($sessionnumber != 0) {
# foreach my $file (@files) {
# my $filename = $file;
# $file = $dir ."/". $file;
# next if $file =~ /^\./ or -d $file;
# my $arch_dir = $CONFIG_PARAMS->{'INBOUND_ARCH_DIR'};
# Println "Inbound Archive dir set to $arch_dir" if $debug;
# my $arch_filename = $arch_dir . "/" . $filename . "." . $sessionnumb
er;
#my $arch_filename = $arch_dir . "/" . $filename . "_" . $archdts;
# unless (cp $file , $arch_filename) {
# Println "ERROR : Could not copy $file to $arch_filename, $!";
# return;
# }
# Println "Archived file $file to $arch_filename";
# unlink $file;
# }
#}
#Println "Deleting cocatenated file $catfile";
#unlink $catfile;
} else {
# Process each outbound file
OUTBOUND: foreach my $file (@files) {
my $filename = $file;
next if $file =~ /^\./ or -d $file;
$file = $dir ."/". $file;
Println "Processing outbound file $file" if $debug;
# delete sig files
if ($file =~ /\.sig/) {
Println "Deleting sig file $file" if $debug;
unlink $file;
next;
}
## if ($file !~ /$CONFIG_PARAMS->{'OUTCRIT'}/) {
## Println "Skipping $file, doesn't match $CONFIG_PARAMS->{'OUTCRIT'}";
## next OUTBOUND;
## }
# main attach for outbound
$attach = "OTEnvelp.att";
# archive directory for outbound files
my $arch_dir = $CONFIG_PARAMS->{'OUTBOUND_ARCH_DIR'};
Println "Outbound Archive dir set to $arch_dir" if $debug;
# is this an TGTASN
if ( $file =~ /$CONFIG_PARAMS->{'TGTASN'}/ ) {
# yes it is so call the sort attachment
$attach = "TGTo856FFEDI.att";
Println "Attachment set to $attach";
}
# is this an ECPOAO
if ( $file =~ /$CONFIG_PARAMS->{'ECPOAO'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo855FFEDI.att";
Println "Attachment set to $attach";
}
# is this an DSVINV
if ( $file =~ /$CONFIG_PARAMS->{'DSVINV'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo846FFEDI.att";
Println "Attachment set to $attach";
}
# is this an INO
if ( $file =~ /$CONFIG_PARAMS->{'INO'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBSONo810FFEDI.att";
Println "Attachment set to $attach";
}
# is this an DSVRMA
if ( $file =~ /$CONFIG_PARAMS->{'DSVRMA'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo180FFEDI.att";
Println "Attachment set to $attach";
}
# is this an DSVASN
if ( $file =~ /$CONFIG_PARAMS->{'DSVASN'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBSONo856FFEDI.att";
Println "Attachment set to $attach";
}
# is this an DSNO
if ( $file =~ /$CONFIG_PARAMS->{'DSNO'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBSONo856FFEDI.att";
Println "Attachment set to $attach";
}
# is this an EPOCAC
if ( $file =~ /$CONFIG_PARAMS->{'EPOCAC'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo865FFEDIC.att";
Println "Attachment set to $attach";
}
# is this an TGT865
if ( $file =~ /$CONFIG_PARAMS->{'TGT865'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo865FFEDIS.att";
Println "Attachment set to $attach";
}
# is this an ECPOAC
if ( $file =~ /$CONFIG_PARAMS->{'ECPOAC'}/ ) {
# yes it is so call the sort attachment
$attach = "GIBo855CSFFEDI.att";
Println "Attachment set to $attach";
}
## my $mapattach;
# grab the map attach from the conf file
## while (my ($key , $value) = each(%CONFIG_PARAMS)) {
## if ($file =~ /$key/) {
## $mapattach = $value;
## }
## }
# is there a map attach for this file?
unless (defined $mapattach) {
#Println "Could not find the map attachment for $file, please check the c
onfiguration file and try again";
Println "WARN : Ignoring $file, not found in the config file";
next OUTBOUND;
}
# set the trace level
my $tracelevel = 0;
# set trace level to 0 if debug
$tracelevel = 0 if $debug;
my $cpfile = $file . ".cp";
unless (cp $file , $cpfile) {
Println "ERROR : Could not copy $file to $file . '.cp'";
return;
}
# construct aicommand
$aicommand = qq{"$ENV{'OT_DIR'}/bin/inittrans" -cs $ENV{'OT_QUEUEID'} -at $
attach -DINPUT_FILE=$cpfile -DMESSAGE=$mapattach -DBYPASS=OTINOByp.att};
# call the translator
my $sessionnumber = callAI($aicommand , \%CONFIG_PARAMS);
my $arch_filename = $arch_dir . "/" . $filename;
if ($sessionnumber != 0) {
# archive file to the archive directory
$arch_filename = $arch_filename . "." . $sessionnumber;
unless (cp $file , $arch_filename) {
Println "ERROR : Could not archive $file to $arch_filename, $!";
return;
}
# delete the file, no longer needed
unlink $file;
# delete the temp file, if AI is not deleting this file, specially for t
he 856
unlink $cpfile if -e $cpfile;
}
} # OUTBOUND : foreach
closedir DIR;
}

if ($error_flag eq 'y') {

Println("E-mailing Exception Report");


my $scrip_call = 'C:/AI_EDI/AIScripts/ExceptionReport.sh'; ##$CONFIG_PARAMS->{'S
CRIPT_DIR'} . '/ExceptionReport.sh ' . $sd . ' ' .$st;
system($scrip_call);
}
#if ( $inout eq '-i' ){
#
#
#
#Println("E-mailing Inbound Document Report");
##my $scrip_call = 'C:/AI_EDI/AIScripts/InboundDocumentReport.sh' ($sd) #; ##$
CONFIG_PARAMS->{'SCRIPT_DIR'} . '/InboundDocumentReport.sh ' .' ($sd)' . .'($s
t)';
#my $scrip_call = 'C:/AI_EDI/AIScripts/btInboundDocumentReport.sh' .' '. $sd .
' ' .$st;
#Println ($scrip_call);
#system($scrip_call);
#
#}
if ( $inout eq '-o' ){
Println("E-mailing Outbound Document Report");
#my $scrip_call = 'C:/AI_EDI/AIScripts/btOutboundDocumentReport.sh' .' '. $sd .
' ' .$st;; ##$CONFIG_PARAMS->{'SCRIPT_DIR'} . '/OutboundDocumentReport.sh '; # .
$sd . ' ' .$st;
#system($scrip_call);
}
# return successfully
return 1;
}
sub callAI() {
local $ARG;
my $aicommand = shift @ARG;
my $CONFIG_PARAMS = shift @ARG;
my $tracelevel = 0;
$tracelevel = 0 if $debug;
$aicommand = $aicommand . " -tl " . $tracelevel . " -I";
Println ($aicommand) if $debug;
my @output = ();
chomp(@output = qx($aicommand 2>&1));
if ($debug) {
for my $out (@output) {
Println($out);
}
}
my $sessionnumber;
LINE: for my $line ( @output ) {
if ( $line =~ /Session# (\w{1,9}) completed successfully./ ) {
# the process completed without any apparent errors
$sessionnumber = $1;
last LINE;
} # end of if $line =~ ...
if ( $line =~ /Cannot connect with server|Connection lost with Server|Inv
alid command arguments/i ) {
Println "ERROR : $line";
return;
}
}
my @applfiles = FindSessionFiles($ENV{'OT_DIR'} . '/' . $CONFIG_PARAMS->{'AP
PL'} . '/' , $sessionnumber);
my @commfiles = FindSessionFiles($ENV{'OT_DIR'} . '/' . $CONFIG_PARAMS->{'CO
MM'} . '/', $sessionnumber);
my @excpfiles = FindSessionFiles($ENV{'OT_DIR'} . '/' . $CONFIG_PARAMS->{'EX
CP'} . '/', $sessionnumber);
my @logsfiles = FindSessionFiles($ENV{'OT_DIR'} . '/' . $CONFIG_PARAMS->{'LO
GS'} . '/', $sessionnumber);
if (scalar @applfiles > 0) {
for my $afile (@applfiles) {
sleep 1;
my $inboundout = $CONFIG_PARAMS->{'INBOUND_OUT_DIR'};
my $newfile = $afile;
$newfile = basename $newfile;
### $newfile =~ s/\.$sessionnumber//g;
unless (cp $afile , "$inboundout/$newfile") {
Println ("ERROR : Unable to copy $afile to $CONFIG_PARAMS->{'INBO
UND_OUT_DIR'}, $!");
return;
}
}
}
if (scalar @commfiles > 0) {
COMM: for my $afile (@commfiles) {
sleep 1;
# special wrap 80 processing for outbound files
if ($afile =~ /$CONFIG_PARAMS->{'WRAP'}/) {
open (COMMFILE , "< $afile");
open (COMMFILETMP , "> $afile.tmp");
my $delim = "\n";
while (<COMMFILE>) {
my $line = $_;
$line =~ s/\r|\n//g;
while ( length $line > 80 ) {
print COMMFILETMP substr( $line, 0, 80 ) . $delim;
$line = substr( $line, 80, length $line );
}
print COMMFILETMP $line . $delim;
}
close COMMFILETMP;
close COMMFILE;
unless (cp $afile . '.tmp' , $afile) {
Println ("ERROR : Unable to move $afile . '.tmp' to $afile");
return;
}
}
unless (cp $afile , $CONFIG_PARAMS->{'OUTBOUND_OUT_DIR'}) {
Println ("ERROR : Unable to copy $afile to $CONFIG_PARAMS->{'OUTBOU
ND_OUT_DIR'}");
return;
}
}
}
if (scalar @excpfiles > 0 ) {
Println ("ERROR : Session number $sessionnumber had errors, please check A
I logs for more detailed information");
# Send email here.
$error_flag = 'y';
Println (@output);
}
return $sessionnumber;
}
sub FindSessionFiles ($$) {
# protect the current value of $ARG ($_)
local $ARG;
# get the directory path
my $dir = shift @ARG;
# get the session number
my $sessionnumber = shift @ARG;
my $caller = (caller 1) [3];
# open a directory handle
unless ( opendir DIR, $dir ) {
# failure
Println "ERROR : Unable to open AI directory $dir \n";
return;
} # end of unless opendir DIR, $dir
# get all the entries in the directory
my @entries = readdir DIR;
# close the directory handle, no longer needed
closedir DIR;
# get the files
@entries = grep /$sessionnumber/, @entries;
# prepend the directory
@entries = map $dir . $ARG, @entries;
# return the array
return @entries;
} # end of sub FindSessionFiles ($$)
# print with a new line
sub Println($) {
my $leader = strftime('%Y/%m/%d %H:%M:%S', localtime) . ' [' . sprintf('%6s',
$PID) . '] ';
print $leader . $_[0] . "\n";
print LOGS $leader . $_[0] . "\n";
}

You might also like