#!/bin/bash -e

wine32=/usr/lib/wine/wine32
wine64=/usr/lib/wine/wine64
WINEDATADIR=/usr/share/wine

# Use ~/.wine by default
[ -z "$WINEPREFIX" ] && export WINEPREFIX=${HOME}/.wine

wine32_hint () {
    case "x$WINEDEBUG" in
        x-all*|x*,-all*|x*err-all*)
            return
            ;;
    esac

    local winepkgname="wine"
    if [ "$(arch)" = "x86_64" ] ; then
        winepkgname="wine32"
    fi

    echo "it looks like ${winepkgname} package is missing, you should install it."
}


fatal()
{
	echo "$@" >&2
	exit 1
}

# set WINEARCH=wine32 if we need it for the prefix
if [ -z "$WINEARCH" ] && [ -s "$WINEPREFIX/system.reg" ] && [ "$(arch)" = "x86_64" ] ; then
    head "$WINEPREFIX/system.reg" | grep -q "#arch=win32" && WINEARCH=win32
fi

# use wine64 by default
if [ -x $wine64 ] && [ "$WINEARCH" != "win32" ] ; then
    wine=$wine64
elif [ -x $wine32 ]; then
    wine=$wine32
else
    echo "error: unable to find wine executable.  this shouldn't happen." >&2
    wine32_hint >&2
    exit 1
fi

WINEADMINST=/usr/bin/wine_adminst
if [ "$1" = "--admin" ] || [ "$1" = "--attach" ] || [ -n "$ATTACH_MODE" ]; then
    if [ -r "$WINEADMINST" ] ; then
        . $WINEADMINST
    else
        fatal "Был использован параметр \"$1\", но скрипт \"$WINEADMINST\" не найден."
    fi
fi

exec -a "$0" $wine "$@"
