#!/bin/bash

# Main modpack project directory
cd "$(dirname "$0")"
basedir="$PWD"

# Location for downloading/updating the mc_rebalance mod
if [ ! -d deps ]; then
	mkdir deps
fi;

# If the user provides input e.g. `./export-pack.sh 1`, clone the repository using https
if [ -n "$1" ]; then
	repo_prefix="https://patience.nearmisses.xyz/"
else
	repo_prefix="forgejo@patience.nearmisses.xyz:"
fi

mod_repo="$repo_prefix/patience/mc_rebalance.git"
pack_repo="$repo_prefix/SergeantAcoustic/mc_rebalance_datapack.git"

# Clone the repos if they exist, otherwise, update them
git clone "$mod_repo" "$basedir/deps/mc_rebalance" || (cd "$basedir/deps/mc_rebalance"; git pull) || exit 1
git clone "$pack_repo" "$basedir/overrides/global_packs/required_data/mc_rebalance_datapack/" || (cd "$basedir/overrides/global_packs/required_data/mc_rebalance_datapack/"; git pull) || exit 1

(cd "$basedir"/deps/mc_rebalance/ && ./gradlew build)

(cd "$basedir/overrides/global_packs/required_data/mc_rebalance_datapack/assets/minecraft/lang/" && ./gen-langs.sh)

cp "$basedir/deps/mc_rebalance/build/libs/mc_rebalance-1.20.1-"*".jar" "$basedir/overrides/mods"

# Final path for the modpack
if [ ! -d release ]; then
	mkdir release
fi;

zip -FSr "$basedir/release/Tinker's tinkering.mrpack" overrides modrinth.index.json -x "*.git*"
