#!/usr/bin/perl

use POSIX;
use File::Basename;
use Cwd qw(abs_path getcwd);

$name = basename($0);
$exe = abs_path($0);
$path = abs_path(dirname($exe));
chop($java=`which java`);
$jarfile_name = "xml-casa-assembly-1.86.jar";

### install path
$jarfile = "$path/java/$jarfile_name";
### development path
if ( ! -e $jarfile ) {
    my $devjarfile = "$path/../target/scala-2.12/$jarfile_name";
    if ( -e $devjarfile ) {
        $jarfile = $devjarfile;
    }
}
if ( ! -e $jarfile ) {
    my $cmd = "curl --create-dirs --output $jarfile https://casa.nrao.edu/download/devel/xml-casa/java/$jarfile_name 2>&1 |";
    open( my $fh, $cmd );
    my @output = <$fh>;
    close( $fh );
    if ( `file $jarfile` =~ m|HTML| ) {
        print( "\t--------------------------------------------------------------------------\n" );
        print( "\tcould not fetch $jarfile_name\n" );
        print( "\t---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  --- \n" );
        foreach ( @output ) {
            print( "\t$_" );
        }
        print( "\t--------------------------------------------------------------------------\n" );
        unlink( $jarfile );
        exit(1);
    }
}
die "configuration error, cannot find jar file ($jarfile_name)" unless -e $jarfile;

@flags = ( );
@inputs = ( );
$doing_something = 0;


foreach ( @ARGV ) {
    m|^-markdown$| && (push(@flags,$_),$doing_something=1,next);
    m|^-upgrade$| && (push(@flags,$_),$doing_something=1,next);
    m|^-header$| && (push(@flags,$_),$doing_something=1,next);
    m|^-swig$| && (push(@flags,$_),$doing_something=1,next);
    m|^-tool$| && (push(@flags,$_),$doing_something=1,next);
    m|^-task$| && (push(@flags,$_),$doing_something=1,next);
    m|^-gotask$| && (push(@flags,$_),$doing_something=1,next);
    m|^-all$| && (push(@flags,$_),$doing_something=1,next);
    m|^-tgts$| && (push(@flags,$_),$doing_something=1,next);
    m|^-output-tool-subdir$| && (push(@flags,$_),next);
    m|^-help$| && (usage(), exit(0));
    m|^gotask-imp-module=(.*)$| && (push(@flags,$_),next);
    m|\.xml$| && -f && (push(@inputs,$_),next);
    if ( m|^outdir=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-h=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-i=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-c=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-tool=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-task=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    if ( m|^output-gotask=(.*)$| ) {
        if ( -d $1 ) { push(@flags,$_) }
        else { die "in '$_' directory does not exist" }
        next;
    }
    die "unknown argument'$_'";
}

unless ( $doing_something ) { print "parameters indicate a no-op...\n" }
@args = ( basename($java), '-jar', $jarfile );
push(@args,@flags);
push(@args,@inputs);
exec { $java } @args;
die "failed to start XML compiler: $!";

sub usage {
    print "$name [-help]\n";
    print "\n        -help  --  display this message\n";
    print "\n";
}