#!/usr/bin/perl

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

my @search;
my $cibTime;
my $key; my $value;
my %lastVal=();
my %currVal=();
my $previous;
my $p;
my $filterDouble=0;
my $showFormerValues=0;
my $formerString="";
my $version=0;
my $help=0;
my $Version="0.2.2022.02.10";

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

init();

if ( $help ) {
   printf "SAPHanaSR-filter --search=\"pattern\" --filterDouble --showFormerValues\n";
   printf "SAPHanaSR-filter { --help | --version }\n";
   printf "";
   exit 0;
}

if ( $version ) {
   printf "%s\n", $Version;
   exit 0;
}

if ( scalar(@search) eq 0 ) {
      @search = ( "." );
}

#  
while ( <> ) {
    if ( /(.*)="(.*)"/ ) {
        # lines like: a/b/c="d e f"
        $key="$1"; $value="$2";
        if ( $key eq "Global/global/cib-time" ) {
            #
            # new cib reached
            # now list all "lost" entries, if their key was matching the serach criteria
            #
            foreach my $search ( @search ) {
                # list empty keys for filterDouble, if value was in list of last keys
                foreach my $lastKey ( keys(%lastVal)) {
                    if ( ($lastKey =~ $search) && (! defined $currVal{$lastKey})) {
                        if ( $filterDouble ) {
                            if ( $showFormerValues  ) {
                                $formerString=sprintf("; (%s)", $lastVal{$lastKey});
                            } else {
                                $formerString="";
                            }
                            printf "%s; %s=%s%s\n", $cibTime, $lastKey, "", $formerString;
                        }
                        delete $lastVal{$lastKey};
                    }
                }
		    }
            $cibTime=$value;
            %currVal = (); # empty current Hash
            $currVal{$key} = $cibTime;
        } else {
            $currVal{$key} = $value;
            #
            # first list all new entries, if matching the serach criteria
            #
            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;
                    }
                }
            }
        }
    }
}
