#!/bin/sh

# env vars:
# VERSION: x.y.z or early-access
# TAG: vx.y.z or early-access
# JAR_SIZE
# JAR_CSUM
# DOC_SIZE
# GITHUB_BOT_EMAIL

EFFECTIVE_VERSION=$VERSION
REPOSITORY_OWNER="kordamp"
REPOSITORY_NAME="jarviz"
TOOL_NAME="jarviz"
TOOL_DESC="Jarviz"
TOOL_FILENAME="jarviz-tool-provider"

if [ "${TAG}" = "early-access" ]; then
  EFFECTIVE_VERSION=$TAG
fi

TARGET_FILE=".bach/external-tools/${TOOL_NAME}@${TAG}.tool-directory.properties"
echo "@description ${TOOL_DESC} ${TAG} (${VERSION})" > $TARGET_FILE
echo " " >> $TARGET_FILE
echo "${TOOL_FILENAME}-${EFFECTIVE_VERSION}.jar=\\" >> $TARGET_FILE
echo "  https://github.com/${REPOSITORY_OWNER}/${REPOSITORY_NAME}/releases/download/${TAG}/${TOOL_FILENAME}-${EFFECTIVE_VERSION}.jar\\" >> $TARGET_FILE
echo "  #SIZE=${JAR_SIZE}&SHA-256=${JAR_CSUM}" >> $TARGET_FILE
echo "README.adoc=\\" >> $TARGET_FILE
echo "  https://github.com/${REPOSITORY_OWNER}/${REPOSITORY_NAME}/raw/${TAG}/README.adoc\\" >> $TARGET_FILE
echo "  #SIZE=${DOC_SIZE}" >> $TARGET_FILE
echo "" >> $TARGET_FILE

git add $TARGET_FILE
git config --global user.email "${GITHUB_BOT_EMAIL}"
git config --global user.name "GitHub Action"
git commit -a -m "Releasing ${TOOL_NAME} ${TAG} (${VERSION})"
git push origin main
