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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
game_dir="$repo_root/rt3_wineprefix/drive_c/rt3"
proxy_path="$game_dir/dinput8.dll"
. "$HOME/.local/share/cargo/env"

cargo build -p rrt-hook --target i686-pc-windows-gnu
cp "$repo_root/target/i686-pc-windows-gnu/debug/dinput8.dll" "$proxy_path"

cd "$game_dir"
export WINEPREFIX="$repo_root/rt3_wineprefix"
export WINEDLLOVERRIDES="dinput8=n,b"

cmd=(/opt/wine-stable/bin/winedbg)
cmd_file="${RRT_WINEDBG_CMD_FILE:-$repo_root/tools/winedbg_manual_load_445ac0.cmd}"
if [[ -n "$cmd_file" ]]; then
    cmd+=(--file "$cmd_file")
fi
cmd+=(RT3.exe)

log_file="${RRT_WINEDBG_LOG:-$repo_root/rt3_manual_load_winedbg.log}"
if [[ -n "$log_file" ]]; then
    cmd_string="$(printf '%q ' "${cmd[@]}")"
    exec script -qefc "${cmd_string% }" "$log_file"
fi

exec "${cmd[@]}"
