#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-07-30 20:10:23 +0300 (Fri, 30 Jul 2021) $
#$Revision: 8841 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.0/tools/ddl1_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 '.*?' 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 '.*?' is of/value '<value>' is of/;
    $line =~ s/value '.*?' is not/value '<value>' is not/;
    $line =~ s/values \[.*?\] appear/values [<values>] appear/;
    $line =~ s/enumeration values \[.*?\]/enumeration values [<values>]/;
    $line =~ s/times as \[.*\]/times as \[.*\]/;

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

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

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

    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/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/;
    }
    print $line;
}
