#!/usr/bin/env bash

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

function IsDotnet6And7Installed
{
   test "$(dpkg-query -W -f='${Status}\n' dotnet6 dotnet7 2>/dev/null | grep -qc "ok installed")" = "2"
}

function CheckDotnetVersionOutput
{
    LogInfo "Check if 'dotnet --version' shows correct version number."
    DOTNET_VERSION_OUTPUT="$(dotnet --version)"

    if [[ ! "$DOTNET_VERSION_OUTPUT" = "$DOTNET_SDK_VERSION" ]]; then
        LogErrorAndTerminate "'dotnet --version' showed version number '$DOTNET_VERSION_OUTPUT' instead of expected version number '$DOTNET_SDK_VERSION'."
    fi
}

function CheckDotnetInfoOutput
{
    LogInfo "Checking 'dotnet --info' output."

    DOTNET_INFO_OUTPUT="$(dotnet --info)"

    expected="4"
    count=$(echo "$DOTNET_INFO_OUTPUT" | grep "/usr/lib/dotnet" -c || true)
    if [[ "${count}" == "$expected" ]]; then
        LogDebug "'dotnet --info' contained the path '/usr/lib/dotnet' $count times. Ok!"
    else
        LogErrorAndTerminate "'dotnet --info' contained the path '/usr/lib/dotnet' $count times. Expected was $expected times."
    fi

    count=$(echo "$DOTNET_INFO_OUTPUT" | grep -E "Version:\s+$DOTNET_SDK_VERSION" -c || true)
    if [[ "${count}" == "1" ]]; then
        LogDebug "'dotnet --info' contains the SDK version number '$DOTNET_SDK_VERSION'. Ok!"
    else
        LogErrorAndTerminate "'dotnet --info' did not contain the SDK version number '$DOTNET_SDK_VERSION'."
    fi

    count=$(echo "$DOTNET_INFO_OUTPUT" | grep -Pz "Host:\s+Version:\s+$DOTNET_RUNTIME_VERSION" -c || true)
    if [[ "${count}" == "1" ]]; then
        LogDebug "'dotnet --info' contains the Runtime version (Host Version) number '$DOTNET_RUNTIME_VERSION'. Ok!"
    else
        LogErrorAndTerminate "'dotnet --info' did not contain the Runtime version (Host Version) number '$DOTNET_RUNTIME_VERSION'."
    fi

    count=$(echo "$DOTNET_INFO_OUTPUT" | grep -E "RID:\s+$DOTNET_RUNTIME_IDENTIFIER" -c || true)

    if [[ "${count}" == "1" ]]; then
        LogDebug "'dotnet --info' contains the Runtime Identifier (RID) '$DOTNET_RUNTIME_IDENTIFIER'. Ok!"
    else
        actual=$(echo "$DOTNET_INFO_OUTPUT" | grep "RID" || true)
        LogErrorAndTerminate "'dotnet --info' did not contain the Runtime Identifier (RID) '$DOTNET_RUNTIME_IDENTIFIER'. Found $(echo "$actual" | tr -s ' ')"
    fi

    expected='global.json file:
  Not found' # this is no formatting error!
    actual="$(echo "$DOTNET_INFO_OUTPUT" | grep "global.json file:" -A1)"
    if [[ "$actual" == "$expected" ]]; then
        LogDebug "'dotnet --info' contains status information that the global.json file was not found. Ok!"
    else
        LogErrorAndTerminate "'dotnet --info' did not contain status information that the global.json file was not found."
    fi
}

if IsDotnet6And7Installed; then
   # Notice that this test will fail for dotnet6 if dotnet7 is installed
   # in parallel, because "dotnet --version" will show the dotnet7 version.
   #
   # It may also fail for dotnet7, because the expected count's (see
   # CheckDotnetInfoOutput below) are incorrect when both versions are installed.
   LogWarning "Skipping Test, because dotnet6 and dotnet7 are both installed. This test is expected to fail when both are installed."
   exit 77 # Skip Test
fi

CheckDotnetVersionOutput
CheckDotnetInfoOutput

LogInfo "Test Ok!"
