#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: andrius $
#$Date: 2017-05-30 14:51:03 +0300 (An, 30 geg. 2017) $ 
#$Revision: 5376 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/cif_tags_in_list $
#------------------------------------------------------------------------------
#*
#* Parse a CIF dictionary file and generate a list of tags
#* with additional information about their loop constraints.
#* The first column signals whether the tag must be declared
#* in a looped list ( '0' -- no, '1' -- 'yes', 2 -- 'both'),
#* the second column signals whether the tag must be present
#* in the loop structure containing other items of the
#* designated _category, ( '0' -- no, '1' -- yes) and
#* the third column is the tag name itself.
#*
#* USAGE:
#*    $0 --options input1.dic input*.dic
#**

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

my $use_parser = 'c';

#* OPTIONS:
#*   --use-perl-parser
#*                     Use development CIF parser written in Perl.
#*   --use-c-parser
#*                     Use faster C/Yacc CIF parser (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" },
    "--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)';

foreach my $filename (@ARGV) {
    my ( $data ) = parse_cif( $filename, { parser => $use_parser } );

    for my $dataset (@$data) {
        my $values = $dataset->{values};

        next unless defined $values;
        next unless defined $values->{_name};

        my @name = @{$values->{_name}};
        my ( $inlist, $list_mandatory ) = ( 0, 0 );
        if( defined $values->{_list} ) {
            if( $values->{_list}[0] eq "yes" ) {
                $inlist = 1;
            } elsif( $values->{_list}[0] eq "both" ) {
                $inlist = 2;
            }
        }
        if( defined $values->{_list_mandatory} and
            $values->{_list_mandatory}[0] eq "yes" ) {
            $list_mandatory = 1;
        }
       for my $name (@name) {
           print "$inlist\t$list_mandatory\t$name\n";
       }
    }
}
