#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2022-06-09 18:32:06 +0300 (Thu, 09 Jun 2022) $
#$Revision: 9325 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.0/scripts/symops $
#------------------------------------------------------------------------------
#*
#* Print out symmetry operators for a given space group, or print out
#* those symmetry operators that belong to the group, while diagnosing
#* as errors those operators that do not belong to the group.
#*
#* USAGE:
#*   $0 --options P212121
#*   $0 --options P212121 -- -y,-x,z -x,y,z
#*   $0 --hall -- '-P 2 2'
#*   $0 --hall -- '-P 2 2' "-y,-x,z" "-x,y,z"
#**

use strict;
use warnings;
use COD::CIF::Data;
use COD::Spacegroups::Symop::Parse qw(
    symop_string_canonical_form
);
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage );
use COD::ToolsVersion qw( get_version_string );

my $quiet = 0; # Be quiet, do not print symmtry operators that are
               # found to belong to the group.

my $do_not_report_errors = 0; # Do not output error messages when the
                              # argument symmetry operator does not
                              # belong to the group.

my $symbol_type = 'hermann_mauguin';

#* OPTIONS:
#*   -q, --quiet
#*                     Do not output recognised operator names (default).
#*   -v, --verbose
#*                     Print out symmetry operators, negate the '-q' option.
#*
#*   --hall            Use Hall symbols for lookup
#*   --hermann-mauguin Use Hermann-Mauguin symbols for lookup
#*   --schoenflies     Use Schönflies symbols for lookup
#*
#*   --error-messages
#*                     Print error messages about unrecognised symmetry
#*                     operators (default).
#*   --no-error-messages
#*                     Do not print error messages about missing
#*                     symmetry operators, just return a non-zero
#*                     status if such are encountered.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "-q,--quiet"   => sub { $quiet = 1 },
    "-v,--verbose" => sub { $quiet = 0 },

    "--hall"              => sub {$symbol_type = 'hall'},
    "--hermann-mauguin"   => sub {$symbol_type = 'hermann_mauguin'},
    "--schoenflies"       => sub {$symbol_type = 'schoenflies'},
    
    "--no-error-messages" => sub { $do_not_report_errors = 1 },
    "--error-messages"    => sub { $do_not_report_errors = 0 },

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

if( @ARGV < 1 ) {
    print STDERR "$0: usage: $0 SGNAME\n";
    print STDERR " e.g.: $0 P212121\n";
    exit 1;
}

my $spacegroup_name = shift( @ARGV );

my $sym_data =
    COD::CIF::Data::lookup_space_group( $symbol_type, $spacegroup_name );

if( defined $sym_data ) {
    local $\ = "\n";
    if( @ARGV == 0 ) {
        for my $symop (@{$sym_data->{symops}}) {
            print symop_string_canonical_form( $symop );
        }
    } else {
        my $status = 0;
        my %symops = map {
            symop_string_canonical_form( $_ ) => 1
        } @{$sym_data->{symops}};
        for my $symop (@ARGV) {
            my $symop_key = symop_string_canonical_form( $symop );
            if( exists $symops{$symop_key} ) {
                print $symop_key, "\t", $symop
                    unless $quiet;
            } else {
                print STDERR "$0: symetry operator '$symop' ('$symop_key') " .
                    "was not found in spacegroup '$spacegroup_name'"
                    unless $do_not_report_errors;
                $status = 3;
            }
        }
        exit $status;
    }
} else {
    print STDERR "$0: spacegroup '$spacegroup_name' could not be identified\n";
    exit 2;
}
