#!/bin/zsh
set -e

echo ""
echo "ExtremeGammon macOS installer"
echo "=============================="
echo ""

if [[ $(uname -m) == "arm64" ]]; then
  echo "==> Installing Rosetta 2..."
  softwareupdate --install-rosetta --agree-to-license 2>/dev/null || true
fi

echo "==> Downloading Wine 11.0..."
curl -L --progress-bar -o ~/Downloads/wine-stable-11.0-osx64.tar.xz \
  https://archive.org/download/wine-stable-11.0-osx64.tar/wine-stable-11.0-osx64.tar.xz

echo "==> Extracting Wine to /Applications..."
tar -xf ~/Downloads/wine-stable-11.0-osx64.tar.xz -C /Applications

echo "==> Clearing Gatekeeper quarantine on Wine..."
xattr -dr com.apple.quarantine "/Applications/Wine Stable.app" 2>/dev/null || true

echo "==> Adding wine to PATH..."
WINE_BIN="/Applications/Wine Stable.app/Contents/Resources/wine/bin"
XG_EXE="$HOME/.wine/drive_c/Program Files (x86)/eXtreme Gammon 2/eXtremeGammon2.exe"
case "$(basename "$SHELL")" in
  zsh)
    grep -q "Wine Stable" ~/.zshrc 2>/dev/null \
      || echo "export PATH=\"$WINE_BIN:\$PATH\"" >> ~/.zshrc ;;
  bash)
    grep -q "Wine Stable" ~/.bash_profile 2>/dev/null \
      || echo "export PATH=\"$WINE_BIN:\$PATH\"" >> ~/.bash_profile ;;
  fish)
    fish -c "fish_add_path \"$WINE_BIN\"" ;;
  *)
    echo "Unrecognized shell. Add this to your shell config manually:"
    echo "  export PATH=\"$WINE_BIN:\$PATH\"" ;;
esac

echo "==> Downloading XG installer..."
curl -L --progress-bar -o ~/Downloads/xg2install.exe \
  https://www.extremegammon.com/xg2install.exe

echo "==> Launching XG installer..."
echo "    Follow the prompts to complete installation."
"$WINE_BIN/wine" ~/Downloads/xg2install.exe

echo "==> Adding xg launch alias..."
ALIAS_LINE="alias xg='echo \"Launching eXtreme Gammon — you can close this terminal once XG opens.\" && nohup wine ~/.wine/drive_c/Program\ Files\ \(x86\)/eXtreme\ Gammon\ 2/eXtremeGammon2.exe > /dev/null 2>&1 &'"
case "$(basename "$SHELL")" in
  zsh)
    grep -q "eXtremeGammon2" ~/.zshrc 2>/dev/null \
      || echo "$ALIAS_LINE" >> ~/.zshrc ;;
  bash)
    grep -q "eXtremeGammon2" ~/.bash_profile 2>/dev/null \
      || echo "$ALIAS_LINE" >> ~/.bash_profile ;;
  fish)
    fish -c 'function xg; set exe "$HOME/.wine/drive_c/Program Files (x86)/eXtreme Gammon 2/eXtremeGammon2.exe"; echo "Launching eXtreme Gammon — you can close this terminal once XG opens."; nohup "/Applications/Wine Stable.app/Contents/Resources/wine/bin/wine" $exe > /dev/null 2>&1 &; end; funcsave xg'
    ;;
  *)
    echo "  Add to your shell config manually: $ALIAS_LINE" ;;
esac

echo "==> Installing app launcher..."
curl -L --progress-bar -o ~/Downloads/eXtremeGammon.zip \
  https://vlamis.nyc/bg/eXtremeGammon.zip
ditto -xk ~/Downloads/eXtremeGammon.zip /Applications
xattr -dr com.apple.quarantine "/Applications/eXtremeGammon.app" 2>/dev/null || true

echo ""
echo "=============================="
echo "All done!"
echo ""
echo "  - Launch from Terminal:  xg  (open a new window first)"
echo "  - Launch from Finder:    eXtremeGammon.app in /Applications"
echo "=============================="
echo ""
