#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2017-07-20 13:13:16 +0300 (Thu, 20 Jul 2017) $
#$Revision: 5477 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.10/scripts/cod_predeposition_check $
#------------------------------------------------------------------------------
#*
#* Filter and check pairs of CIF and Fobs files for suitability for
#* deposition to COD database.
#*
#* USAGE:
#*    $0 --options input.cif input*.cif
#**

use strict;
use warnings;
use POSIX qw( strftime );
use COD::CIF::Data::CODPredepositionCheck qw( filter_and_check );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ToolsVersion;

my %database = (
        host  => 'www.crystallography.net',
        user  => 'cod_reader',
        name  => 'cod',
        table => 'data',
        password => '',
        platform => 'mysql',
    );

my @cif;
my @hkl;

my $deposition_type = 'published';
my $options;
$options->{'use_parser'} = 'c';

my $print_timestamps = 0;

my $tmp_dir;
my $tmp_file = "tmp-cod_predeposition_check-$$.tmp";

#* OPTIONS:
#*   -d, --database  cod
#*                     Use database "cod" to query for structures.
#*
#*   -h, --host   www.crystallography.net
#*   -s, --server www.crystallography.net
#*                     Query COD database on the host 'www.crystallography.net'.
#*
#*   -l, --localhost
#*                     Use database server on the localhost to query the COD
#*                     database.
#*
#*   -p, --port 3306
#*                     Use use the specified port (default 3306) to query
#*                     structures.
#*
#*   -t, --table  data
#*                     Use SQL table 'data' to query for structures.
#*
#*   -u, --user cod_reader
#*                     Use user name 'cod_reader' to access COD database;
#*                     this reader should be granted SELECT privilege, i.e.
#*                     should be able to read the COD database, without
#*                     supplying a password.
#*
#*   --password
#*                     Use the specified password (default empty) to connect.
#*
#*   --platform 'SQLite'
#*                     Use the SQL database platform 'SQLite' to query
#*                     structures (default 'mysql').
#*
#*   --cif input1.cif
#*                     Specify a CIF file to be deposited. Multiple CIF files
#*                     can be provided in a way like this:
#*                     $0 --cif input1.cif --cif input2.cif
#*
#*   --hkl, --fobs input1.hkl
#*                     Specify a Fobs file to be deposited. Multiple Fobs files
#*                     can be provided in way like this:
#*                     $0 --hkl input1.hkl --hkl input2.hkl
#*                     The order of the files is important in order to pair CIF
#*                     and Fobs files correctly.
#*
#*   --type, --deposition-type 'prepublication'
#*                     Specify the deposition type under which the structure
#*                     is to be deposited (default 'published').
#*
#*   --author-name 'Doe, J.'
#*                     Use the specified name as the name of the person
#*                     depositing the files.
#*
#*   --journal 'The Journal'
#*                     Use the specified journal name as if it was supplied
#*                     by the depositor during the 'prepublication' type
#*                     deposition.
#*
#*   --hold-period 3
#*                     Use the specified hold period as if it was supplied by
#*                     the depositor.
#*
#*   --replace
#*                     Treat given files as replacements for previously
#*                     deposited data.
#*   --no-replace, --dont-replace
#*                     Do not treat given files as replacements for previously
#*                     deposited data (default).
#*
#*   --release
#*                     Treat given files as if they were previously deposited
#*                     as prepublication material and should now be released
#*                     into the public domain.
#*   --no-release, --dont-release
#*                     Do not treat given files as if they were previously
#*                     deposited as prepublication material and should now
#*                     be released into the public domain (default).
#*
#*   --bypass-checks
#*                     Do not check CIF files for compliance with COD
#*                     requirements.
#*   --dont-bypass-checks, --no-bypass-checks
#*                     Check CIF files for compliance with COD requirements
#*                     (default).
#*
#*   --split-pdcif
#*                     Output powder diffraction data blocks as a HKL file.
#*   --dont-split-pdcif, --no-split-pdcif
#*                     Output powder diffraction data blocks as part of the
#*                     CIF file (default).
#*
#*   --print-timestamps
#*                     Print timestamps to STDOUT at the start and end of
#*                     each CIF file deposition.
#*   --no-print-timestamps, --dont-print-timestamps
#*                     Do not print timestamps to STDOUT at the start and end
#*                     of each CIF file deposition (default)
#*
#*   --tmp-dir, --temp-dir
#*                     Use the specified temporary directory (default './').
#*
#*   --use-perl-parser
#*                     Use Perl parser to parse CIF files.
#*   --use-c-parser
#*                     Use C parser to parse CIF files (default).
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '-d,--database'  => \$database{name},
    '-l,--localhost' => sub { $database{host} = 'localhost' },
    '-h,--host'      => \$database{host},
    '-p,--port'      => \$database{port},
    '-s,--server'    => \$database{host},
    '-t,--table'     => \$database{table},
    '-u,--user'      => \$database{user},
    '--password'     => \$database{password},
    '--platform'     => \$database{platform},

    '--cif' => sub { push @cif, get_value() },
    '--hkl,--fobs' => sub { @hkl[(@cif-1 > 0) ? @cif-1 : 0] = get_value() },

    '--type,--deposition-type' => \$deposition_type,

    '--bypass-checks'      => sub { $options->{bypass_checks} = 1 },
    '--dont-bypass-checks' => sub { delete( $options->{bypass_checks} ) },
    '--no-bypass-checks'   => sub { delete( $options->{bypass_checks} ) },

    '--replace'      => sub { $options->{replace} = 1 },
    '--dont-replace' => sub { delete( $options->{replace} ) },
    '--no-replace'   => sub { delete( $options->{replace} ) },

    '--release'      => sub { $options->{release} = 1 },
    '--dont-release' => sub { delete( $options->{release} ) },
    '--no-release'   => sub { delete( $options->{release} ) },

    '--hold-period' => \$options->{hold_period},
    '--journal'     => \$options->{journal},
    '--author-name' => \$options->{author_name},

    '--split-pdcif'      => sub { $options->{split_pdcif} = 1 },
    '--dont-split-pdcif' => sub { delete( $options->{split_pdcif} ) },
    '--no-split-pdcif'   => sub { delete( $options->{split_pdcif} ) },

    '--print-timestamps'      => sub { $print_timestamps = 1 },
    '--dont-print-timestamps' => sub { $print_timestamps = 0 },
    '--no-print-timestamps'   => sub { $print_timestamps = 0 },

    '--tmp-dir,--temp-dir' => \$tmp_dir,

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

push @cif, @ARGV;

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

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

if( $tmp_dir ) {
    $tmp_file = $tmp_dir . '/' . $tmp_file;
}

if( exists $options->{release} ) {
    $options->{replace} = 1;
}
if( $options->{release} && $deposition_type eq 'prepublication' ) {
    die "$0:: ERROR, structure can be released either as published or " .
        'personal communication.' . "\n";
}

for my $i (0..@cif-1) {
    print "START $cif[$i] " . strftime( "%Y-%m-%d %T\n", localtime )
        if $print_timestamps;
    my( $cif, $hkl );
    eval {
        ( $cif, $hkl ) =
            filter_and_check( $cif[$i],
                              $cif[$i],
                              $hkl[$i],
                              $hkl[$i],
                              \%database,
                              $deposition_type,
                              $tmp_file,
                              $options );
    };
    if( $@ ) {
        if ( $@ =~ /[^.]\n$/ ) {
            $@ =~ s/\n$/.\n/
        };
        print {*STDERR} $@;
    } else {
        if ( defined $cif ) { print map { "CIF $_\n" } split m/\n/, $cif };
        if ( defined $hkl ) { print map { "HKL $_\n" } split m/\n/, $hkl };
    }
    print "FINISH $cif[$i] " . strftime( "%Y-%m-%d %T\n", localtime )
        if $print_timestamps;
    unlink $tmp_file if -e $tmp_file;
}
