#!/bin/sh
# VolnOS — open a Windows .exe/.msi by routing it to the on-demand Wine Flatpak.
# Installed to /usr/bin/volnos-run-exe and wired to .exe/.msi via
# volnos-wine-launcher.desktop + /etc/xdg/mimeapps.list.
#
# Wine is no longer baked into the image; it installs on demand (VolnOS Setup or the
# Software Center) as the org.winehq.Wine Flatpak into the /data system installation,
# so it survives OTA updates. This wrapper runs the file with that Flatpak if present,
# or guides the user to install Windows-app support if it isn't.
set -u

APP="org.winehq.Wine"
FILE="${1:-}"

# Installed in ANY installation (the /data 'volnos' system install, --user, etc.)?
if flatpak info "$APP" >/dev/null 2>&1; then
    case "$FILE" in
        *.msi|*.MSI) exec flatpak run "$APP" msiexec /i "$FILE" ;;
        *)           exec flatpak run "$APP" "$FILE" ;;
    esac
fi

# Not installed yet — explain, and offer to open the Software Center to get it.
MSG="Windows app support isn't installed yet.

Open VolnOS Setup or the Software Center and install \"Windows app support (Wine)\", then try opening this file again."
if command -v zenity >/dev/null 2>&1; then
    zenity --warning --no-wrap --title="VolnOS — Windows apps" --text="$MSG"
elif command -v notify-send >/dev/null 2>&1; then
    notify-send "VolnOS — Windows apps" "$MSG"
else
    printf '%s\n' "$MSG" >&2
fi
if command -v volnos-software-center >/dev/null 2>&1; then
    volnos-software-center >/dev/null 2>&1 &
fi
exit 1
