#!/usr/bin/perl

use strict;
use warnings;

use File::Basename qw( basename );
use IPC::Run3;
use Getopt::Long::Descriptive;

my $basename = basename $0;
my( $opt, $usage ) = describe_options( <<"END" . 'OPTIONS',
USAGE
    $basename [<args>] [<files>]

DESCRIPTION
    $basename prepares tests for ChemOnomatopist

END
    [ 'author',  'mark output tests with AUTHOR flag' ],
    [ 'pubchem', 'treat the input as three-columned PubChem file' ],
    [],
    [ 'help', 'print usage message and exit', { shortcircuit => 1 } ],
);

if( $opt->help ) {
    print $usage->text;
    exit;
}

my @lines = <>;

for (@lines) {
    s/^\s+//;
    s/\s+$//;

    my( $smiles, $iupac, $source );
    if( $opt->pubchem ) {
        ( my $id, $iupac, $smiles ) = split /\t/, $_;
        $source = "PubChem $id";
    } else {
        my( $stderr );
        $iupac = $_;
        run3 [ 'java', '-jar', '/usr/share/java/opsin.jar' ], \$iupac, \$smiles, \$stderr;
        $smiles =~ s/\n$//;
    }
    $iupac =~ s/'/\\'/g;

    print "{ smiles => '$smiles', iupac => '$iupac'";
    print ', AUTHOR => 1' if $opt->author;
    print ' },';
    print " # $source" if $source;
    print "\n";
}
