#!/usr/bin/bash

#获取默认的wine
DEFAULT_BIN=""
get_default_bin() {
    if [ -f "/opt/kylin-wine/wine10/bin/kylin-wine" ]; then
        DEFAULT_BIN="/opt/kylin-wine/wine10/bin/kylin-wine"
    elif [ -f "/opt/kylin-wine/wine10-w64/bin/kylin-wine" ]; then
        DEFAULT_BIN="/opt/kylin-wine/wine10-w64/bin/kylin-wine"
    elif [ -f "/opt/kylin-wine/wine8/bin/kylin-wine" ]; then
        DEFAULT_BIN="/opt/kylin-wine/wine8/bin/kylin-wine"
    elif [ -f "/opt/kylin-wine/wine8-32/bin/kylin-wine" ]; then
        DEFAULT_BIN="/opt/kylin-wine/wine8-32/bin/kylin-wine"
    else
        return 1
    fi

    return 0
}

#kylin-wine --default 供wine助手获取默认的wine
if [ "$#" -eq 1 ] && [ "$1" == "--default" ]; then
    get_default_bin
    echo ${DEFAULT_BIN}
    exit 0
fi

BOTTLE_CONF=${HOME}/.wine/.wine-assistant/wabottle.json
if [ -n "$WINEPREFIX" ]; then
    BOTTLE_CONF=${WINEPREFIX}/.wine-assistant/wabottle.json
fi

WINE_BIN=""
if [ -f "$BOTTLE_CONF" ]; then
    WINE_BIN=$(sed -E 's/.*"bin":\s*"([^"]+)".*/\1/' ${BOTTLE_CONF})
fi

if [ ! -f "$WINE_BIN" ]; then
    get_default_bin
    if [ $? -eq 1 ]; then
        echo "Wine is not installed !"
        exit 1
    fi
    WINE_BIN=${DEFAULT_BIN}
fi


$WINE_BIN "$@"
