#!/bin/bash
cd /mnt/c

WIN_USERNAME=$(cmd.exe /c echo %USERNAME% 2>/dev/null | tr -d '\r')
WIN_OP_BASE="/mnt/c/Users/$WIN_USERNAME/AppData/Local/Microsoft/WinGet/Packages"
OP_DIR=$(ls -td "$WIN_OP_BASE"/AgileBits.1Password.CLI* 2>/dev/null | head -n1)

if [ -z "$OP_DIR" ]; then
    echo "[ERROR] Could not find 1Password CLI folder in $WIN_OP_BASE" >&2
    exit 1
fi

mapfile -d '' op_env_vars < <(env -0 | grep -z ^OP_ | cut -z -d= -f1)
export WSLENV="${WSLENV:-}:$(IFS=:; echo "${op_env_vars[*]}")"
exec "$OP_DIR/op.exe" "$@"
