#!/bin/sh -e
# Based on the Debian wrapper script written by Jens Reyer and Michael Gilbert
# SPDX-License-Identifier: LGPL-2.1-or-later

wine32=/usr/bin/wine32
wine64=/usr/bin/wine64

wine32_hint () {
    if [ "$(xbps-uhelper arch)" = "x86_64-musl" ]; then
        echo "wine under musl supports both 64bit & 32bit under the new experimental WoW64 mode"
    else
        echo "it looks like wine-32bit is missing, you should install it."
        if uname -a | grep "x86_64" > /dev/null && xbps-query -l | grep void-repo-multilib > /dev/null; then
            echo "the multilib repository needs to be enabled first.  as root, please"
            echo "execute \"xbps-install -S void-repo-multilib && xbps-install -S wine-32bit\""
        else
            echo "as root, please execute \"xbps-install -S wine-32bit\""
        fi
    fi
}

if test -x $wine64; then
    wine=$wine64
    if test -z "$WINELOADER"; then
        export WINELOADER=$wine64
    fi
    case "x$WINEDEBUG" in
        x-all*|x*,-all*|x*err-all*)
            ;;
        *)
            if ! test -x $wine32; then
	        wine32_hint >&2
	    fi
            ;;
    esac
elif test -x $wine32; then
    wine=$wine32
else
    echo "error: unable to find wine executable.  this shouldn't happen." >&2
    exit 1
fi

if test -z "$WINEDEBUG"; then
    export WINEDEBUG=fixme-all
fi

appname=$(basename "$0")
case "$appname" in
	wine|wine32|wine64) exec $wine "$@" ;;
	*) exec $wine "$appname.exe" "$@" ;;
esac
