#!/usr/bin/env bash
# Workaround: official data.getwhisky.app is dead (404). Mirror from frankea/Whisky.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${SCRIPT_DIR}/config.sh"

LIB_URL="https://github.com/frankea/Whisky/releases/download/v3.0.0/Libraries.tar.gz"
APP_SUPPORT="${WHISKY_LIBS}"

mkdir -p "${GAMES_CACHE_DIR}"

echo "==> Download Libraries.tar.gz (~314 MB) if missing..."
if [[ ! -f "${LIBRARIES_TAR}" ]] || [[ $(stat -f%z "${LIBRARIES_TAR}" 2>/dev/null || stat -c%s "${LIBRARIES_TAR}") -lt 100000000 ]]; then
  curl -L --fail -o "${LIBRARIES_TAR}" "${LIB_URL}"
fi

echo "==> Extract to ${APP_SUPPORT}"
mkdir -p "${APP_SUPPORT}"
tar -xzf "${LIBRARIES_TAR}" -C "${APP_SUPPORT}"

echo "==> Done. WhiskyWine version:"
plutil -p "${APP_SUPPORT}/Libraries/WhiskyWineVersion.plist"
echo "Restart Whisky (Cmd+Q → open -a Whisky)."
