#!/usr/bin/env bash

set -e
source debian/tests/.tests.rc.d/init.sh

ChangeToAutopkgtestTmpFolder

LogInfo "Create C# Console Project"
dotnet new console --name Testing
cd Testing

LogInfo "Overwrite program.cs with demo code"
cat <<EOF >Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis;

namespace TestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> myStack = new Stack<int>();
            var th = new Thread(()=>WaitAndPrint(myStack));
            th.Start();
            Console.WriteLine("Me first!");
            myStack.Push(1);
            Console.WriteLine("Finished tasks: {0}", myStack.Count);
            Thread.Sleep(1000);
            Console.WriteLine("Finished tasks: {0}", myStack.Count);
        }

        private static void WaitAndPrint(Stack<int> myStack)
        {
            Thread.Sleep(1000);
            Console.WriteLine("Me second!");
            myStack.Push(2);
        }
    }
}
EOF

LogInfo "Try to run without Nuget Package"
(dotnet run 2>&1 && LogErrorAndTerminate "'dotnet run' succeeded although nuget package is missing") || true

LogInfo "Add Nuget Package"
dotnet add Testing.csproj package Google.Apis

LogInfo "Run Application"
dotnet run

LogInfo "Remove Nuget Package"
dotnet remove Testing.csproj package Google.Apis

LogInfo "Try to run without Nuget Package again"
(dotnet run 2>&1 && LogErrorAndTerminate "'dotnet run' succeeded although nuget package is missing") || true

LogInfo "Test Ok!"
