#! /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/ssg_symop_string $
#------------------------------------------------------------------------------
#*
#* Determine superspace group symmetry operators from symmetry matrices.
#* Superspace group symmetry operators are output as parsable strings
#* as described in the CIF _space_group_symop_ssg_operation_algebraic
#* data item.
#*
#* USAGE: $0 < input.matrix
#* USAGE: $0 input.matrix
#* USAGE: $0 input1.matrix input*.matrix
#**

use strict;
use warnings;
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage );
use COD::Spacegroups::Symop::SSGParse qw( string_from_symop );
use COD::ToolsVersion;

#* OPTIONS:
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "--help,--usage" => sub { usage; exit },
    '--version'      => sub { print 'cod-tools version ',
                              $COD::ToolsVersion::Version, "\n";
                              exit }
);

my @matrix;

while(<>) {
    next if /^#/;
    next if /^\s*$/;
    chomp;
    if( /^\s*\[\s*$/ ) {
        @matrix = ();
        next;
    }
    if( /^\s*\[/ ) { # Matrix row
        s/^\s*\[\s*|\s*\]\s*$//g;
        my @row = split( /,\s*/ );
        push( @matrix, \@row );
        next;
    }
    if( /^\s*\]/ ) {
        print_commented_matrix( \@matrix );
        print string_from_symop( \@matrix ), "\n\n";
    }
}

sub print_commented_matrix
{
    my ($m) = @_;

    print "# [\n";
    for (@$m) {
        print "#   [ ";
        for (@$_) {
            printf "%2s, ", $_;
        }
        print " ]\n";
    }
    print "# ]\n";

    return;
}
