#! /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.6/scripts/cif_tags_in_list $
#------------------------------------------------------------------------------
#*
#* Parse a DDL1 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::DDL::DDL1 qw( get_data_type );
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';
my $exclude_category_names = 0;

#* OPTIONS:
#*
#*   --exclude-category-names
#*                     Exclude categories from the output.
#*   --no-exclude-category-names
#*                     Include categories in the output (default).
#*
#*   --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(
    '--exclude-category-names'    => sub { $exclude_category_names = 1 },
    '--no-exclude-category-names' => sub { $exclude_category_names = 0 },

    '--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 if !defined $values;
        next if !defined $values->{_name};

        my $data_type = get_data_type( $dataset );
        if ( $exclude_category_names ) {
            next if !defined $data_type;
            next if $data_type eq 'null'; 
        } 

        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";
       }
    }
}
