#!/bin/bash

fileMissing=0

# Check if we have all commands installed that this script needs

function failcmd() {
    if [ -x "$( command -v $1 )" ] ; then
	return 0
    else
	1>&2 echo "Command \"$1\" not found, please install"
	exit -1
    fi
}

failcmd wget
failcmd ar
failcmd xz
failcmd tar
failcmd zstd

# Get the ImageJ base library (in version 1.48, which is the lowest we currently support)
if [ ! -e ij148v.jar ] ; then
    fileMissing=1
#    wget https://imagej.nih.gov/ij/download/jars/ij148v.jar
    wget -c https://wsr.imagej.net/download/jars/ij148v.jar
fi

# Get our forked version of JTransforms 
if [ ! -e jtransforms_fairSIM_fork.jar ] ; then
    fileMissing=1
    wget https://github.com/fairSIM/JTransforms/releases/download/v1.0.0/jtransforms_fairSIM_fork.jar
fi

# Get the original version of JTransforms
if [ ! -e JTransforms-3.1.jar ] ; then 
    fileMissing=1
    wget https://repo1.maven.org/maven2/com/github/wendykierp/JTransforms/3.1/JTransforms-3.1.jar
fi

# Get JTransforms JLargeArray dependency
if [ ! -e JLargeArrays-1.6.jar ] ; then
    fileMissing=1
    wget https://repo1.maven.org/maven2/pl/edu/icm/JLargeArrays/1.6/JLargeArrays-1.6.jar
fi

# Get the Apache fast math dependencies
if [ ! -e commons-math3-3.6.1.jar ] ; then
    fileMissing=1
    wget https://repo1.maven.org/maven2/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar
fi


# This fetches the java 1.8 runtime, needed for backwards-compatible
# compilation to Java 8 with newer compilers
#   unfortunately, Java 6's rt.jar does not seem to be on Maven, so
#   fetch it by extracting it from Ubuntu's openjdk deb.
#   Java 9 will make it much more easier with the "-release" option
if [ ! -e rt-1.6.jar ] ; then
    fileMissing=1

    mkdir tmp-rt-jar
    cd tmp-rt-jar

    # download the 'openjdk-6-jre-headless' deb file
    #wget http://security.ubuntu.com/ubuntu/pool/universe/o/openjdk-6/openjdk-6-jre-headless_6b41-1.13.13-0ubuntu0.14.04.1_amd64.deb -O openjdk-6.deb
    wget https://security.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jre-headless_8u312-b07-0ubuntu1_amd64.deb -O openjdk-8.deb


    # extract the deb
    echo "Extracting rt.jar from the .deb file"
    echo "This might take a few moments... "
    ar -x openjdk-8.deb

    # extract the rt.jar from the data.tar
    zstd -d data.tar.zst
    tar -xf data.tar ./usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
    mv ./usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar ../rt-1.8.jar
    cd ..

    rm -rf tmp-rt-jar
    echo "Done."

fi

if [ $fileMissing -eq 0 ] ; then
    echo "All files found, fairSIM should compile"
fi

