#!/bin/bash
me=pipeasio-register

WINEPREFIX=${WINEPREFIX:=~/.wine}

if [ ! -d "${WINEPREFIX}" ]; then
    wineboot -u || exit $?
fi

# Candidate install roots, in preference order.
candidates=(
    "${PIPEASIO_PREFIX:+${PIPEASIO_PREFIX}/lib/wine}"
    "${HOME}/.local/lib/wine"
    "/usr/lib/wine"
    "/usr/lib64/wine"
    "/usr/lib/x86_64-linux-gnu/wine"
    "/opt/wine-devel/lib64/wine"
    "/opt/wine-stable/lib64/wine"
    "/opt/wine-staging/lib64/wine"
)

prefix=""
for p in "${candidates[@]}"; do
    [ -z "${p}" ] && continue
    if [ -e "${p}/x86_64-unix/pipeasio64.dll.so" ] \
       && [ -e "${p}/x86_64-windows/pipeasio64.dll" ]; then
        prefix="${p}"
        break
    fi
done

if [ -z "${prefix}" ]; then
    >&2 echo "[${me}] PipeASIO install not found in any candidate root."
    >&2 echo "[${me}] Searched: ${candidates[*]}"
    >&2 echo "[${me}] Set PIPEASIO_PREFIX=<install-prefix> or run 'cmake --install'."
    exit 1
fi

>&2 echo "[${me}] using PipeASIO install at ${prefix}"

# Stage the PE half where regsvr32 can LoadLibrary it by name.
cp -v --no-preserve mode "${prefix}/x86_64-windows/pipeasio64.dll" \
                         "${WINEPREFIX}/drive_c/windows/system32/" || exit $?

# Let Wine find the ELF half next to the PE half.
export WINEDLLPATH="${prefix}${WINEDLLPATH:+:${WINEDLLPATH}}"
wine regsvr32 pipeasio64.dll
code=$?

# Optional 32-bit WoW64 registration.
if [ ${code} -eq 0 ] \
   && [ -e "${prefix}/i386-windows/pipeasio32.dll" ] \
   && [ -e "${prefix}/x86_64-unix/pipeasio32.so" ]; then
    >&2 echo "[${me}] registering experimental WoW64 32-bit front end"
    mkdir -p "${WINEPREFIX}/drive_c/windows/syswow64" || exit $?
    cp -v --no-preserve mode "${prefix}/i386-windows/pipeasio32.dll" \
                             "${WINEPREFIX}/drive_c/windows/syswow64/" || exit $?
    wine reg add \
        'HKCR\CLSID\{2D3CA9E2-1193-4C5D-B5FD-38798F3DC074}\InprocServer32' \
        /ve /t REG_SZ /d 'pipeasio32.dll' /f /reg:32 || exit $?
    wine reg add \
        'HKCR\CLSID\{2D3CA9E2-1193-4C5D-B5FD-38798F3DC074}\InprocServer32' \
        /v ThreadingModel /t REG_SZ /d Apartment /f /reg:32 || exit $?
    wine reg add \
        'HKCU\Software\Wine\DllOverrides' \
        /v pipeasio32 /t REG_SZ /d builtin /f /reg:32 || exit $?
    wine reg add \
        'HKLM\Software\ASIO\PipeASIO' \
        /v CLSID /t REG_SZ /d '{2D3CA9E2-1193-4C5D-B5FD-38798F3DC074}' /f /reg:32 || exit $?
    wine reg add \
        'HKLM\Software\ASIO\PipeASIO' \
        /v Description /t REG_SZ /d 'PipeASIO Driver' /f /reg:32 || exit $?
    >&2 echo "[${me}] 32-bit WoW64 front end registered"
fi

if [ ${code} -eq 0 ]; then
    >&2 echo "[${me}] registered in ${WINEPREFIX}"
    if [ "${prefix#${HOME}/}" != "${prefix}" ]; then
        >&2 echo "[${me}] for Proton/Faugus, set WINEDLLPATH=${prefix} in the launcher env."
    fi
else
    >&2 echo "[${me}] regsvr32 failed (status ${code})"
fi
exit ${code}
