#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2019-11-18 11:08:55 +0200 (Mon, 18 Nov 2019) $ 
#$Revision: 7429 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.7/scripts/cif_parse $
#------------------------------------------------------------------------------
#*
#* Parse a CIF file.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::ShowStruct qw( showRef );
use COD::ToolsVersion;

my $use_parser = 'c';
my $fix_errors = 0;

#* OPTIONS:
#*   --use-perl-parser
#*                     Use development CIF parser written in Perl.
#*   --use-c-parser
#*                     Use faster C/Yacc CIF parser (default).
#*   --fix-syntax-errors
#*                     Try to fix syntax errors in the input CIF
#*                     files that can be corrected unambiguously.
#*   --dont-fix-syntax-errors, --no-fix-syntax-errors
#*                     Do not try to fix syntax errors in input
#*                     CIF files (default).
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '--use-perl-parser'         => sub { $use_parser = 'perl' },
    '--use-c-parser'            => sub { $use_parser = 'c' },

    '--fix-syntax-errors'       => sub { $fix_errors = 1 },
    '--dont-fix-syntax-errors'  => sub { $fix_errors = 0 },
    '--no-fix-syntax-errors'    => sub { $fix_errors = 0 },

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

@ARGV = ('-') unless @ARGV;

binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';

warn "$0:: WARNING, the current output format is considered " .
     "deprecated -- it will be changed in future releases.\n";

foreach my $filename ( @ARGV ) {

    my ( $data ) = parse_cif( $filename, { 'parser'     => $use_parser,
                                           'fix_errors' => $fix_errors } );

    for my $dataset (@$data) {
        showRef( $dataset );
    }
}
