#!/bin/bash
# winkod_wine_diag.sh — one-shot triage for a failing Windows program under
# Винкод's bundled wine. Runs steps 1–3 of the skill: PE arch, runtime
# inventory in the prefix, and a clean launch with a captured log.
#
# Usage:
#   winkod_wine_diag.sh <bottle-uuid> [exe-windows-or-unix-path]
# If exe is omitted, reads primaryExecutable from the bottle manifest.

set -uo pipefail

UUID="${1:?usage: winkod_wine_diag.sh <bottle-uuid> [exe]}"
EXE_ARG="${2:-}"

WINE="/Applications/Винкод.app/Contents/Resources/wine"
[ -x "$WINE/bin/wineloader" ] || WINE="$HOME/Library/Application Support/Winkod-build/wine"
SUP="$HOME/Library/Application Support/Winkod"
PREFIX="$SUP/bottles/$UUID"
LOG="/tmp/winkod_diag_$UUID.log"

echo "=== Винкод wine diagnostics — bottle $UUID ==="
[ -x "$WINE/bin/wineloader" ] || { echo "FATAL: wineloader not found ($WINE)"; exit 1; }
[ -d "$PREFIX" ] || { echo "FATAL: prefix not found ($PREFIX)"; exit 1; }
echo "wine:   $WINE"
echo "prefix: $PREFIX"

# Resolve exe
if [ -z "$EXE_ARG" ]; then
  EXE_ARG=$(python3 - "$SUP/bottles/manifest.json" "$UUID" <<'PY' 2>/dev/null
import json,sys,urllib.parse
m=json.load(open(sys.argv[1])); u=sys.argv[2]
for b in m:
    if b.get("id")==u:
        p=b.get("primaryExecutable","")
        print(urllib.parse.unquote(p.replace("file://","")))
        break
PY
)
fi
echo "exe:    ${EXE_ARG:-<none>}"
echo

# Step 1 — architecture
if [ -f "$EXE_ARG" ]; then
  echo "--- [1] PE architecture ---"
  file "$EXE_ARG" | sed 's/^/    /'
fi

# Step 2/3 — runtime inventory in the prefix
echo "--- [2] runtime inventory in prefix ---"
DC="$PREFIX/drive_c"
chk() { test -e "$1" && echo "    [yes] $2" || echo "    [ NO] $2"; }
chk "$DC/windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll" ".NET 4.x (Microsoft)"
chk "$DC/windows/mono" "Wine Mono"
ls "$DC/windows/system32/"msvcr*.dll "$DC/windows/system32/"msvcp*.dll "$DC/windows/system32/"vcruntime*.dll 2>/dev/null \
  | sed 's#.*/#    VC++: #' || echo "    [ NO] VC++ runtimes"
chk "$DC/windows/system32/d3dcompiler_47.dll" "d3dcompiler_47"
chk "$DC/windows/Fonts/arial.ttf" "corefonts (arial)"

# Step 3 — clean launch with warn-level log
if [ -f "$EXE_ARG" ]; then
  echo "--- [3] launch (WINEDEBUG=warn, 12s) → $LOG ---"
  WINEPATH_WIN=$(env -i HOME="$HOME" PATH="$WINE/bin:/usr/bin:/bin" WINEPREFIX="$PREFIX" \
    WINEDEBUG=-all DYLD_FALLBACK_LIBRARY_PATH="$WINE/lib64:$WINE/lib:/usr/lib" \
    "$WINE/bin/wineloader" winepath -w "$EXE_ARG" 2>/dev/null)
  env -i HOME="$HOME" PATH="$WINE/bin:/usr/bin:/bin" WINEPREFIX="$PREFIX" \
    WINEDEBUG=warn+module WINEDLLPATH="$WINE/lib/wine" \
    DYLD_FALLBACK_LIBRARY_PATH="$WINE/lib64:$WINE/lib:/usr/lib" \
    "$WINE/bin/wineloader" "${WINEPATH_WIN:-$EXE_ARG}" >"$LOG" 2>&1 &
  PID=$!
  sleep 12
  if pgrep -P $PID >/dev/null 2>&1 || kill -0 $PID 2>/dev/null; then
    echo "    process still alive (likely GUI opened) — good sign"
  fi
  echo "    --- notable log lines ---"
  grep -iE 'err:|fixme:.*not found|import_dll|mscoree|assembly|FileLoad|FileNotFound|cannot find' "$LOG" \
    | head -20 | sed 's/^/    /'
  echo "    (full log: $LOG)"
fi
echo "=== done ==="
