#!/usr/bin/env bash
# Resolve Wine through Factory while retaining repository-owned state.
factory_repo_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
export PATH="$factory_repo_root/.tools/factory-bin:$PATH"

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )


# Reconstruction tooling is command-line only.  Do not let Wine route compiler
# or build invocations through WSLg/X11, and suppress the desktop integration
# helper that can create host-visible windows or menu entries.
unset DISPLAY WAYLAND_DISPLAY
export WINEDLLOVERRIDES="winemenubuilder.exe=d;${WINEDLLOVERRIDES:-}"

unset WINEPREFIX WINEARCH

MAIN_DRIVE_PATH=$SCRIPT_DIR/prefix
export WINEPATH="$SCRIPT_DIR;$WINEPATH"

# Disable wine logs (comment me for debugging)
export WINEDEBUG=-all
#export LANG=ja_JP.UTF-8

prefix_args=()
if [[ -n "${TH08_WINEPREFIX:-}" ]]; then prefix_args=(--prefix "$TH08_WINEPREFIX"); fi
exec "$factory_repo_root/scripts/shared_runtime.py" "${prefix_args[@]}" wine -- "$@" 2> >(grep -v "preloader: Warning: failed to reserve range 0000000000010000-0000000000110000" 1>&2)
