#!/usr/bin/perl

$WinDir = $ENV{'WINDIR'};

print $WinDir, "\n";

system(cls);

$True = 1;
$False = 0;

$version = `ver`;

print $version;

if($version=~/ 4/i){
    $VerNumber = 4;
#    print "Inside Version NT4 IF statement\n";
#    print $version;
}
elsif($version=~/ 5/i){
    $VerNumber = 5 ;
 #   print "Inside Version NT5 IF statement\n";
 #   print $version;
}
else{
    $version=~s/(\t|\n|\n\n)//g;
    print "\n***** There is an emergency going on. *****\n\tThis is $version!\n\tI need to be run on NT4 or Win2K to do my thing.\n\n";
    exit 0;
}

#print "The VerNumber is : ", $VerNumber, "\n";

$InFile   = $WinDir . "\\WNT" . $VerNumber . "0PART.LOG";
$OutFile  = $WinDir . "\\WNT" . $VerNumber . "0PART.LO_";

open IN, "<$InFile", or die "Cannot open $InFile for Reading :$!";
open OUT, ">$OutFile", or die "Cannot open $OutFile for Writing :$!";

LINE: while (<IN>) {
    print COPY;

      $SystemLine = "\n\n\t $version \n";

      if($_ =~/NTLOG REPORT/i){
          print OUT $SystemLine;
          print OUT $_;
          $NTLogSection = $True;        
          next LINE;
      }
                   

      if($_ =~/Tests Aborted/i){
          print OUT $_;
          print OUT "\n--------------- Failure Summary ---------------\n";
          $NTLogSection = $False;        
          next LINE;
      }
  
      if($NTLogSection){print OUT $_; next LINE;}
      if($_=~/VAR\[SEV1]/i){push(@Failures1, $_); next LINE;}
      if($_=~/VAR\[SEV2]/i){push(@Failures2, $_); next LINE;}
      if($_=~/VAR\[SEV3]/i){push(@Failures3, $_); next LINE;}
}

foreach(@Failures1){print OUT;}
foreach(@Failures2){print OUT;}
foreach(@Failures3){print OUT;}

print OUT "\n\n";

print "\n\n\tParsing Completed\n";

if ($VerNumber == 5){
    $InFile2 = $WinDir . "\\WNT40PART\.LO_";
    $InFile1 = $WinDir . "\\WNT50PART\.LO_";
    $OutFile2 = $WinDir . "\\MERGE_WNT50PART\.LOG";

    open IN2, "<$InFile2", or die "Cannot open $InFile for Reading :$!";
    open IN1, "<$InFile1", or die "Cannot open $InFile for Reading :$!";
    open OUT2, ">$OutFile2", or die "Cannot open $OutFile for Writing :$!";

    while(<IN1>){print OUT2;}
    while(<IN2>){print OUT2;}
}
close Out2;
print "\n\n\tThe following files have been written to $WinDir \n";
print "\n\t $OutFile \n\t $OutFile2 \n";
if($VerNumber == 4){system(Premerge.bat);}
if($VerNumber == 5){system(merge.bat);}