#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use Pod::Usage qw( pod2usage );

my $joinType = '1and2';
my $in1;
my $in2;
my $out;
my $ofmt;

my $jardir = $ENV{JARDIR};
$jardir = '/home/panstarrs/bills/jars' unless $jardir;

my $stiltsJar = "$jardir/stilts.jar";
my $stilts = "java -jar $stiltsJar";

my ($verbose, $save_temps);

GetOptions(
    "ofmt=s"            =>  \$ofmt,
    "join=s"            =>  \$joinType,
    "verbose|v"         =>  \$verbose,
    "save-temps"        =>  \$save_temps,
) or pod2usage( 2 );

die "usage: $0 [--ofmt <ofmt>] <psps detections file> <ipp detections file> [<out>]\n" unless (scalar @ARGV == 2) or (scalar @ARGV == 3);

$in1 = shift;
$in2 = shift;
$out = shift;

my $cmd = "$stilts tmatch2 fixcols=dups suffix1='' suffix2='_i'"
            . " matcher=2d values1='imageID ippDetectID' values2='IMAGE_ID IPP_IDET'  params=.01"
            . " join=$joinType "
            . " in1=$in1 in2=$in2";

$cmd .= " out=$out" if $out;
$cmd .= " ofmt=$ofmt" if $ofmt;

$cmd .= " progress=none" unless $verbose;

print "$cmd\n" if $verbose;

my $rc = system $cmd;
if ($rc) {
    my $status = $rc >> 8;
    die "$cmd failed\nrc = $rc status = $status";
}
