#!/bin/bash

export WINEDEBUG=-all

dlls_dir=`dirname "$(readlink -f $0)"`
build_arch='@arch@'
winelib='@winelib@'

if [ $winelib == 'True' ]; then
    dll_ext='dll.so'
    # strip off the trailing bindir to get to the prefix, then append libdir
    dlls_dir="${dlls_dir%/@bindir@}/@libdir@"
else
    dll_ext='dll'
fi

if [ ! -f "$dlls_dir/d3d11.$dll_ext" ] || [ ! -f "$dlls_dir/dxgi.$dll_ext" ]; then
    echo "d3d11.$dll_ext or dxgi.$dll_ext not found in $dlls_dir" >&2
    exit 1
fi

if [ -z "$wine" ]; then
    if [ $build_arch == "x86_64" ]; then
        wine="wine64"
    else
        wine="wine"
    fi
fi

winever=`$wine --version | grep wine`
if [ -z "$winever" ]; then
    echo "$wine:"' Not a wine executable. Check your $wine.' >&2
    exit 1
fi

quiet=false
assume=

function ask {
    echo "$1"
    if [ -z "$assume" ]; then
        read continue
    else
        continue=$assume
        echo "$continue"
    fi
}

POSITIONAL=()
while [[ $# -gt 0 ]]; do

    case $1 in
    -y)
        assume='y'
        shift
        ;;
    -n)
        assume='n'
        shift
        ;;
    -q|--quiet)
        quiet=true
        assume=${assume:-'y'}
        shift 
        ;;
    *)
        POSITIONAL+=("$1")
        shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}"

if [ "$quiet" = true ]; then
    exec >/dev/null
fi

if [ -z "$WINEPREFIX" ]; then
    ask "WINEPREFIX is not set, continue? (y/N)"
    if [ "$continue" != "y" ] && [ "$continue" != "Y" ]; then
    exit 1
    fi
else
    if ! [ -f "$WINEPREFIX/system.reg" ]; then
        ask "WINEPREFIX does not point to an existing wine installation. Proceeding will create a new one, continue? (y/N)"
        if [ "$continue" != "y" ] && [ "$continue" != "Y" ]; then
        exit 1
        fi
    fi
fi
unix_sys_path="$($wine winepath -u 'C:\windows\system32')"
if [ $? -ne 0 ]; then
    exit 1
fi


ret=0

function removeOverride {
    echo "    [1/2] Removing override... "
    $wine reg add 'HKEY_CURRENT_USER\Software\Wine\DllOverrides' /v $1 /d builtin /f > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Failed"
        exit 1
    fi
    local dll="$unix_sys_path/$1.dll"
    echo "    [2/2] Removing link... "
    if [ -h "$dll" ]; then
        out=$(rm "$dll" 2>&1)
        if [ $? -ne 0 ]; then
            ret=2
            echo -e "$out"
        fi
    else
        echo -e "'$dll' is not a link or doesn't exist."
        ret=2
    fi
}

function createOverride {
    echo "    [1/2] Creating override... "
    $wine reg add 'HKEY_CURRENT_USER\Software\Wine\DllOverrides' /v $1 /d native /f >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo -e "Failed"
        exit 1
    fi
    echo "    [2/2] Creating link to $1.$dll_ext... "
    ln -sf "$dlls_dir/$1.$dll_ext" "$unix_sys_path/$1.dll"
    if [ $? -ne 0 ]; then
        echo -e "Failed"
        exit 1
    fi
}

case "$1" in
uninstall)
    fun=removeOverride
    ;;
install)
    fun=createOverride
    ;;
*)
    echo "Unrecognized option: $1"
    echo "Usage: $0 [install|uninstall] [-q|--quiet] [-y|-n]"
    exit 1
    ;;
esac

echo '[1/5] dxgi:'
$fun dxgi
echo '[2/5] d3d10:'
$fun d3d10
echo '[3/5] d3d10_1:'
$fun d3d10_1
echo '[4/5] d3d10core:'
$fun d3d10core
echo '[5/5] d3d11:'
$fun d3d11
exit $ret