#!/bin/bash



BUNDLERESOURCEPATH="$(dirname "$0")/../Resources"



#find wine, try in Bundle, ~/Applications, /Applications, Spotlight
if [ -f "$BUNDLERESOURCEPATH/Wine.bundle/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$BUNDLERESOURCEPATH/Wine.bundle/Contents/Resources"
elif [ -f "$HOME/Applications/Wine.app/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$HOME/Applications/Wine.app/Contents/Resources"
elif [ -f "/Applications/Wine.app/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="/Applications/Wine.app/Contents/Resources"
elif [ -f "$(mdfind 'kMDItemCFBundleIdentifier == org.kronenberg.Wine' | grep -m 1 'Wine.app')/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$(mdfind 'kMDItemCFBundleIdentifier == org.kronenberg.Wine' | grep -m 1 'Wine.app')/Contents/Resources"
else
    echo "Wine not found!"
    exit 1
fi



# create working copy
export WINEPREFIX="$HOME/Library/Application Support/com.dhstudio.sportssoundspro_20161119"
if [ ! -d "$WINEPREFIX" ]; then
    "$BUNDLERESOURCEPATH/Winetricks.app/Contents/MacOS/./Winetricks" "$BUNDLERESOURCEPATH/wineprefix" "$WINEPREFIX" "$(defaults read "$BUNDLERESOURCEPATH/../Info" CFBundleName)"
fi



# exports (we keep X11 for fallback)
export PATH="$WINEUSRPATH/bin":$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$WINEUSRPATH/lib":"/opt/X11/lib":"/usr/X11/lib"
export DYLD_FALLBACK_LIBRARY_PATH="/usr/lib:$WINEUSRPATH/lib":"/opt/X11/lib":"/usr/X11/lib"
export WINEPATH="$WINEUSRPATH/bin"

#some default windows vars that might be missing ( http://ss64.com/nt/syntax-variables.html )
[ -z "$COMPUTERNAME" ] && export COMPUTERNAME="$(/usr/sbin/scutil --get ComputerName)"

# start wine and program, if possible in the programs root directory, change to forwardslashes
BIN_FILE="$(sed 's|\\|/|g' <<< "$(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramPath)")"
BIN_PATH="$BUNDLERESOURCEPATH/wineprefix/drive_c$(sed 's|C:||' <<< "$(dirname "$BIN_FILE")")"
if [ -d "$BIN_PATH" ]; then
    cd "$BIN_PATH"
fi
if [ "$(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramArguments)" != "" ]; then
"$WINEUSRPATH/bin/wine" "$BIN_FILE" $(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramArguments)
else
"$WINEUSRPATH/bin/wine" "$BIN_FILE"
fi

