#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-06-13 18:19:21 +0300 (Mon, 13 Jun 2022) $
#$Revision: 9329 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.1/tools/ddlm_summary_filter $
#------------------------------------------------------------------------------
#*
#* Replace variable parts of the DDLm-based validation messages with
#* placeholder values.
#*
#* USAGE:
#*    $0 --options messages1.lst messages*.lst
#**

use strict;
use warnings;

use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::ToolsVersion;

my $replace_data_names = 1;

#* OPTIONS:
#*
#*   --replace-data-names
#*                     Replace data names with placeholder values. Default.
#*   --no-replace-data-names
#*                     Only replace the data values with placeholder values
#*                     leaving the data names intact.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '--replace-data-names'    => sub{ $replace_data_names = 1 },
    '--no-replace-data-names' => sub{ $replace_data_names = 0 },

    '--options'      => sub{ options; exit },
    '--help,--usage' => sub{ usage; exit },
    '--version'      => sub { print 'cod-tools version ',
                                 $COD::ToolsVersion::Version, "\n";
                                 exit }
);

while (my $line = <>) {
    $line =~ s/value '.*?'/value '<value>'/;
    $line =~ s/range value '.*?'/range value '<value>'/g;
    $line =~ s/value '.*?' violates/value '<value>' violates/;
    $line =~ s/value '.*?' appears/value '<value>' appears/;
    $line =~ s/value '.*?' that was/value '<value>' that was/;
    $line =~ s/value '.*?' should be/value '<value>' should be/;
    $line =~ s/value '.*?' must be/value '<value>' must be/;
    $line =~ s/value '.*?' must have/value '<value>' must have/;
    $line =~ s/value '.*?' has/value '<value>' has/;
    $line =~ s/values \[.*?\]/values [<values>]/g;

    $line =~ s/times as \[\[.*?\](, \[.*?\])*\]/times as [[<values>]]/;
    $line =~ s/times as \[[^\[].*?\]/times as [<values>]/;
    $line =~ s/appears [0-9]+ times/appears <times> times/;
    $line =~ s/appear [0-9]+ times/appear <times> times/;
    $line =~ s/ \([0-9]+ times\)//;

    $line =~ s/uncertainty value \'[(][0-9]+[)]\'/uncertainty value '<su>'/;

    $line =~ s/in range [(\[(].*?[)\]]/in range [<range>]/;

    $line =~ s/with the loop index '\d+'/with the loop index '<index>'/;

    $line =~ s/data structure position '.*?'/data structure position '<position>'/;
    $line =~ s/[0-9]+ instead of [0-9]+/<count> instead of <count>/;

    $line =~ s/scheme component '.*?'/scheme component '<component>'/;

    $line =~ s/the '.*?' symbol/the '<symbol>' symbol/;
    
    $line =~ s/parenthesis notation [(]'.*?'[)]/parenthesis notation ('<su>')/;
    $line =~ s/item [(]'.*?'[)] do not match/item ('<su>') do not match/;

    if ( $replace_data_names ) {
        $line =~ s/the '_[^ ]+?' data item/the '<data name>' data item/g;
        $line =~ s/data item \'_[^ ]+\'/data item '<data name>'/g;
        $line =~ s/data items \[.+?\]/data items [<data names>]/g;
        $line =~ s/ '[^ ]+?' category/ '<category>' category/g;
        $line =~ s/data names \[.+?\] refer/data names [<data name>] refer/;
        $line =~ s/the \[.+?\] data items/the [<data names>] data items/;

        my $mixed_category_message = 
            'data items \[<data names>\] belong to ' .
            'the \'<category>\' category, ';
        $line =~ s/(${mixed_category_message}){2,}/$1/;

        $line =~ s/the '.*?' frame/the '<data name>' frame/;
    }
    print $line;
}
