#!/bin/bash

set -exuo pipefail

PACKAGE_HOME="${PREFIX}/share/${PKG_NAME}"
mkdir -p "${PREFIX}/bin"
mkdir -p "${PACKAGE_HOME}"

# fake the git-commit-id-plugin based on the output of the gitlab API (since there is no .git folder in the tarball)
mkdir -p target/classes
curl https://git.wur.nl/api/v4/projects/5607/releases/v${PKG_VERSION} > release.json
echo "#Generated by Git-Commit-Id-Plugin" > target/classes/git.properties
echo "git.build.time=$(jq -r '.commit.created_at' release.json)" >> target/classes/git.properties
echo "git.build.version=${PKG_VERSION}" >> target/classes/git.properties
echo "git.commit.id.abbrev=$(jq -r '.commit.short_id' release.json)" >> target/classes/git.properties
echo "git.commit.id.full=$(jq -r '.commit.id' release.json)" >> target/classes/git.properties

# https://git.wur.nl/bioinformatics/pantools/-/blob/v3.4.0/README.md#building-a-runnable-jar
# this will generate 2 JAR files:
# * 'original-pantools-X.Y.Z.jar'
# * 'pantools-X.Y.Z.jar' <- JAR to package
mvn package -DskipTests=true -Dmaven.gitcommitid.skip=true

# rename 'pantools-X.Y.Z.jar' to 'pantools.jar'
cp target/pantools-${PKG_VERSION}.jar "${PACKAGE_HOME}/pantools.jar"
cp "${RECIPE_DIR}/pantools.py" ${PREFIX}/bin/pantools
chmod 0755 ${PREFIX}/bin/pantools
