#!/bin/bash

: "${FORGE_VERSION:=${FORGEVERSION:-RECOMMENDED}}"
: "${FORGE_FORCE_REINSTALL:=false}}"

# shellcheck source=start-utils
. "${SCRIPTS:-$(dirname "$0")}/start-utils"
isDebugging && set -x
resultsFile=/data/.run-forge.env

function mc-image-helper-forge() {
  mc-image-helper install-forge \
    --output-directory=/data \
    --results-file="${resultsFile}" \
    --minecraft-version="${VERSION}" \
    --force-reinstall="${FORGE_FORCE_REINSTALL}" "$@"
}

if [[ ${FORGE_INSTALLER} ]]; then
  if ! mc-image-helper-forge --forge-installer="${FORGE_INSTALLER}" ; then
      logError "Failed to installForge given installer ${FORGE_INSTALLER}"
      exit 1
  fi
else
  if ! mc-image-helper-forge --forge-version="${FORGE_VERSION}"; then
      logError "Failed to install Forge"
      exit 1
  fi
fi

applyResultsFile ${resultsFile}

export FAMILY=FORGE

exec "${SCRIPTS:-/}start-setupWorld" "$@"
