#!/bin/sh
set -eu
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
. "$SCRIPT_DIR/release.env"
case "$(uname -s)" in Darwin) DEFAULT_ROOT="$HOME/Library/Application Support/Ragnarok";; *) DEFAULT_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/Ragnarok";; esac
ROOT=${RAGNAROK_DESTINATION:-$DEFAULT_ROOT}
PROFILE="$ROOT/Profile"; RUNTIME="$ROOT/tModLoader-$VERSION"
checksum(){ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f1; else shasum -a 256 "$1" | cut -d ' ' -f1; fi; }
verify(){ [ -f "$1" ] && [ "$(checksum "$1")" = "$2" ]; }
if [ "${1:-}" = '--check' ]; then
 [ -f "$PROFILE/ragnarok-files.sha256" ] || { printf '%s\n' 'Run Set Up Ragnarok first.'; exit 1; }
 failures=0
 while IFS="$(printf '\t')" read -r sha path; do
  if ! verify "$PROFILE/$path" "$sha"; then printf 'Changed or missing: %s\n' "$path"; failures=$((failures+1)); fi
 done < "$PROFILE/ragnarok-files.sha256"
 [ -f "$RUNTIME/start-tModLoader.sh" ] || failures=$((failures+1))
 printf 'Version: %s\nChanged or missing files: %s\n' "$VERSION" "$failures" | tee "$ROOT/Ragnarok-Check.txt"
 [ "$failures" -eq 0 ]; exit
fi
PRESET=${RAGNAROK_PRESET:-}
if [ -z "$PRESET" ]; then
 printf '%s\n' 'Graphics: 1 Low (recommended), 2 Medium, 3 High, 4 Lights and Shadows'
 printf '%s' 'Choose 1-4, or Enter for Low: '; read -r choice || choice=1
 case "$choice" in 2) PRESET=medium;; 3) PRESET=high;; 4) PRESET=shadows;; *) PRESET=low;; esac
fi
case "$PRESET" in low|medium|high|shadows) ;; *) printf '%s\n' 'Unknown graphics preset.'; exit 1;; esac
for command in curl unzip; do command -v "$command" >/dev/null 2>&1 || { printf 'Install %s first using your OS package manager.\n' "$command"; exit 1; }; done
printf 'Installing in %s\nYou need your own Terraria installation and Steam.\n' "$ROOT"
mkdir -p "$ROOT/Downloads" "$PROFILE"
fetch(){
 url=$1; target=$2; sha=$3
 if verify "$target" "$sha"; then printf 'Verified cached download: %s\n' "$target"; return; fi
 curl --fail --location --retry 3 --proto '=https' --tlsv1.2 --output "$target.partial" "$url"
 verify "$target.partial" "$sha" || { printf '%s\n' 'Download checksum mismatch. Profile has not been changed.'; exit 1; }
 mv -f "$target.partial" "$target"
}
PACK="$ROOT/Downloads/$PACK_FILENAME"; TML="$ROOT/Downloads/tModLoader.zip"
fetch "$PACK_URL" "$PACK" "$PACK_SHA256"; fetch "$RUNTIME_URL" "$TML" "$RUNTIME_SHA256"
BUNDLE="$ROOT/Bundle-$(printf '%s' "$PACK_SHA256" | cut -c1-12)"
if [ ! -f "$BUNDLE/.verified" ]; then mkdir -p "$BUNDLE"; unzip -q -o "$PACK" -d "$BUNDLE"; printf '%s' "$PACK_SHA256" > "$BUNDLE/.verified"; fi
if [ ! -f "$RUNTIME/.verified" ]; then mkdir -p "$RUNTIME"; unzip -q -o "$TML" -d "$RUNTIME"; printf '%s' "$RUNTIME_SHA256" > "$RUNTIME/.verified"; fi
# First pass checks every selected mod before any profile content changes.
while IFS="$(printf '\t')" read -r name path sha presets; do
 case ",$presets," in *",$PRESET,"*) verify "$BUNDLE/$path" "$sha" || { printf 'Bundled mod checksum mismatch: %s\n' "$name"; exit 1; };; esac
done < "$SCRIPT_DIR/mods.tsv"
STAMP=$(date -u +%Y%m%dT%H%M%SZ)-$$; BACKUP="$ROOT/Backups/$STAMP"
for name in Mods ModConfigs; do
 if [ -d "$PROFILE/$name" ]; then mkdir -p "$BACKUP"; cp -R "$PROFILE/$name" "$BACKUP/$name"; fi
 mkdir -p "$PROFILE/$name"
done
printf '[\n' > "$PROFILE/Mods/enabled.json"; separator=''
while IFS="$(printf '\t')" read -r name path sha presets; do
 case ",$presets," in *",$PRESET,"*) cp "$BUNDLE/$path" "$PROFILE/Mods/$(basename "$path")"; printf '%s"%s"' "$separator" "$name" >> "$PROFILE/Mods/enabled.json"; separator=',';; esac
done < "$SCRIPT_DIR/mods.tsv"
printf '\n]\n' >> "$PROFILE/Mods/enabled.json"
cp "$BUNDLE"/base/ModConfigs/*.json "$PROFILE/ModConfigs/"
if [ -d "$SCRIPT_DIR/presets/$PRESET" ]; then cp "$SCRIPT_DIR/presets/$PRESET/"*.json "$PROFILE/ModConfigs/"; fi
if [ -d "$BUNDLE/ResourcePacks" ]; then mkdir -p "$PROFILE/ResourcePacks"; cp -R "$BUNDLE/ResourcePacks/." "$PROFILE/ResourcePacks/"; fi
: > "$PROFILE/ragnarok-files.sha256"
for file in "$PROFILE"/Mods/* "$PROFILE"/ModConfigs/*; do [ -f "$file" ] || continue; relative=${file#"$PROFILE/"}; printf '%s\t%s\n' "$(checksum "$file")" "$relative" >> "$PROFILE/ragnarok-files.sha256"; done
cat > "$ROOT/Play Ragnarok.command" <<EOF
#!/bin/sh
set -eu
ROOT=\$(CDPATH= cd -- "\$(dirname -- "\$0")" && pwd)
cd "\$ROOT/tModLoader-$VERSION"
exec ./start-tModLoader.sh -tmlsavedirectory "\$ROOT/Profile" -steamworkshopfolder none
EOF
chmod +x "$ROOT/Play Ragnarok.command" "$RUNTIME/start-tModLoader.sh"
# Keep Wine, macOS and Linux library scripts runnable where the official archive requires it.
find "$RUNTIME/LaunchUtils" -type f -name '*.sh' -exec chmod +x {} \;
printf 'Ready! Launch %s/Play Ragnarok.command\nJoin ragnarok.chickenturtle.org : 7777; ask the host for the password.\n' "$ROOT"
printf '%s\n' 'Steam must be running. The first launch may download the official .NET runtime.'
