
#!/usr/bin/perl

use strict;
#use Data::Dumper;
my $src;
my $dst;


my @currentMeasureLines = ();
my $data = {};
my $HeaderLine = '';
my $tmpLine;

sub getKey {
	my (@data) = @_;
	my ($scandate,$rest) = split /T/,$data[1],2;
	
	return $scandate.$data[2].$data[3];
}

sub isCompliant {
	my ($record) = @_;
	my $returnValue = 0;
	
	if($record->[4].$record->[5] eq "truefalse") {
		$returnValue = 1;
	}
	
	return $returnValue;	
}

# read all data in a hash
if((scalar(@ARGV) == 2) and ($ARGV[0] ne '') and ($ARGV[1] ne '')) {
	open($src,$ARGV[0]) or die "File >$ARGV[0]< not found.";
	open($dst,"> ".$ARGV[1]) or die "Can not create output >$ARGV[1]< file.";
	while (<$src>) 
	{
	  if($_ !~ /^#/) {
	  	if($HeaderLine eq '') {
	  		$HeaderLine = $_;
	  	}
	  	else {
		  $tmpLine = $_;
		  my @fields = split /";"/ , $_;
		  my $currentKey = getKey(@fields);
		  if(!defined $data->{$currentKey}) {
		  	$data->{$currentKey} = ();
		  }
		  push @{$data->{$currentKey}},[@fields];
	  	}
	  }
	}
	close($src);
	
	# create new file
	# remove wrong records
	print $dst $HeaderLine;
	foreach my $key (sort keys %{$data}) {
		if(scalar(@{$data->{$key}}) > 1) {
			foreach my $record (@{$data->{$key}}) {
				if(!isCompliant($record)) {
					print $dst join('";"',@{$record});
				}	
			}
		}
		else {
			print $dst join('";"',@{$data->{$key}->[0]});
		}
	}
	close($dst);
}
else {
	print "Syntax:\n";
	print " filter_sap_itc_files.pl <input file name> <output file name>\n";
}


