#!/usr/bin/perl

#  <relpath>="<value>"
use strict;
use Getopt::Long;

my @search;
my $cibTime;
my $key; my $value;
my %lastVal;
my $previous;
my $p;
my $filterDouble=0;
my $showFormerValues=0;
my $formerString="";
my $version=0;
my $help=0;
my $theVersion="0.1.2019.08.30.1";

sub init()
{
    my $result = GetOptions ("search=s" => \@search,
                          "filterDouble" => \$filterDouble,
                          "showFormerValues" => \$showFormerValues,
                          "version" => \$version,
                          "help" => \$help,
         );
   return 0;
}

init();

#  
while ( <> ) {
    if ( /(.*)="(.*)"/ ) {
        # lines like: a/b/c="d e f"
        $key="$1"; $value="$2";
        if ( $key eq "Global/global/cib-time" ) {
            $cibTime=$value;
        } else {
            foreach my $search ( @search ) {
                if ( $key =~ $search ) {
                    if ( defined $lastVal{$key} ) {
                        $previous=$lastVal{$key};
                        if ( $previous eq $value ) {
                            $p=1;
                        } else {
                            # new value, store and mark line to be printed
                            $lastVal{$key}=$value;
                            $p=0;
                        }
                    } else {
                            # new value and empty hash slot, store and mark line to be printed
                            $previous="";
                            $lastVal{$key}=$value;
                            $p=0;
                    }
                    if ( $showFormerValues  && $previous ) {
                       $formerString=sprintf("; (%s)", $previous);
                    } else {
                       $formerString = "";
                    }
                    if ( (! $filterDouble ) || ( $p == 0 )) {
                        printf "%s; %s=%s%s\n", $cibTime, $key, $value, $formerString;
                    }
                }
            }
        }
    }
}
