#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Revision: 8738 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.0/scripts/msg_parse $
#$Date: 2021-04-28 19:35:53 +0300 (Wed, 28 Apr 2021) $
#$Id: cif_diff 3294 2015-04-22 10:12:45Z andrius $
#------------------------------------------------------------------------------
#*
#* Parse COD error messages and output the results in the desired format.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::UserMessage qw( parse_message note );
use COD::ToolsVersion qw( get_version_string );

my $ignore_comments = 0;
my @dsv_print_order = ( qw(
                            program
                            filename
                            line_no
                            column_no
                            add_pos
                            err_level
                            message
                        ) );
my $print_options = {
    'format'    => 'dsv',
    'delimiter' => "\t",
    'dsv_order' => \@dsv_print_order
};

#* OPTIONS:
#*   -d, --delimiter "\t"
#*                     Delimiter that is used to separate fields in the
#*                     delimiter-separated values (dsv) output format
#*                     (default: "\t").
#*   --dsv
#*                     Output results in delimiter-separated values (dsv)
#*                     format (default).
#*   --ignore-comments
#*                     Ignore lines starting with a number symbol ('#').
#*   --process-comments
#*                     Treat lines starting with a number symbol ('#') as
#*                     error messages (default).
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "-d,--delimiter"     => \$print_options->{delimiter},
    "--dsv"              => sub { $print_options->{format} = 'dsv' },
    "--ignore-comments"  => sub { $ignore_comments = 1 },
    "--process-comments" => sub { $ignore_comments = 0 },

    '--options'      => sub { options; exit },
    '--help,--usage' => sub { usage; exit },
    '--version'      => sub { print get_version_string(), "\n"; exit }
);

my $msg;
while (<>) {
    next if $ignore_comments && $_ =~ /^\s*#/ ;

    if ( !defined $msg ) {
        $msg = $_;
    } elsif ( /^ / && $msg =~ /[^ ].*:\s*$/m) {
        $msg .= $_;
    } else {
        print_parsed_msg($msg, $print_options);
        $msg = $_;
    }

    if (eof) {
        print_parsed_msg($msg, $print_options);
    }
}

sub print_parsed_msg
{
    my ($msg, $options) = @_;

    my $format = $options->{format};
    my $delim  = $options->{delimiter};
    my $order  = $options->{dsv_order};

    my $parsed_msg = parse_message( $msg );
    if ( defined $parsed_msg ) {
        if ( $format eq 'dsv' ) {
            print join $delim, map { ( defined $parsed_msg->{$_} ) ?
                                               $parsed_msg->{$_} :
                                               '' } @{$order};
            if ( defined $parsed_msg->{line_content} ) {
                print "\t\n$parsed_msg->{line_content}"
            } else {
                print "\n";
            };
        }
    } else {
        chomp($msg);
        note( {
            'program'  => $0,
            'filename' => $ARGV,
            'message'  => "unable to parse error message '$msg'"
        } );
    }

    return;
};


