#!/usr/bin/perl

use Getopt::Long;
use File::Copy;

use lib "/mt/apps/gpms/DateTrans/lib";
use DateTrans qw(monName monNumber timeFormat AmPmFormat dayNumbeFormat);

use lib "/mt/apps/gpms/DateTrans/lib";
use  xsdxsltcheck qw(xsdcheck xsltcheck FGDC2MRFcheck xsl4mrf2fgdccheck);


use File::Basename;

$correct=substr($0,2);



sub usage() {
    print "Usage: perl $correct [-listfile=<listfile>][-directory=<directory>][-product=<product>]\n";
    print "Example:\n";
    print "perl $correct -listfile=list.txt -directory=/dir/subdir -product=TLFE2007\n";
    print"where list.txt is the filename of the list file containg the names of the files to validated,\n";
       print "/dir/subdir is the directory containing the files to be validated\n";
       print "and TLFE2007 is the product.\n";
   print "Please also take note of the following shortcut for the directory variable: putting mtdata for the directory name\n";
  print "will yield the /mtdata/geo/gpms/input/mif directory.\n"; 
}
%gOptions;
&GetOptions(\%gOptions, qw(listfile=s directory=s  product=s));
unless ($gOptions{listfile} and $gOptions{directory} and $gOptions{product}) {
        usage();
        exit(1);
};
#---------------------------------------------file mangement section----------------------------------------------------------------------------------------
$Infile = $gOptions{listfile};
chomp($Branch = $gOptions{product});
chomp($indir= $gOptions{directory});

if ($indir eq "mtdata"){
$indir = "/mtdata/geo/gpms/input/mif";
}elsif ($indir eq "nfs"){
$indir ="/nfs/gpms/input/mif";
}elsif ($indir eq "input"){
$indir ="/home/mccre004/input_files";
};

$root="/mt/apps";
$rootout="/mtdata/geo";
#$indir="$rootout/gpms/input/mif";
$inlist="$rootout/gpms/input/list";
$outputdir ="$rootout/gpms/output/AFF";
$errlogdir ="$rootout/gpms/log";
$combolist = $inlist . "/" . $Infile;
$stylesheet ="$root/gpms/MIF2AFF/MIF2MRF.xsl";
$FGDCdir="$rootout/gpms/output/fgdc";
$AFFStyleSheet = "/mt/apps/gpms/MIF2AFF";
#------------------------------------------------------error trap--------------------------------------------------------------------------------------------

if (-e $indir){
print "Please Wait, Now processing the files ----------------------------------------------\n\n";
#print "for the $indir directory\n";
}else{
print "The directory $indir  could not be found! \n";
print "Please make sure the pathname, $indir, was spelled correctly and that it exists.\n";
exit(1);
};

if (-s $indir){
}else{
print "The directory $indir does not contain any files to be validated! \n";
exit(1);
};

if (-e $combolist){
#print "Please Wait, Now processing -----------------------------------------------\n";
}else{
print "The file $combolist could not be found! \n";
print "Please make sure the file, $Infile,  was spelled correctly and that the file is in the $inlist directory.\n";
exit(1);
}; 

#making sure the file list contains something
if (-s $combolist){
#comment
}else{
print "The file $combolist does not contain the list of files to be validated! \n";
print "Please make sure the file, $Infile,  contains a list of files to be validated.\n";
exit(1);
};

#making sure the AFF stylesheet exists
if (-e $AFFStyleSheet ){
}else{
print "The stylesheet MIF2AFF, located in the $AFFStyleSheet directory, could not be found!\n";
print "This stylesheet is needed to transform the MIF files into AFF files\n";
print "The script $0 is now termanating!\n";
exit(1);
};

if (-e $stylesheet ){
}else{
print "The stylesheet MIF2MRF, located in the $stylesheet directory, could not be found!\n";
print "This stylesheet is needed to validate the MIF files\n";
print "The script $0 is now termanating!\n";
exit(1);
};


#------------------------------------getting the date---------------------------------------------------------------------------------------------------------
my $now=`date`;
$mon = substr($now,4,3);
$day=substr($now,8,2);
$year =substr($now,24,4);
#$time= substr($now,11,5);
$hour = substr($now,11,2);
$min = substr($now,14,2);
my $daynum= dayNumbeFormat($day);

#converting the date to a number
my $monnum = monName($mon);
my $monnum =monNumber($mon);
my $Month_Number = monNumber($mon);

$dateString =$year . $monnum . $daynum . $hour . $min;

$time = $hour . ":" . $min;
$oldhour = $hour;
my $newhour =timeFormat($hour);
my $ampm =AmPmFormat($oldhour);


$newtime = $newhour . ":" . $min . " $ampm";

#-----------------------------------------------------making the directories---------------------------------------------------------------------------
#$mrf ="MRF_$Branch";
$newdir=$Branch . $dateString;
#system "mkdir $outputdir/$newdir";
$success = system "mkdir $outputdir/$newdir";
if ($success >0){
print "\$success = $success\n";
print "There is not enough room on the disk. $0 is now termanating!\n";
exit(1);
};

$newerrdir ="$errlogdir/$newdir";
system "mkdir $newerrdir";
chmod 0777, "$outputdir/$newdir";
chmod 0777, "$newerrdir";
system "mkdir $FGDCdir/$newdir";
chmod 0777, "$FGDCdir/$newdir";

#---------------------------------------------------------------------Counter variable Management section------------------------------------------
$badfile_counter2=0;
$goodfile_counter=0;
$countere=0;
$flag_array_counter=0;
#----------------------------------------------------------Saxon-----------------------------------------------------------------------------------------
$ENV{'PATH'} = "/opt/saxon_lib/jdk1.5.0_11/bin:/mt/apps/gpms/output/mrf$newerrdir:$ENV{'PATH'}";#goodone
$cp = "CLASSPATH";#goodone
$ENV{$cp} = "/opt/saxon_lib/jdk1.5.0_11/lib/:/opt/saxon_lib/saxon9/saxon9sa.jar:/opt/saxon_lib/saxon9/saxon9-jdom.jar:/opt/saxon_lib/saxon9/:/opt/saxon_lib/saxon9/saxon9.jar:$ENV{$cp}";
#---------------------------------------redirecting standard output section------------------------------------------------------------------------------


#redirecting STDOUT because the Saxon program automatically sends all its output to STDOUT
# Saving a copy of STDOUT as SAVEOUT so that items can still be printed to the screen
open (SAVEOUT,">&STDOUT");

#-----------------------------------------Filename array creation section------------------------------------------------------------------------------
#open a filehamdle for the file containing the filenames
open INPUTFILE, "$combolist";
while(<INPUTFILE>){
$slashpos= rindex($_, "/");
# print "\$slashpos = $slashpos\n";
if ($slashpos < 0){
$flagpos=index($_,"xml");
$flagless=substr($_, 0,$flagpos+3);
$flag=substr($_,$flagpos+4);
$ppos=index($flag,"p");
$filename="$indir/$flagless";

if ($ppos>=0){
$filename[$first_counter]="$indir/$flagless";
$flagarray[$first_counter]="$flag";
#putting the posting flag in a hash array
$flaghash{$filename}="$flag";
$first_counter++;
} else{
$noflagarray[$flag_array_counter]=$_;
$flag_array_counter++;
};


}elsif($slashpos > 0){


my $dirnameA = dirname $_;

$slash_directory_name[$slash_directory_name_counter]=$dirnameA;
$slash_directory_name_counter++;

$infile=$_;
$dotloc=rindex($infile,".")+4;
$slashfilename = substr($infile,0,$dotloc);
$flagpos=index($infile,"xml");
$flag=substr($infile,$flagpos+4);
$flaglen =length($flag);
$ppos=index($flag,"p");
$flag2=substr($flag,0,1);

if ($ppos>=0){
$filename[$first_counter]=$slashfilename;
$flagarray[$first_counter]="$flag";
$flaghash{$slashfilename}="$flag";
$first_counter++;
} else{
$noflagarray[$flag_array_counter]=$slashfilename;
$flag_array_counter++;
};

};
};
#-------------------------------------------validating the incoming MIF File--------------------------------------------------------------------------------------
print "Now validating the MIF files-------------------------------------------------------------------------------------\n";
$filecounter=1;
foreach $filename (@filename){
chomp($filename);
#setting up the variables necessary to make the the finalfilename variable, which is needed to make the MRF file
$slashpos= rindex($filename, "/") +1;
$newfilename = substr($filename,$slashpos);
$dotlocation = index($newfilename, ".");
$finalfilename=substr($newfilename, 0, $dotlocation);
#setting up the TMP and MRF filenames
$MRF = $finalfilename . ".mrf";
$TMP = "TMP" . "-" . $newfilename;
$valcounter++;


open STDOUT,">$TMP";

#making the error file and redirecting the STDERR to the error file
#have a new $dotlocation variable because the $filename and $newfilename variables are different lengths
$errmain= substr($newfilename, 0, $dotlocation);
$errfile = $errmain .".err";

open STDERR, ">$errfile";

#starts schema aware transformation. This will cause the transformation to fail if the document is invalid
# the -vw option indicates that validation errors found when validating the result tree should be treated as warnings only.
#This option is available only with the Saxon-SA version of the command, com.saxonica.Transform.
printf SAVEOUT "$filecounter: Now validating $filename\n";
$filecounter++;
my @csax = "java com.saxonica.Transform -vw $filename $root/gpms/xsl/MIF2MRF.xsl";
my $mif = system(@csax);

close STDERR;
#print SAVEOUT "\$mif = $mif\n";
if ($mif > 0) {
print"error\n";
$badMif[$badMif_counter] = $errfile;
$badMif_counter++;
#copy(
system "chmod 777 $TMP";

if (-e $TMP ){
unlink $TMP;
system "rm $TMP";
};

}else{
$goodmif[$goodMif_counter]=$filename;
$goodMif_counter++;
};
#print SAVEOUT "\n";
};
print SAVEOUT "\n";

#------------------------------------------Transforming MIF to AFF-----------------------------------------------------------------------------------------------------
print SAVEOUT "Now transforming the MIF files into AFF files ---------------------------------------------------------------------\n";
$valcounter=0;
$filecounter =1;

foreach $goodmif (@goodmif){
chomp($goodmif);
#setting up the variables necessary to make the the finalfilename variable, which is needed to make the MRF file
$slashpos= rindex($goodmif, "/") +1;
$newfilename = substr($goodmif,$slashpos);
$dotlocation = rindex($newfilename, ".");
$finalfilename=substr($newfilename, 0, $dotlocation);
#setting up the TMP and MRF filenames

#if (-e $TMP){
if ($valcounter >0){
close STDERR;
system "rm $AFFOutFile";
};

$AFFOutFile="AFF" . "-" . $finalfilename . ".xml" ;

open STDOUT,">$AFFOutFile";

#making the error file and redirecting the STDERR to the error file
#have a new $dotlocation variable because the $goodmif and $newfilename variables are different lengths
$errmain= substr($newfilename, 0, $dotlocation);
$errfile = $errmain . ".err";

open STDERR, ">$errfile";

#starts schema aware transformation. This will cause the transformation to fail if the document is invalid
# the -vw option indicates that validation errors found when validating the result tree should be treated as warnings only.
#This option is available only with the Saxon-SA version of the command, com.saxonica.Transform.
printf SAVEOUT "$filecounter: Now transforming $goodmif from the MIF format to the American Fact Finder Format(AFF)\n";
$filecounter++; 
my @csax = "java com.saxonica.Transform -vw $goodmif $root/gpms/MIF2AFF/MIF2AFF.xsl";
my $affResult  = system(@csax);
#opening fielhandles so that *.xsd will be replaced by Repository.xsd
#basically replicating the sed command 
close STDERR;

if ($affResult > 0) {
$badfile[$badfile_counter] = $errfile;
$badfile_counter++;
#copy(
unlink $AFFOutFile;

if (-e  $TMP){
unlink $TMP;
};

print SAVEOUT "-------------------------------------\n\n";

}else{
unlink $errfile;
$goodfile[$goodfile_counter]= $AFFOutFile;
#print SAVEOUT "\$MRF = $MRF\n";
#determine if the files are to be posted. If the position of n, as indicated by $npos is -1, then the file is not to be posted
$goodflaghash{$AFFOutFile}=$flaghash{$goodmif};
$npos=index($flaghash{$goodmif},"n");

$xmlDotPos=rindex($AFFOutFile, ".");
$CleanAFFOutFileParta= substr($AFFOutFile,0,$xmlDotPos);
$CleanAFFOutFile = $CleanAFFOutFileParta . "_Good.xml";

open CLEANAFF, "> $CleanAFFOutFile";
open INAFF, "<$AFFOutFile";

while (<INAFF>){

if ($_ =~ /Publication_Date/ ){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$PubyearLength=$leftBracPos-$rightBracPos;
$Pubyear= substr($_,$rightBracPos,$PubyearLength);
print CLEANAFF "$Pubyear";
}elsif($_ =~ /Metadata_File_Identifier/){
#print SAVEOUT "\$_ = $_\n";
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$File_Id_Length=$leftBracPos-$rightBracPos;
$File_Id=substr($_,$rightBracPos,$File_Id_Length);
print CLEANAFF "$File_Id";
}elsif($_ =~ /<document/){
$HyPos =index($_, "-");


#using the position of the - to differentiate the document and document-type-ref elements
if ($HyPos >12){
$xmlns="xmlns=\"http://factfinder.census.gov/2009/metadata\"";
$xmlSchema="xsi:schemaLocation=\"http://factfinder.census.gov/2009/metadata\"";
print CLEANAFF $_  . $xmlns . "\n" . $xmlSchema
};

}elsif($_ =~ /Title/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$Title_Length=$leftBracPos-$rightBracPos;

$Title=substr($_,$rightBracPos,$Title_Length);
print CLEANAFF "$Title";
}elsif($_ =~ /Abstract/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$Abstract_Length=$leftBracPos-$rightBracPos;
$Abstract=substr($_,$rightBracPos,$Abstract_Length);
print CLEANAFF "$Abstract";
}elsif($_ =~ /Ordering_Instructions/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$Order_Length=$leftBracPos-$rightBracPos;
$Order=substr($_,$rightBracPos,$Order_Length);
print CLEANAFF "$Order";
}elsif($_ =~ /Network_Resource_Name/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$Net_Length=$leftBracPos-$rightBracPos;
$Net=substr($_,$rightBracPos,$Net_Length);
print CLEANAFF "$Net";
}elsif($_ =~ /Metadata_Date/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$Met_Date_Length=$leftBracPos-$rightBracPos;
$Met_Date=substr($_,$rightBracPos,$Met_Date_Length);
print CLEANAFF "$Met_Date";
}elsif($_ =~ /Theme_Keyword/){
}elsif($_ =~ /\/Theme_Keyword/){
}elsif($_ =~ /Transfer_Size/){
$rightBracPos=index($_,">")+1;
$leftBracPos=rindex($_,"<");
$TransSizeLength=$leftBracPos-$rightBracPos;
$Trans=substr($_,$rightBracPos,$TransSizeLength);
print CLEANAFF "$Trans";
}else{
print CLEANAFF "$_";
};
};

if ($npos==0){
$nopassarray[$no_pass_counter]=$goodfile[$goodfile_counter];
#print SAVEOUT "\$nopassarray[$no_pass_counter] = $nopassarray[$no_pass_counter]\n";
$no_pass_counter++;
}elsif ($npos<0){
#print SAVEOUT "npos < 0 : \$AFFOutFile = $AFFOutFile\n";
$passarray[$pass_counter]=$AFFOutFile;
#$pass_counter++;
#printf SAVEOUT"\$pass_counter = $pass_counter\n";
#printf SAVEOUT "\$passarray[$pass_counter]=$passarray[$pass_counter]\n" ;
$pass_counter++;
};#if ($npos==0){
};#if ($affResult > 0) {
};#foreach



printf SAVEOUT "\n";

#-------------------------------error handling for invalid MIF files-------------------------------------------------------------------------------------------------
foreach $badMif (@badMif){
#print SAVEOUT "\$badMif =  $badMif\n";
open BADFILEH, "$badMif";
$bad="no";
$slashpos= rindex($badMif, "/")+1;
$wheredot2 = index($badMif, ".");
$AFF_Main_name = substr($badMif,$slashpos,$wheredot2);
$name = "$AFF_Main_name.xml";
$bad ="no";
$sizebadMif = -s $badMif;
if (-s $badMif == 0){
print SAVEOUT "In the size = 0 section.\n";
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "Here!";
$countere++;
$bad ="yes";
}else{
#print SAVEOUT "\nIn the size > 0 Section(MIF Error)\n\n"; 

while(<BADFILEH>){
if ($_=~ /does not exist/){
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "not found!\n";
print SAVEOUT "not found!\n";
$countere++;
$bad ="yes";
#file not existing not being reported to the error file
}elsif (-s $badMif == 0 and $badMif2[$badMif_counter2-1] ne $name and $bad ne "yes"){
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "Here!";
#print SAVEOUT "Here!(-s $badMif == 0)";
unlink "$badMif";
$countere++;
$bad ="yes";
printf SAVEOUT "Emptyfile";
}elsif($badMif2[$badMif_counter2-1] ne $name and $bad ne "yes"){
$name = $badMif;
$badMif2[$badMif_counter2] = "$name";
$badMif_counter2++;
$bad ="no";
system "cp $badMif $newerrdir/$name";
unlink $badMif
};#if
}#s if
};#while
close BADFILEH
};#foreach
print SAVEOUT "\n\n";

#------------------------------error filehandling section for bad AFF files--------------------------------------------------------------------------------------------

foreach $badfile (@badfile){
#print SAVEOUT "in the badfile loop: \$badfile = $badfile\n";
open BADFILEH, "$badfile";
$bad="no";
$slashpos= rindex($badfile, "/")+1;
$wheredot2 = index($badfile, ".");
$AFF_Main_name = substr($badfile,$slashpos,$wheredot2);
$name = "$AFF_Main_name.xml";
#print SAVEOUT "\$name = $name\n";
$bad ="no";
if (-s $badfile == 0){
print SAVEOUT "In the size = 0 section.\n";
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "Here!";
#unlink "$badfile";
$countere++;
$bad ="yes";
}else{
#print SAVEOUT "\nIn the size > 0 Section\n\n"; 

while(<BADFILEH>){
if ($_=~ /does not exist/){
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "not found!\n";
#unlink "$badfile";
$countere++;
$bad ="yes";
#file not existing not being reported to the error file
}elsif (-s $badfile == 0 and $badfile2[$badfile_counter2-1] ne $name and $bad ne "yes"){
$not_exist[$countere]= "$name";
printf  MRFOUTPUT "Here!";
unlink "$badfile";
$countere++;
$bad ="yes";
printf SAVEOUT "Emptyfile";
}elsif($badfile2[$badfile_counter2-1] ne $name and $bad ne "yes"){
#print SAVEOUT "\nIn the ne=0  Section\n\n";

$name = $badfile;
$badfile2[$badfile_counter2] = "$name";
$badfile_counter2++;
$bad ="no";
#print SAVEOUT "Error file being copied!\n";
system "cp $badfile $newerrdir/$name";
unlink $badfile
};#if
}#s if
};#while
close BADFILEH
};#foreach

#printf SAVEOUT "Done with the error file handling!!!!!!!!!!!!!!!!!!!!\n";
#--------------------------------------final variables counting section--------------------------------------------------------------------------------
$total = $badfile_counter2 + $pass_counter+$no_pass_counter ;
$newtotal=$total+$countere+$flag_array_counter;
$pergood = (($pass_counter )/$total) *100;
$perbad = (($badfile_counter2)/$total) *100;
#-------------------------------working with the log file----------------------------------------------------------------------------------------------
#This is the introductary paragraph.
open LOG, ">$errlogdir/$newdir/$newdir.log";
printf LOG "This log details the transformation and validation results of the $newtotal mif files contained in \n the  $combolist list file.\n";

if ($slash_directory_name_counter >0){
printf LOG "\nThese files can be found in the following directories:\n";
printf LOG "1. $indir (This is the directory given at the command line)\n";
$slash_directory_nameb =2;
@slash_directory_name=sort(@slash_directory_name);

foreach $slash_directory_name (@slash_directory_name){

$slash_directory_nameca =$slash_directory_namec-1;

if ($slash_directory_name[$slash_directory_namec] ne $slash_directory_name[$slash_directory_nameca] and $slash_directory_name[$slash_directory_namec] ne $indir){
printf LOG "$slash_directory_nameb: $slash_directory_name\n";
$slash_directory_nameb++;
$slash_directory_namec++;
}elsif($slash_directory_namec == 0){
printf LOG "$slash_directory_nameb: $slash_directory_name\n";
$slash_directory_nameb++;
$slash_directory_namec++;
};#if ($slash_directory_name[$slash_directory_namec] ne $slash_directory_name[$slash_directory_nameca] and $slash_directory_name[$slash_directory_namec] ne $indir){
};#foreach $slash_directory_name (@slash_directory_name){

printf LOG "\n$0 was run on $monnum/$daynum/$year at $time ($newtime) for the $Branch product.\n\n";
}else{
printf LOG "\nThese files can be found in the $indir directory.\n";
printf LOG "It was run on $monnum/$daynum/$year at $time ($newtime) for the $Branch product.\n\n";

};#if ($slash_directory_name_counter >0){

#--------------------------indicator section-----------------------------------------------------------------------------------------------------------
if ($countere == 1) {
	printf SAVEOUT "The following file could not be found!\n";
	printf LOG "The following file could not be found!\n";
		foreach $not_exist (@not_exist){

			printf SAVEOUT "$countere: $not_exist\n";
			printf LOG "$countere: $not_exist\n"; 
		};
	printf SAVEOUT"\n";
	printf LOG "\n";		
}elsif ($countere >1) {
	printf SAVEOUT "The following files could not be found!\n";
	printf LOG "The following files could not be found!\n";
	foreach $not_exist (@not_exist){
		$notexistcounter++;
		printf SAVEOUT "$notexistcounter: $not_exist\n";
		printf LOG "$notexistcounter: $not_exist\n";
	};
	printf SAVEOUT"\n";
	printf LOG"\n";
};

$counter=1;

if ($flag_array_counter ==1){
	$flagfile="FailedFiles";
	#printf SAVEOUT "\$flagfile = $flagfile";
	open FLAGLIST, ">$errlogdir/$newdir/$flagfile";
        #to the screen
	printf SAVEOUT "The following file does not have a FGDC posting flag and therefore can not be processed. Please put a space and a p (for post) or np (for not post) after the filename. A list containg this file can be found in $flagfile in the \n";

	printf SAVEOUT "$errlogdir/$newdir directory.\n";
	#to the logfile
	printf LOG  "The following file does not have a FGDC posting flag and therefore can not be processed. Please put a space and a p (for post) or np (for not post)
after the filename.\n";
	printf LOG  "A file called $flagfile has a list with this file in it for you to flag.\n";	
	foreach $noflagarray (@noflagarray){
		printf SAVEOUT "$counter: $noflagarray\n";
		printf LOG "$counter: $noflagarray\n"; 
		printf FLAGLIST "$noflagarray";
	};
close FLAGLIST;	
}elsif ($flag_array_counter >1) {
	$flagfile="FailedFiles";
	 #printf SAVEOUT "\$flagfile = $flagfile\n";
	 open FLAGLIST, ">$errlogdir/$newdir/$flagfile";
#	#to the screen
	 printf SAVEOUT "The following files do not have a FGDC posting flag and therefore can not be processed. Please put a space and a p (for post) or np (for not post)
#after the filename. A list of these files can be found in $flagfile in the \n";
	 printf SAVEOUT "$errlogdir/$newdir directory.\n";
	 #to the logfile
	 printf LOG  "The following files do not have a FGDC posting flag and therefore can not be processed. Please put a space and a p (for post) or np (for not post)
after the filename. A list of these files can be found in $flagfile in the $errlogdir/$newdir directory.\n";
	foreach $noflagarray (@noflagarray){ 
			printf SAVEOUT "$counter: $noflagarray";
			printf LOG "$counter: $noflagarray";
			printf FLAGLIST "$noflagarray";
			$counter++;
		};
	close FLAGLIST;
	printf SAVEOUT"\n";
	printf LOG"\n";
};


if ($badMif_counter >0 ){
$badMif_counter_counter=1;
print  SAVEOUT "$badMif_counter files are invalid MIF files. These files WERE NOT TRANSFORMED into AFF files. Please see the error files at $errlogdir/$newdir\n";
print LOG "$badMif_counter files are invalid MIF files. These files WERE NOT TRANSFORMED into AFF files. Please see the error files at $errlogdir/$newdir\n";
foreach $badMif (@badMif){ 
print  SAVEOUT "$badMif_counter_counter: $badMif\n";
print LOG "$badMif_counter_counter: $badMif\n";
$badMif_counter_counter++;
};
print SAVEOUT"\n";
print LOG "\n";
};


if ($badfile_counter2 >0){
$badfile2_Post_counter=1;
print  SAVEOUT "$badfile_counter2 files have errors in them. Please see the error files at  $errlogdir/$newdir\n";
print LOG "$badfile_counter2 files have errors in them. Please see the error files at $errlogdir/$newdir\n";
 
foreach $badfile2 (@badfile2){
print SAVEOUT "$badfile2_Post_counter: $badfile2\n";
print LOG "$badfile2_Post_counter: $badfile2\n";
$badfile2_Post_counter++;

};
};

if ($no_pass_counter >0){
$AFFNoPostCounter=1;
print  SAVEOUT "$no_pass_counter MIF files that WERE NOT POSTED to the GOS server were transformed into AFF files.  \n";
print  SAVEOUT "Please see them at $outputdir/$newdir\n";
print LOG "$no_pass_counter MIF files that WERE NOT POSTED to the GOS server were transformed into AFF files.  \n";
print LOG "Please see them at $outputdir/$newdir\n";
foreach $nopassarray(@nopassarray){
print SAVEOUT "$AFFNoPostCounter: $nopassarray\n";
print LOG  "$AFFNoPostCounter: $nopassarray\n"; 
$AFFNoPostCounter++;
$dotLoc=index($nopassarray,".");
$preGood=substr($nopassarray,0,$dotLoc);
$goodfile=$preGood ."_Good.xml";
system "cp $goodfile $nopassarray";
system "cp $nopassarray  $outputdir/$newdir";
unlink $nopassarray;
system "chmod 777 $goodfile";
};
print SAVEOUT"\n";
print LOG "\n";
};

if( $pass_counter>0){

print  SAVEOUT "$pass_counter MIF files that WERE POSTED to the GOS server  were transformed into AFF files  \n";
print  SAVEOUT "Please see them at $outputdir/$newdir\n";
print LOG "$pass_counter MIF files that WERE POSTED to the GOS server  were transformed into AFF files  \n";
print LOG  "Please see them at $outputdir/$newdir\n";
$AFFGoodCounter=1;
foreach $passarray (@passarray){
print SAVEOUT "$AFFGoodCounter: $passarray\n";
print LOG "$AFFGoodCounter: $passarray\n";
$AFFGoodCounter++;
$dotLoc=index($nopassarray,".");
$preGood=substr($nopassarray,0,$dotLoc);
$goodfile=$preGood ."_Good.xml";
system "cp $goodfile $nopassarray";


system "cp $passarray  $outputdir/$newdir";
system "chmod 777 $goodfile";
unlink $passarray;
};
print SAVEOUT "\n";
print LOG  "\n";
};


system "rm AFF-*";
system "rm TMP-*";

print SAVEOUT "Please see the log file at $newerrdir.\n";
print LOG "Please see the log file at $newerrdir.\n";

if ($badfile_counter2>0 or $flag_array_counter>0){
exit(2);
}else{
exit(0);
};
