#!/usr/bin/env bash

set -e

WINEPREFIX=$(pwd)/dxvk-test-prefix
export WINEPREFIX

if [[ -f /usr/bin/wine64-development ]]; then
    WINE=/usr/bin/wine64-development
elif [[ -f /usr/bin/wine-development ]]; then
    WINE=wine-development
else
    echo "Could not find wine executable"
    exit 1
fi

# $1 = test expression prefix (can be !)
function check_files {
    # Only checking for system32 (native)
    unix_dlls_path=$( $WINE winepath -u c:\\windows\\system32 2> /dev/null )

    dlls=(
        "dxgi.dll"
        "d3d10.dll"
        "d3d10_1.dll"
        "d3d10core.dll"
        "d3d11.dll"
    )

    for file in "${dlls[@]}"; do
        local full_dll_path=$unix_dlls_path/$file
        echo "Checking for $full_dll_path..."
        test $1 -h $full_dll_path
    done
}

echo "Checking that dxvk-setup is present..."
test -f /usr/bin/dxvk-setup

if [[ -d "$WINEPREFIX" ]]; then
    echo "Removing existing wine prefix..."
    rm -rf $WINEPREFIX
fi

echo "Checking that there is nothing at WINEPREFIX..."
test ! -d $WINEPREFIX

echo "Creating a wine prefix..."
$WINE wineboot

echo "Checking that a wine prefix was created..."
test -d $WINEPREFIX

echo "Checking that the dlls are absent..."
check_files !

echo "Running dxvk-setup..."
dxvk-setup install --yes

echo "Checking that dlls were installed..."
check_files

echo "Uninstalling the dlls..."
dxvk-setup uninstall --yes

echo "Checking that the dlls were removed..."
check_files !

echo "Cleaing up WINEPREFIX..."
rm -rf $WINEPREFIX

echo "Checking that WINEPREFIX was removed..."
test ! -d $WINEPREFIX
