#!/bin/sh
# Marble-WoW for Mac: copies the launcher to ~/Library/Application Support/Marble-WoW
# and opens it in a Terminal window (the window shows the download progress).
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
D="$HOME/Library/Application Support/Marble-WoW"
mkdir -p "$D"
# files written here by this script do not get the "downloaded from the internet" flag
cat "$RES/marble-wow.py" > "$D/marble-wow.py"
cat > "$D/Marble-WoW.command" <<'CMD'
#!/bin/sh
clear
D="$HOME/Library/Application Support/Marble-WoW"
PY=""
for c in /opt/homebrew/bin/python3 /usr/local/bin/python3 /Library/Frameworks/Python.framework/Versions/Current/bin/python3; do
    if [ -x "$c" ]; then PY="$c"; break; fi
done
if [ -z "$PY" ]; then
    if xcode-select -p >/dev/null 2>&1; then
        PY=/usr/bin/python3
    else
        xcode-select --install >/dev/null 2>&1
        osascript -e 'display dialog "Marble-WoW needs the Apple Command Line Tools (free).\nClick Install in the window that opened. When it finishes, open Marble-WoW again.\n\nMarble-WoW necesita las Command Line Tools de Apple (gratis).\nPulsa Instalar en la ventana que se abrió. Cuando termine, abre Marble-WoW otra vez." with title "Marble-WoW" buttons {"OK"} default button 1 with icon note' >/dev/null 2>&1
        exit 1
    fi
fi
# Apple Silicon: the game runs through Rosetta 2 (installed once)
if [ "$(uname -m)" = "arm64" ] && ! /usr/bin/arch -x86_64 /usr/bin/true >/dev/null 2>&1; then
    echo "Installing Rosetta 2 (only once)... / Instalando Rosetta 2 (solo una vez)..."
    softwareupdate --install-rosetta --agree-to-license || sudo softwareupdate --install-rosetta --agree-to-license
fi
"$PY" "$D/marble-wow.py" --in-terminal
CMD
chmod +x "$D/Marble-WoW.command"
open -a Terminal "$D/Marble-WoW.command"
