#!/bin/sh
# Regression test for winetricks
#
# Copyright (C) 2010-2014 Dan Kegel
# Copyright (C) 2011-2017 Austin English
# See also copyright notice in src/winetricks.
#
# This software comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the GNU Lesser
# Public License version 2.1 (or later), as published by the Free
# Software Foundation. Please see the file COPYING for details.

# Requires wine >= 2.8 (https://bugs.winehq.org/show_bug.cgi?id=37811)

# TODO:
# add selfupdate tests
# automate non -q verbs with autohotkey

# Override this if you want to put the work area on a different disk
WINE_PREFIXES=${WINE_PREFIXES:-$HOME/winetrickstest-prefixes}
XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
cache="$XDG_CACHE_HOME/winetricks"

#set -e
set -x

if [ -f src/winetricks ] ; then
    TOP="$PWD"
    shwinetricks="${PWD}/src/winetricks"
elif [ -f ../src/winetricks ] ; then
    # realpath isn't available on OSX, use a subshell instead:
    TOP="$(cd .. && echo "$PWD")"
    shwinetricks="${TOP}/src/winetricks"
elif [ -f ../../src/winetricks ] ; then
    # realpath isn't available on OSX, use a subshell instead:
    TOP="$(cd ../.. && echo "$PWD")"
    shwinetricks="${TOP}/src/winetricks"
else
    echo "Running from unknown directory. Exiting"
    exit 1
fi

# Start with a fresh output dir each time:
outputdir="${TOP}/output"
rm -rf "${outputdir}"
mkdir -p "${outputdir}"

# Workaround for:
# kcov has trouble with forking shell scripts
# https://github.com/SimonKagstrom/kcov/issues/64
# https://github.com/SimonKagstrom/kcov/issues/165

# check-deps checks for time/cabextract, but doesn't invoke winetricks so we don't care about it for coverage
# If the below code runs, it creates an empty 'kcov-results' dir in the root directory. So, bail out here:
if [ "$1" = "check-deps" ] ; then
    true

# Using an environmental variable rather than a CLI option
# so it doesn't need extra handling/targets in Makefile
elif [ -n "$WINETRICKS_ENABLE_KCOV" ] ; then
    KCOV_RESULTS="${outputdir}/kcov-results"
    rm -rf "$KCOV_RESULTS"
    mkdir -p "$KCOV_RESULTS"

    # kcov --kcov-options output-dir testscript --test-script-args
    # I think kcov lets you append results, if not we may have to use separate for each invocation then combine at the end
    # It seems to be capping out at 468 lines for me. Mentioned on their github here:
    # https://github.com/SimonKagstrom/kcov/issues/165

    # Previously, winetricks-test used `$WINETRICKS`. That fails for kcov, which
    # then tracks bash itself and not winetricks. `${shwinetricks}` works fine.
    WINETRICKS="kcov --configure=bash-use-basic-parser=1 $KCOV_RESULTS \"${shwinetricks}\""
    #WINETRICKS="kcov --configure=bash-use-basic-parser=1 $KCOV_RESULTS-$(date +%k-%M-%S) \"${shwinetricks}\""
else
    WINETRICKS="${shwinetricks}"
fi

# Disable winetricks update checks:
WINETRICKS_LATEST_VERSION_CHECK=disabled
export WINETRICKS_LATEST_VERSION_CHECK

# Disable some verbose output that makes parsing harder:
WINETRICKS_SUPER_QUIET=1
export WINETRICKS_SUPER_QUIET

# verbs known to not work in -q mode yet
BLACKLIST="dx8sdk|kde|psdk2003|psdkwin7"
# verbs that hang in -q because of simple problem we should work around soon
BLACKLIST="$BLACKLIST|vc2005trial"
# verbs that are too slow
BLACKLIST="$BLACKLIST|bfbc2|dxsdk_nov2006|dxsdk_jun2010"
# steam verbs I don't have
BLACKLIST="$BLACKLIST|borderlands|supermeatboy|trine"
# broken http://bugs.winehq.org/show_bug.cgi?id=26411:
BLACKLIST="$BLACKLIST|mfsxde|mfsx_demo"
# hang lately, probably ui changes in install process
BLACKLIST="$BLACKLIST|bioshock2"
# broken/flaky, http://bugs.winehq.org/show_bug.cgi?id=26016
BLACKLIST="$BLACKLIST|xmllite"
# redundant metaverbs
BLACKLIST="$BLACKLIST|allcodecs"
# https://bugs.winehq.org/show_bug.cgi?id=16876
BLACKLIST="$BLACKLIST|wmi"

# Travis CI is apparently blocking archive.org, which breaks several verbs:
# https://github.com/travis-ci/travis-ci/issues/6798
# Blacklist those verbs on those hosts:
# FIXME: setup another test target for torify, use it to d/l these?
ARCHIVE_ORG_BLACKLIST="atmlib|avifil32|cabinet|cmd|comctl32ocx|comdgl32ocx|gmdls|mdac27|mfc40|msflxgrd|mshflxgd"
ARCHIVE_ORG_BLACKLIST="$ARCHIVE_ORG_BLACKLIST|msls31ms|msmask|msscript|msxml3|pdh|pngfilt|riched30"
ARCHIVE_ORG_BLACKLIST="$ARCHIVE_ORG_BLACKLIST|richtx32|secur32|tabctl32|usp10|"

# See https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
if [ "$TRAVIS" = "true" ] ; then
    BLACKLIST="$ARCHIVE_ORG_BLACKLIST|$BLACKLIST"
fi

# Tests that fail under Xvfb
XVFB_DOTNET_BLACKLIST="dotnet11|dotnet11sp1|dotnet20|dotnet20sdk|dotnet20sp1|dotnet30|dotnet40|dotnet46"
XVFB_BLACKLIST="$XVFB_DOTNET_BLACKLIST|adobeair|binkw32|dirac|directmusic|dxdiag|flash|gdiplus_winxp|gfw|ie6|ie7|ie8"
XVFB_BLACKLIST="$XVFB_BLACKLIST|jet40|nuget|quicktime72|vcrun2008|vcrun2010|vcrun2012|vcrun2013|vcrun2015"
XVFB_BLACKLIST="$XVFB_BLACKLIST|vjrun20|windowscodecs|wmi|wmp9|wmp10|wsh56js|wsh56vb|xmllite|xna31|xna40|xvid"

# Verbs known to update frequently
QUICKCHECK="flash steam"

failed_verbs=""
passes=0
errors=0
skips=0

# Check for programs this script (or winetricks) uses.
# Better to find out they're missing now than in the
# middle of a two day run.
check_deps() {
    for tool in time cabextract; do
        command -v "$tool"
        ret=$?

        if [ ! $ret -eq 0 ] ; then
            echo "Please install $tool."
            exit 1
        fi
    done
}

fail()
{
    echo "FAIL: $*"
    failed_verbs="${failed_verbs}|$*"
    errors=$((errors + 1))
}

pass()
{
    echo "PASS: $*"
    passes=$((passes + 1))
}

skip()
{
    echo "SKIP: $*"
    skips=$((skips + 1))
    was_skipped=1
}

w_die()
{
    echo "$*"
    exit 1
}

w_time()
{
    # OSX time doesn't support -o, so try it first:
    if ! /usr/bin/time -o "${outputdir}/test.log" echo test > /dev/null 2>&1; then
        /usr/bin/time -p "$@"
    else
        /usr/bin/time -p -o "${outputdir}/time.log" "$@"
    fi
}

case "$LANG" in
    ""|"C") echo "Some games won't install in the Posix locale; doing 'export LANG=en_US.UTF-8'" ; export LANG=en_US.UTF-8;;
esac

case "$OS" in
    "Windows_NT")
        # Mostly unimplemented...
        # Cheezy fix for getting rid of double slashes when running cygwin in wine
        case "$HOME" in
            /) HOME="" ;;
        esac

        WINE=""
        WINESERVER=true
        #DRIVE_C="C:/"
        ;;
    *)
        export WINE=${WINE:-wine}
        # Find wineserver.  Some distros (Debian) don't have it on the path,
        # on the mistaken understanding that user scripts never need it :-(
        # If wineserver is from wine-development set WINE to wine-development.
        # FIXME: get packagers to put wineserver on the path.
        for x in \
            "$WINESERVER" \
            "${WINE}server" \
            "$(command -v wineserver 2> /dev/null)" \
            "$(dirname $WINE)/server/wineserver" \
            /usr/lib/wine/wineserver \
            /usr/lib/i386-kfreebsd-gnu/wine/wineserver \
            /usr/lib/i386-linux-gnu/wine/wineserver \
            /usr/lib/powerpc-linux-gnu/wine/wineserver \
            /usr/lib/i386-kfreebsd-gnu/wine/bin/wineserver \
            /usr/lib/i386-linux-gnu/wine/bin/wineserver \
            /usr/lib/powerpc-linux-gnu/wine/bin/wineserver \
            /usr/lib/x86_64-linux-gnu/wine/bin/wineserver \
            /usr/lib/i386-kfreebsd-gnu/wine-development/wineserver \
            /usr/lib/i386-linux-gnu/wine-development/wineserver \
            /usr/lib/powerpc-linux-gnu/wine-development/wineserver \
            /usr/lib/x86_64-linux-gnu/wine-development/wineserver \
            file-not-found
    do
        if [ -x "$x" ] ; then
            case "$x" in
                /usr/lib/*/wine-development/wineserver)
                    if [ -x /usr/bin/wine-development ] ; then
                        WINE="/usr/bin/wine-development"
                    fi
                    ;;
            esac
            break
        fi
    done

    case "$x" in
        file-not-found) w_die "wineserver not found!" ;;
        *) WINESERVER="$x" ;;
    esac
    ;;
esac

test_speed()
{
    # shellcheck disable=SC2086
    if ! w_time $XVFB $WINETRICKS nocrashdialog "$1" > "${outputdir}/foo.log"; then
        fail "winetricks $1 returned status $?"
    fi

    # shellcheck disable=SC2046
    if [ $(wc -l < "${outputdir}/foo.log") -lt 5 ] ; then
        fail "winetricks $1 returned too few lines"
    fi

    if [ ! -f time.log ] ; then
        # OSX, fake it:
        seconds=0
    else
        seconds=$(awk '/real/ {print $2}' < "${outputdir}/time.log" | sed 's/\..*//')
    fi

    echo "test_speed: winetricks $1 took $seconds seconds"
    # Longest runtime as of 11 Dec 2010 is 5 seconds on an e8400 with cygwin
    # shellcheck disable=SC2086
    if [ $seconds -gt 7 ] ; then
        fail "test_speed: winetricks $1 took $seconds seconds"
    fi
}

# Return the number of blocks available in the system
total_df()
{
    df | grep -v /dev/loop | awk '/^\// { sum += $4 } END { print sum }'
}

# for single apps, wrapper around test_command:
test_app()
{
    app=$1
    was_skipped=0

    # Watch transient disk space
    DF_START=$(total_df)
    if [ -d "$W_CACHE/$app" ] ; then
        DU_CACHE_START=$(du -s "$W_CACHE/$app" | awk '{print $1}')
    else
        DU_CACHE_START=0
    fi
    touch "${outputdir}/df-daemon"
    (set +x; while test -f "${outputdir}/df-daemon"; do total_df; sleep 1; done ) > "${outputdir}/df-during.log" &

    test_command --verify "$app"

    if [ $was_skipped = 1 ]; then
        skip "post-install file check for skipped verb $app"
    else
        # Post install:
        # Don't check whether metaverbs are installed
        case "$app" in
            allcodecs) ;;
            *)
                # if test was skipped because of wrong arch, don't check for it:
                if [ "$archskip" = "1" ]; then
                    skip "$app was skipped because of wrong prefix arch!"
                    return
                fi

                # no xvfb needed
                "${shwinetricks}" -q list-installed > "${outputdir}/list-installed.out"

                if ! grep -w "$app" "${outputdir}/list-installed.out"; then
                    fail "test app $app not installed after install?"
                fi

                ;;
        esac

        # Cleanup..
        echo "rm ${outputdir}/df-daemon"
        rm "${outputdir}/df-daemon"

        # Total max disk usage = max df change plus any initial blocks in cache
        DF_MIN=$(awk '{ if (min == "" || $1 < min) min=$1; } END {printf "%d\n", min}' < "${outputdir}/df-during.log" )
        DF_DIFF=$((DF_START - DF_MIN))
        TOTAL=$((DF_DIFF + DU_CACHE_START))
        echo "test_app: ${app}: max_disk $TOTAL blocks."
        TOTAL_MB=$((TOTAL / 1024))
        mkdir -p "${outputdir}/measurements"
        echo "${app}:size_MB=${TOTAL_MB},time_sec=${seconds}" >> "${outputdir}/measurements/$app.dat"

        # Cleanup the prefix, unless $W_OPT_NOCLEAN is set to 1 (to match winetricks):
        if [ "$W_OPT_NOCLEAN" = "1" ]; then
            echo "W_OPT_NOCLEAN set to 1, not removing wineprefix $WINEPREFIX"
        else
            echo "Removing wineprefix $WINEPREFIX"
            # FIXME: for some reason, steam doesn't clean up. $WINESERVER -k doesn't help either
            rm -rf "$WINEPREFIX"
        fi
    fi
}

test_command()
{
    archskip=0
    command="$*"

    # This previously changed spaces to hyphens. Going back to spaces so we test WINEPREFIXes
    # with special characters, to prevent issues like https://github.com/Winetricks/winetricks/issues/995
    export WINEPREFIX="$WINE_PREFIXES/${command}"
    #DRIVE_C="$WINEPREFIX/dosdevices/c:"

    # always use a clean $WINEPREFIX
    if [ -d "$WINEPREFIX" ] ; then
        rm -rf "$WINEPREFIX"
    fi

    # Isolate us from the user's home directory
    # shellcheck disable=SC2086
    $XVFB $WINETRICKS sandbox

    echo "Installing $command"

    # shellcheck disable=SC2086
    $XVFB $WINETRICKS --no-isolate -q nocrashdialog "$@"
    return=$?

    if [ "$return" = "32" ] && [ "$WINEARCH" != "win32" ]; then
        skip "$command is not supported on $WINEARCH, requires win32"
        archskip=1
    elif [ "$return" = "64" ] && [ "$WINEARCH" != "win64" ]; then
        skip "$command is not supported on $WINEARCH, requires win64"
        archskip=1
    fi

    if [ "$EXPECT_FAIL" = "yes" ] ; then
        # A success is failure:
        if [ "$return" = "0" ] ; then
            fail "$command succeeded, should have failed"
        else
            pass "test_command $command expected to fail, and did fail!"
            return
        fi
    else
        echo "winetricks $* completed"
    fi

    if [ ! -f time.log ] ; then
        seconds=0
    else
        seconds=$(awk '/real/ {print $2}' < time.log | sed 's/\..*//')
    fi

    echo "test_app: ${app}: install_time $seconds seconds."

    # Cleanup:
    for x in $command; do
        case $x in
            dotnet*)
                # bug in wine?  the wineconsoles are for the langpacks, and seem empty.
                if pgrep wineconsole; then
                    killall wineconsole
                fi
                ;;
            fontxplorer)
                # a number of apps open a folder on the desktop
                # FIXME: this should this be in the verb
                if pgrep winefile ; then
                    killall winefile.exe
                fi
                ;;
            hegemony*)
                # FIXME: this should this be in the verb
                if pgrep Launcher ; then
                    killall Launcher.exe
                fi
                ;;
            wmi)
                # wmi starts a service
                # FIXME: this should this be in the verb
                if pgrep WinMgmt ; then
                    killall WinMgmt.exe
                fi
                ;;
        esac
    done

    # Arcania-Gothic4, et al
    if pgrep notepad.exe ; then
        killall notepad.exe
    fi

    echo "Checking for dangling processes!"
    # shellcheck disable=SC2009
    ps augxw | grep \\.exe
    "$WINESERVER" -w
    echo "Wineserver done."

    pass "$@"
}

test_custom_verbs()
{
    # Custom .verb support isn't commonly used, and may break without notice for a while
    # Also try with --isolate:
    # https://github.com/Winetricks/winetricks/issues/599

    # Test as apps first, then dll, since they take different codepaths

    # First, a working 'app' as an app:
cat > "${outputdir}/true.verb" <<_EOF
w_metadata true apps

load_true()
{
    echo "true should succeed"
    /bin/true
}

_EOF

    # Next, a broken 'app' as an app:
cat > "${outputdir}/false.verb" <<_EOF
w_metadata false apps

load_false()
{
    echo "false should fail"
    false
}

_EOF

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/true.verb" ; ret=$?
    case $ret in
        0) pass "${outputdir}/true.verb not isolated, as apps passed" ;;
        *) fail "${outputdir}/true.verb not isolated, as apps failed" ;;
    esac

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/false.verb" ; ret=$?
    case $ret in
        0) fail "false.verb not isolated, as apps worked, should have failed" ;;
        1) pass "false.verb not isolated, as apps passed" ;;
        *) fail "false.verb not isolated, as apps failed in unexpected way" ;;
    esac

    # no xvfb needed
    $WINETRICKS --isolate "${outputdir}/true.verb" ; ret=$?
    case $ret in
        0) pass "${outputdir}/true.verb isolated, as apps passed" ;;
        *) fail "${outputdir}/true.verb isolated, as apps failed" ;;
    esac

    # no xvfb needed
    $WINETRICKS --isolate "${outputdir}/false.verb" ; ret=$?
    case $ret in
        0) fail "${outputdir}/false.verb isolated, as apps worked, should have failed" ;;
        1) pass "${outputdir}/false.verb isolated, as apps passed" ;;
        *) fail "${outputdir}/false.verb isolated, as apps failed in unexpected way" ;;
    esac

    # Repeat as dll:

    # First, a working 'app' as a dll:
cat > "${outputdir}/true.verb" <<_EOF
w_metadata true dlls

load_true()
{
    echo "true should succeed"
    true
}
_EOF

    # Next, a broken 'app' as a dll:
cat > "${outputdir}/false.verb" <<_EOF
w_metadata false dlls

load_false()
{
    echo "false should fail"
    false
}
_EOF

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/true.verb" ; ret=$?
    case $ret in
        0) pass "true.verb isolated, as dlls passed" ;;
        *) fail "true.verb isolated, as dlls failed" ;;
    esac

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/false.verb" ; ret=$?
    case $ret in
        0) fail "${outputdir}/false.verb isolated, as dlls worked, should have failed" ;;
        1) pass "${outputdir}/false.verb isolated, as dlls passed" ;;
        *) fail "${outputdir}/false.verb isolated, as dlls failed in unexpected way" ;;
    esac

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/true.verb" ; ret=$?
    case $ret in
        0) pass "${outputdir}/true.verb isolated, as dlls passed" ;;
        *) fail "${outputdir}/true.verb isolated, as dlls failed" ;;
    esac

    # no xvfb needed
    $WINETRICKS --no-isolate "${outputdir}/false.verb" ; ret=$?
    case $ret in
        0) fail "${outputdir}/false.verb isolated, as dlls worked, should have failed" ;;
        1) pass "${outputdir}/false.verb isolated, as dlls passed" ;;
        *) fail "${outputdir}/false.verb isolated, as dlls failed in unexpected way" ;;
    esac

    rm "${outputdir}/false.verb" "${outputdir}/true.verb"
    pass
}

test_dlls()
{
    # no xvfb needed, kcov breaks
    "${shwinetricks}" list-manual-download > "${outputdir}/manual.log"
    "${shwinetricks}" dlls list | awk '{print $1}' > "${outputdir}/dlls.log"
    if grep .------------------- "${outputdir}/dlls.log" ; then
        fail "output of dlls list contained garbage"
        exit 1
    fi

    sort -u < "${outputdir}/dlls.log" | grep -F -w -v -f "${outputdir}/manual.log" | grep -E -v "$BLACKLIST" > "${outputdir}/dlls.verbs"

    if [ ! -f "${outputdir}/dlls.verbs" ] ; then
        w_die "${outputdir}/dlls.verbs doesn't exist? No verbs to test!"
    elif [ -f "${outputdir}/dlls.verbs" ] && [ ! -s "${outputdir}/dlls.verbs" ] ; then
        w_die "${outputdir}/dlls.verbs exists, but it is empty"
    else
        echo "Testing these verbs:"
        cat "${outputdir}/dlls.verbs"
    fi

    while IFS= read -r line; do
        test_app "$line"
    done < "${outputdir}/dlls.verbs"
}

test_dotnet()
{
    # verify that each individual installer works:
    for x in dotnet11 dotnet11sp1 \
            dotnet20 dotnet20sp1 dotnet20sp2 \
            dotnet30 dotnet30sp1 dotnet35 dotnet35sp1 \
            dotnet40 dotnet45 dotnet452 dotnet46 dotnet461 dotnet462 dotnet472; do
        echo "testing $x"
        test_command --verify $x
    done

    # FIXME: add other possible combinations

    # combinations that should work:
    for combo in "dotnet20 dotnet20sp2"; do
        # shellcheck disable=SC2086
        test_command --verify $combo
    done

    # combinations that should break:
    for fail_combo in "dotnet11 dotnet20"; do
        # shellcheck disable=SC2086
        EXPECT_FAIL=yes test_command $fail_combo
    done
}

test_install_cached_or_download()
{
    # no xvfb needed, kcov breaks
    "${shwinetricks}" list-cached list-download > "${outputdir}/ticd.log"
    if grep .------------------- ticd.log ; then
        fail "output of list-cached list-download contained garbage"
        exit 1
    fi
    sort -u < ticd.log | grep -E -v "$BLACKLIST" > ticd.verbs

    if true ; then
        # Split into two, only do half
        nverbs=$(wc -l < ticd.verbs )
        firsthalf=$((nverbs / 2 + 1))
        sed "${firsthalf}"',$d' < ticd.verbs > ticd-1.verbs
        sed '1,'"${firsthalf}"d   < ticd.verbs > ticd-2.verbs
        VERBS=$(cat ticd-1.verbs)
    else
        VERBS=$(cat ticd.verbs)
    fi

    for a in $VERBS; do
        test_app "$a"
    done

    # no xvfb needed, kcov breaks
    "${shwinetricks}" list-cached | sort > cached.txt

    # Verbs that are just wrappers around others don't detect cache/install state yet.
    # Verbs that are just informative placeholders don't ever download (gecko).
    # And some verbs (gecko110) don't usually install for other reasons.
    BLACKLIST_CACHE="allfonts|cjkfonts|allcodecs|gecko|gecko110|fontfix"

    grep -E -vw "$BLACKLIST_CACHE" ticd.verbs > download.txt
    comm -23 download.txt cached.txt > download-but-not-cached.txt
    echo "Supposedly downloadable"
    cat download.txt
    echo "Cached"
    cat cached.txt
    echo "Downloadable but not cached"
    cat download-but-not-cached.txt

    # shellcheck disable=SC2046
    if [ $(wc -l < download-but-not-cached.txt) != 0 ] ; then
        fail "test_install_cached_or_download: asked to install all downloadable apps, but some not listed as cached afterwards"
        cat download-but-not-cached.txt
    fi
}

test_quick()
{
    echo "warning: quick test takes up around 20GB"
    # Test the frequent download-changes-checksum offenders first
    export W_CACHE="$cache"
    for a in $QUICKCHECK; do
        # shellcheck disable=SC2115
        rm -rf "$WINE_PREFIXES/$a"
        test_app "$a"
    done

    # And test all the automatically-downloadable dlls
    test_dlls
}

test_full() {
    test_quick
    test_dotnet
    test_speed list
    test_speed list-download
    #test_install_cached_or_download
    test_speed list-cached
    test_speed list-installed
    test_custom_verbs
    test_w_compare_wine_version
}

test_xvfb() {
    if [ ! "$(command -v xvfb-run 2>/dev/null)" ] ; then
        w_die "Please install xvfb-run for xvfb tests"
    fi

    BLACKLIST="$BLACKLIST|$XVFB_BLACKLIST"
    export BLACKLIST

    # Not test_quick() since flash fails without proper X, but test_quick() doesn't respect $BLACKLIST
    # Also, we don't really want to duplicate those tests twice, as this is for TravisCI where time is limited..
    test_dlls
}

helper_test_w_compare_wine_version()
{
    version="$1" # version we're pretending to be (e.g., wine-2.0)
    comparison="$2" # the comparison to make (e.g., ',1.8', '1.9,', or '1.8,1.9')
    workaround_expected="$3" # should the workaround be executed (yes/no)

    WINETRICKS_WINE_VERSION="$version"
    export WINETRICKS_WINE_VERSION

if [ "${workaround_expected}" = "yes" ] ; then
    cat > "${outputdir}/test_version.verb" <<_EOF
w_metadata test_version settings

load_test_version()
{
    # FIXME: w_info/w_die aren't getting through b/c of QUIET? stop using quiet everywhere..
    if w_workaround_wine_bug 99999 "test" "$comparison" ; then
        w_info "Using w_workaround, expected, version: $WINETRICKS_WINE_VERSION"
    else
        w_die "Not using w_workaround, unexpected, version: $WINETRICKS_WINE_VERSION"
    fi
}
_EOF
elif [ "${workaround_expected}" = "no" ] ; then
    cat > "${outputdir}/test_version.verb" <<_EOF
w_metadata test_version settings

load_test_version()
{
    if w_workaround_wine_bug 99999 "test" "$comparison" ; then
        w_die "Using w_workaround, unexpected, version: $WINETRICKS_WINE_VERSION"
    else
        w_info "Not using w_workaround, expected, version: $WINETRICKS_WINE_VERSION"
    fi
}
_EOF
else
    w_die "unknown value ${workaround_expected}!"
fi

    # no xvfb needed
    $WINETRICKS -v --no-isolate "${outputdir}/test_version.verb" ; ret=$?
    case $ret in
        0) pass "test_version.verb (version: $version comparison: $comparison expected: $workaround_expected passed" ;;
        *) fail "test_version.verb (version: $version comparison: $comparison expected: $workaround_expected failed" ;;
    esac
}

test_w_compare_wine_version()
{
    # Previous code was very fragile, and had no regression tests.
    # Now that we can override reported Wine version with $WINETRICKS_WINE_VERSION, easier to test using some custom verbs

    #    val1,   (for >= val1)
    #    ,val2   (for <= val2)
    #    val1,val2 (for >= val1 && <= val2)

    # helper_test_w_compare_wine_version() {
    #    version="$1" # version we're pretending to be (e.g., wine-2.0)
    #    comparison="$2" # the comparison to make (e.g., ',1.8', '1.9,', or '1.8,1.9')
    #    workaround_expected="$3" # should the workaround be executed (yes/no)

    helper_test_w_compare_wine_version "wine-2.0" ",1.6" "no"
    helper_test_w_compare_wine_version "wine-2.0" ",1.4" "no"
    helper_test_w_compare_wine_version "wine-2.0" "1.4,1.6" "no"
    helper_test_w_compare_wine_version "wine-1.6" "1.4,2.0" "yes"
    helper_test_w_compare_wine_version "wine-1.0" "1.8.0," "no"
    helper_test_w_compare_wine_version "wine-1.0" ",1.8.0" "yes"
    helper_test_w_compare_wine_version "wine-1.3" ",1.2" "no"
    helper_test_w_compare_wine_version "wine-1.3" "1.2," "yes"
    helper_test_w_compare_wine_version "wine-1.3.4" ",1.2" "no"
    helper_test_w_compare_wine_version "wine-1.3.4" "1.2," "yes"
    helper_test_w_compare_wine_version "wine-1.6" ",1.2" "no"
    helper_test_w_compare_wine_version "wine-1.6" ",1.2" "no"
    helper_test_w_compare_wine_version "wine-1.6" "1.2," "yes"
    helper_test_w_compare_wine_version "wine-1.6-rc1" ",1.2" "no"
    helper_test_w_compare_wine_version "wine-1.6-rc1" "1.2," "yes"
    helper_test_w_compare_wine_version "wine-2.0.1" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0.1" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0.1" ",2.0" "no"
    helper_test_w_compare_wine_version "wine-2.0.1" ",2.0" "no"
    helper_test_w_compare_wine_version "wine-2.0.1" "2.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0" ",1.8.1" "no"
    helper_test_w_compare_wine_version "wine-2.0" "1.8.1," "yes"
    helper_test_w_compare_wine_version "wine-2.0" ",1.8" "no"
    helper_test_w_compare_wine_version "wine-2.0" "1.8," "yes"
    helper_test_w_compare_wine_version "wine-2.0" ",1.9" "no"
    helper_test_w_compare_wine_version "wine-2.0.1 (debian 2.0.1-r1)" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0.1 (debian 2.0.1-r1)" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0.1 (staging)" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0.1 (staging)" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-2.0-rc1" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0-rc1" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-2.0-rc1" "1.8.0," "yes"
    helper_test_w_compare_wine_version "wine-3.0" ",1.8.0" "no"
    helper_test_w_compare_wine_version "wine-3.0" "1.8.0," "yes"

    # FIXME: there's plenty of room for more tests..
}

case "$1" in
    check-deps) check_deps ; exit $? ;;
    custom-verbs) test_custom_verbs ;;
    dotnet) check_deps && test_dotnet ;;
    full)  check_deps && test_full ;;
    quick) check_deps && test_quick ;;
    w_compare_wine_version) check_deps && test_w_compare_wine_version ;;
    xvfb-check) check_deps && XVFB=xvfb-run && export XVFB && test_xvfb ;;
    *) echo "Usage: $0 quick|full" ; exit 1 ;;
esac

# Turn off set -x so the results are cleaner:
set +x

echo "==============================================================="
echo "Test over, $errors failures, $passes successes, $skips skipped."
if [ $errors = 0 ] && [ $passes -gt 0 ] ; then
    echo PASS
    echo "==============================================================="
    exit 0
else
    echo FAIL
    echo "Test failures:"
    oldifs="$IFS"
    IFS="|"
    for test_failure in $failed_verbs; do
        if [ -z "$test_failure" ]; then
            :
        fi
        echo "$test_failure"
    done
    IFS=$oldifs
    echo "==============================================================="
    exit 1
fi
