#!/usr/bin/env bash

WINEPREFIX=~/.wine
export WINEPREFIX

set -e

# $1 = test expression prefix (can be !)
function check_files {
    unix_64bit_dlls_path=$( wine winepath -u c:\\windows\\sysnative 2> /dev/null )
    unix_32bit_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
        full_64bit_file_path=$unix_64bit_dlls_path/$file
        echo "Checking for $full_64bit_file_path..."
        test $1 -h $full_64bit_file_path

        full_32bit_file_path=$unix_32bit_dlls_path/$file
        echo "Checking for $full_32bit_file_path..."
        test $1 -h $full_32bit_file_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..."
wine64 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 "Cleaing up WINEPREFIX..."
rm -rf $WINEPREFIX

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