#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd -- "$script_dir/.." && pwd)"
version="$(<"$repo_root/VERSION.txt")"
installer_path="${1:-$script_dir/build/installer/MirageSetup-${version}-x64.exe}"

if [[ $# -gt 1 ]]; then
  printf 'usage: %s [installer-path]\n' "$0" >&2
  exit 2
fi

installer_path="$(realpath "$installer_path")"

if [[ ! -f "$installer_path" ]]; then
  printf 'installer not found: %s\n' "$installer_path" >&2
  exit 1
fi

if [[ "$installer_path" != "$repo_root/"* ]]; then
  printf 'installer path must be inside repo root: %s\n' "$installer_path" >&2
  exit 1
fi

image_tag="${MIRAGE_PODMAN_WINE_IMAGE:-local/mirage-wine64-test:bookworm}"
apt_mirror="${MIRAGE_PODMAN_WINE_APT_MIRROR:-http://mirrors.tuna.tsinghua.edu.cn/debian}"
apt_security_mirror="${MIRAGE_PODMAN_WINE_APT_SECURITY_MIRROR:-http://mirrors.tuna.tsinghua.edu.cn/debian-security}"
podman_root="${MIRAGE_PODMAN_ROOT:-}"
podman_runroot="${MIRAGE_PODMAN_RUNROOT:-}"
podman_storage_driver="${MIRAGE_PODMAN_STORAGE_DRIVER:-}"
work_root="$script_dir/build/podman-wine64"
logs_dir="$work_root/logs"
install_root="$work_root/install-root"
wineprefix="$work_root/wineprefix"
container_work="/work"
container_installer="/src/${installer_path#"$repo_root"/}"
container_install_root="$container_work/install-root"
container_logs_dir="$container_work/logs"
podman_cmd=(podman)

if [[ -n "$podman_root" ]]; then
  mkdir -p "$podman_root"
  podman_cmd+=(--root "$podman_root")
  if [[ -z "$podman_runroot" ]]; then
    podman_runroot="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/podman-mirage-wine64-run"
  fi
fi

if [[ -n "$podman_runroot" ]]; then
  mkdir -p "$podman_runroot"
  podman_cmd+=(--runroot "$podman_runroot")
fi

if [[ -n "$podman_storage_driver" ]]; then
  podman_cmd+=(--storage-driver "$podman_storage_driver")
fi

rm -rf "$work_root"
mkdir -p "$logs_dir" "$install_root" "$wineprefix"

"${podman_cmd[@]}" build \
  --build-arg "APT_MIRROR=$apt_mirror" \
  --build-arg "APT_SECURITY_MIRROR=$apt_security_mirror" \
  -t "$image_tag" \
  -f "$script_dir/Containerfile.wine64-test" \
  "$repo_root"

"${podman_cmd[@]}" run --rm \
  --userns=keep-id \
  -v "$repo_root:/src" \
  -v "$work_root:$container_work" \
  -w /src \
  "$image_tag" \
  bash -lc '
    set -euo pipefail
    export WINEARCH=win64
    export WINEPREFIX="'"$container_work"'/wineprefix"
    installer="'"$container_installer"'"
    install_root="'"$container_install_root"'"
    logs_dir="'"$container_logs_dir"'"
    install_dir_win="Z:${install_root//\//\\\\}"
    install_log_win="Z:${logs_dir//\//\\\\}\\\\install.log"
    runtime_log="${logs_dir}/wine-runtime.log"
    wine_bin="$(command -v wine64 || command -v wine || true)"
    if [[ -z "${wine_bin}" && -x /usr/lib/wine/wine64 ]]; then
      wine_bin=/usr/lib/wine/wine64
    fi
    wineserver_bin="$(command -v wineserver || true)"
    if [[ -z "${wineserver_bin}" && -x /usr/lib/wine/wineserver ]]; then
      wineserver_bin=/usr/lib/wine/wineserver
    fi
    if [[ -z "${wine_bin}" || -z "${wineserver_bin}" ]]; then
      printf "wine runtime not found in container\n" >&2
      exit 1
    fi

    xvfb-run -a bash -lc "
      set -euo pipefail
      \"${wine_bin}\" wineboot -u
      \"${wineserver_bin}\" -w
      \"${wine_bin}\" \"${installer}\" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /DIR=\"${install_dir_win}\" /LOG=\"${install_log_win}\"
      \"${wineserver_bin}\" -w
    " > "${runtime_log}" 2>&1
  '

test -f "$install_root/MirageGUI.exe"
test -f "$install_root/mirage.exe"
test -f "$install_root/wintun.dll"
test -f "$logs_dir/install.log"
if awk '/Process exit code:/ && $NF != 0 { bad = 1 } END { exit bad ? 0 : 1 }' "$logs_dir/install.log"; then
  printf 'installer post-install step failed; see %s and %s\n' "$logs_dir/install.log" "$logs_dir/wine-runtime.log" >&2
  exit 1
fi

printf 'installer test root: %s\n' "$work_root"
printf 'install log: %s\n' "$logs_dir/install.log"
