#!/usr/bin/env bash
# The Whitney Builders (MODDED) - Minecraft modded client installer
# Installs the NeoForge 1.21.1 client profile + all mods into your Minecraft dir.
# Linux / macOS / Git-Bash. Requires: java (21+), curl.
#
# Usage:
#   bash install.sh          install/update the pack (keeps any other mods you have)
#   bash install.sh --clean  same, then OFFERS to remove jars not in this pack's
#                            manifest (makes the client exactly match the server)
set -euo pipefail

BASE_URL="${BASE_URL:-https://thewhitneybuilders.org}"
MC_DIR="${MC_DIR:-$HOME/.minecraft}"
CLEAN=0
for arg in "$@"; do
  case "$arg" in
    --clean) CLEAN=1 ;;
    -h|--help) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "Unknown option: $arg (try --help)" >&2; exit 1 ;;
  esac
done

if ! command -v java >/dev/null 2>&1; then
  echo "ERROR: java not found on PATH. Install a Java 21+ JDK and re-run." >&2
  exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
  echo "ERROR: curl not found. Install curl and re-run." >&2
  exit 1
fi

read -r -p "Minecraft directory [$MC_DIR]: " ans
MC_DIR="${ans:-$MC_DIR}"
mkdir -p "$MC_DIR/mods"

decode() { printf '%s' "$1" | sed -e 's/%20/ /g' -e "s/%27/'/g" -e 's/%2B/+/g'; }

echo
echo "Fetching mod list..."
MANIFEST="$(mktemp)"
trap 'rm -f "$MANIFEST"' EXIT
curl -fsSL -o "$MANIFEST" "$BASE_URL/mods.json"
# each mod entry has a "path" (URL-encoded, relative to the site root)
PATHS="$(grep -oE '"path": *"[^"]+"' "$MANIFEST" | sed 's/^"path": *"//; s/"$//' | sort -u)"
COUNT="$(printf '%s\n' "$PATHS" | grep -c .)"

# --- NeoForge client profile ---
INSTALLER="$MC_DIR/neoforge-21.1.248-installer.jar"
if [ ! -f "$INSTALLER" ]; then
  echo "Downloading NeoForge installer..."
  curl -fsSL -o "$INSTALLER" "$BASE_URL/neoforge-21.1.248-installer.jar"
fi
echo "Installing NeoForge client profile into $MC_DIR (first run may take a minute)..."
java -jar "$INSTALLER" --installClient "$MC_DIR" >/dev/null

# --- Mods (server pack + client-only extras) ---
KNOWN=()
i=0
while IFS= read -r p; do
  [ -z "$p" ] && continue
  i=$((i+1))
  name="$(decode "${p##*/}")"
  KNOWN+=("$name")
  dest="$MC_DIR/mods/$name"
  if [ -s "$dest" ]; then
    echo "[$i/$COUNT] $name (already present)"
    continue
  fi
  echo "[$i/$COUNT] $name"
  curl -fsSL -o "$dest.part" "$BASE_URL$p"
  mv "$dest.part" "$dest"
done <<< "$PATHS"
rm -f "$MC_DIR/mods/"*.part 2>/dev/null || true

# --- Optional cleanup: jars that are NOT in this pack's manifest ---
EXTRA=()
for j in "$MC_DIR/mods"/*.jar; do
  [ -e "$j" ] || continue
  b="$(basename "$j")"
  hit=0
  for k in "${KNOWN[@]}"; do
    if [ "$b" = "$k" ]; then hit=1; break; fi
  done
  [ "$hit" = "1" ] || EXTRA+=("$b")
done
if [ "${#EXTRA[@]}" -gt 0 ]; then
  if [ "$CLEAN" = "1" ]; then
    echo
    echo "Extra jars in $MC_DIR/mods (not part of this pack):"
    printf '  - %s\n' "${EXTRA[@]}"
    read -r -p "Remove these ${#EXTRA[@]} jar(s) so the client exactly matches the server? [y/N] " ans
    if [[ "$ans" =~ ^[Yy] ]]; then
      for b in "${EXTRA[@]}"; do
        rm -f "$MC_DIR/mods/$b"
        echo "  removed $b"
      done
    else
      echo "  kept them (you can re-run with --clean any time)"
    fi
  else
    echo
    echo "NOTE: ${#EXTRA[@]} jar(s) in $MC_DIR/mods are not part of this pack"
    echo "      (left untouched). Run with --clean to remove them if you want the"
    echo "      client to exactly match the server."
  fi
fi

echo
echo "Done! $COUNT mods installed in $MC_DIR/mods"
echo "Next: launch Minecraft, select the 'neoforge' profile, join thewhitneybuilders.org"
