#!/usr/bin/env bash
# Note that this test only checks if the project builds without an error; NOT if the build artifacts run for the intended platforms.

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

case "$RUN_MINIMAL_TEST" in
    [Tt][Rr][Uu][Ee]|'1') # TRUE
        LogDebug "RUN_MINIMAL_TEST is set to TRUE."
        RUN_MINIMAL_TEST=true
        ;;
    [Ff][Aa][Ll][Ss][Ee]|0) # FALSE
        LogDebug "RUN_MINIMAL_TEST is set to FALSE."
        RUN_MINIMAL_TEST=false
        ;;
    '')
        LogDebug "RUN_MINIMAL_TEST is null, empty or unset."

        # save time and avoid potential timeouts on arm64 autopkgtest cloud runners
        if [[ "$(GetSystemArchitecture)" == "arm64" ]]; then
            RUN_MINIMAL_TEST=true
        else
            RUN_MINIMAL_TEST=false
        fi
        ;;
    *)
        LogErrorAndTerminate "RUN_MINIMAL_TEST is defined as unknown value '$RUN_MINIMAL_TEST'."
        ;;
esac

if $RUN_MINIMAL_TEST; then
    LogInfo "Running minimal test case."
fi

LogDebug "Reading supported .NET Runtime Identifiers"
mapfile -t SupportedDotnetRuntimeIdentifiers < <(dotnet fsi \
 "debian/tests/.tests.rc.d/PrintSupportedRuntimeIdentifiers.fsx" \
 "src/runtime/src/libraries/Microsoft.NETCore.Platforms/src/runtime.compatibility.json")
for RID in "${SupportedDotnetRuntimeIdentifiers[@]}"; do
    echo " - '$RID'"
done
echo

ChangeToAutopkgtestTmpFolder

declare -A LanguageProjectFileExtension
LanguageProjectFileExtension["C#"]=".csproj"
LanguageProjectFileExtension["F#"]=".fsproj"
LanguageProjectFileExtension["VB"]=".vbproj"

if $RUN_MINIMAL_TEST; then
    LANGUAGES=(C#)
else
    LANGUAGES=("${DOTNET_LANGUAGES[@]}")
fi

LogDebug "$(echo "Testing for Languages: ${LANGUAGES[@]}")";

for LANGUAGE in "${LANGUAGES[@]}"; do
    LogDebug "Creating $LANGUAGE console template"
    dotnet new console --name "HelloWorld-$LANGUAGE" --language "$LANGUAGE"
done

for RID in "${SupportedDotnetRuntimeIdentifiers[@]}"; do
    for LANGUAGE in "${LANGUAGES[@]}"; do
        ProjectName="HelloWorld-${LANGUAGE}"
        ProjectFile="${ProjectName}/${ProjectName}${LanguageProjectFileExtension["${LANGUAGE}"]}"
        
        ArtifactRoot="${ProjectName}/bin/Release/${DOTNET_VERSION_NAME}/${RID}"
        Artifact1="${ArtifactRoot}/${ProjectName}.dll"
        Artifact2="${ArtifactRoot}/publish"

        LogInfo "Building $LANGUAGE Project for ${RID} with --self-contained"
        dotnet publish -c Release -r "${RID}" --self-contained "$ProjectFile"
        
        LogInfo "Check if build artifacts for ${RID} exists.";
        if [[ ! -e "$Artifact1" ]]; then
            LogErrorAndTerminate "Build artifact was not found: '$Artifact1'"
        fi

        if [[ ! -d "$Artifact2" ]]; then
            LogErrorAndTerminate "Directory that would contain build artifacts was not found: '$Artifact2'"
        fi

        LogDebug "Cleaning build artifacts"
        rm -r "$ArtifactRoot"

        if ! $RUN_MINIMAL_TEST; then
            LogInfo "Building $LANGUAGE Project for ${RID} with --no-self-contained"
            dotnet publish -c Release -r "${RID}" --no-self-contained "$ProjectFile"

            LogInfo "Check if build artifacts for ${RID} exists.";
            if [[ ! -e "$Artifact1" ]]; then
                LogErrorAndTerminate "Build artifact was not found: '$Artifact1'"
            fi

            if [[ ! -d "$Artifact2" ]]; then
                LogErrorAndTerminate "Directory that would contain build artifacts was not found: '$Artifact2'"
            fi

            LogDebug "Cleaning build artifacts"
            rm -r "$ArtifactRoot"
        fi
    done
done

LogInfo "Test Ok!"
