#!/bin/bash

: "${NEOFORGE_VERSION:=latest}"
: "${NEOFORGE_FORCE_REINSTALL:=false}}"
: "${NEOFORGE_INSTALLER:=}"

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

resultsFile=/data/.run-neoforge.env

if [[ ${NEOFORGE_INSTALLER} ]]; then
  if ! mc-image-helper install-neoforge --neoforge-installer="${NEOFORGE_INSTALLER}" --output-directory=/data --results-file=${resultsFile} --minecraft-version="${VERSION}" --force-reinstall="${NEOFORGE_FORCE_REINSTALL}"; then
    logError "Failed to installForge given installer ${NEOFORGE_INSTALLER}"
    exit 1
  fi
elif ! mc-image-helper install-neoforge \
  --output-directory=/data \
  --results-file=${resultsFile} \
  --minecraft-version="${VERSION}" \
  --neoforge-version="${NEOFORGE_VERSION}" \
  --force-reinstall="${NEOFORGE_FORCE_REINSTALL}"; then
  logError "Failed to install NeoForge"
  exit 1
fi

applyResultsFile ${resultsFile}

export FAMILY=FORGE

exec "$(dirname "$0")/start-setupWorld" "$@"
