#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Revision: 5467 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/msg_parse $
#$Date: 2017-07-18 18:16:38 +0300 (An, 18 liep. 2017) $
#$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;

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 'cod-tools version ',
                              $COD::ToolsVersion::Version, "\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($0, $ARGV, undef, "unable to parse error message '$msg'", undef);
    }

    return;
};


