#!/usr/bin/env bash

set -eo pipefail
source debian/tests/.tests.rc.d/init.sh

ChangeToAutopkgtestTmpFolder

for LANGUAGE in "${DOTNET_LANGUAGES[@]}"; do
    LogInfo "Creating $LANGUAGE HelloWorld Project"
    dotnet new console --name "HelloWorld" --language $LANGUAGE
    cd "HelloWorld"

    LogInfo "Crossbuilding $LANGUAGE HelloWorld Project for ''"
    dotnet publish -c Release -r win-x64 --self-contained

    LogInfo "Checking if binary exists"
    BINARY_PATH="bin/Release/$DOTNET_VERSION_NAME/win-x64/HelloWorld.exe"
    test -e "$BINARY_PATH"

    LogInfo "Running binary with wine"
    wine "$BINARY_PATH" 2>/dev/null | tee stdout.log

    LogInfo "Comparing with expected output"
    case "$LANGUAGE" in 
        C#)
            test "$(< stdout.log)" = "$(echo -e 'Hello, World!\r\n')"
            ;;
        F#) 
            test "$(< stdout.log)" = "$(echo -e 'Hello from F#\r\n')"
            ;;
        VB) 
            test "$(< stdout.log)" = "$(echo -e 'Hello World!\r\n')"
            ;;
    esac

    LogInfo "Cleaning up"
    cd ..
    rm -r "HelloWorld"
done

LogInfo "Test Ok!"
