#!/usr/bin/env bash
# VC++ 2015 из __redist JA3 (innoextract не запускает установщики).
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

JA3="${BOTTLE_DIR}/drive_c/GOG Games/Jagged Alliance 3"
VC_X86="${JA3}/__redist/MSVC2015/VC_redist.x86.exe"
VC_X64="${JA3}/__redist/MSVC2015_x64/VC_redist.x64.exe"
MARKER="${BOTTLE_DIR}/.ja3_redist_installed"

[[ -f "${JA3}/JA3.exe" ]] || {
  echo "❌ Сначала install-ja3-innoextract.sh"
  exit 1
}

if [[ -f "${MARKER}" ]]; then
  echo "JA3 redist уже ставился: ${MARKER}"
  exit 0
fi

export_wine_env_gog
gog_wine_check

for vc in "${VC_X86}" "${VC_X64}"; do
  [[ -f "${vc}" ]] || continue
  base="$(basename "${vc}")"
  cp -f "${vc}" "${BOTTLE_DIR}/drive_c/${base}"
  echo "VC++ ${base}…"
  "${WINE64}" "C:\\${base}" /install /quiet /norestart || true
done

date -u +"%Y-%m-%dT%H:%M:%SZ" >"${MARKER}"
echo "OK: VC++ redist для JA3"
