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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
game_dir="$repo_root/rt3_wineprefix/drive_c/rt3"
log_path="$game_dir/rrt_hook_attach.log"
proxy_path="$game_dir/dinput8.dll"

. "$HOME/.local/share/cargo/env"

cargo build -p rrt-hook --target i686-pc-windows-gnu

rm -f "$log_path"
cp "$repo_root/target/i686-pc-windows-gnu/debug/dinput8.dll" "$proxy_path"

        #RRT_WRITE_FINANCE_TEMPLATE=1 \
(
    cd "$game_dir"
    timeout 600s env \
        RRT_WRITE_FINANCE_CAPTURE=1 \
        WINEPREFIX="$repo_root/rt3_wineprefix" \
        WINEDLLOVERRIDES="dinput8=n,b" \
        /opt/wine-stable/bin/wine RT3.exe
) >/tmp/rrt-hook-wine.log 2>&1 || true

if [[ -f "$log_path" ]]; then
    cat "$log_path"
else
    echo "attach-log-missing" >&2
    exit 1
fi
