#!/usr/bin/env bash
# Agent S: .NET analyzers and NuGet audit on one copy ($1 = baseline or reference).
d="$1"
SCANW='<scratch>\sec\scan'
ROOT=<scratch>/sec/scan
OUT="$ROOT/out/dotnet/$d"
OUTW="$SCANW\\out\\dotnet\\$d"
mkdir -p "$OUT"
cd "$ROOT/$d/PatientPortal" || exit 1
API=src/PatientPortal.Api/PatientPortal.Api.csproj
TST=tests/PatientPortal.Tests/PatientPortal.Tests.csproj
run() {
  local name="$1"; shift
  echo "cd $d/PatientPortal && $*" >> "$OUT/commands.txt"
  local t0=$(date +%s%N)
  "$@" > "$OUT/$name.txt" 2>&1
  local ec=$?
  local t1=$(date +%s%N)
  echo "exit=$ec ms=$(( (t1 - t0) / 1000000 ))" >> "$OUT/$name.txt"
}
: > "$OUT/commands.txt"
# 1. NuGet audit as the project stands, then the plain build (nothing switched on).
run 01-restore dotnet restore
run 02-build-plain dotnet build --no-restore --no-incremental
# 2. Vulnerable packages, then restore with the audit forced on.
run 03-list-vulnerable dotnet list package --vulnerable --include-transitive
run 04-restore-audit-on dotnet restore --force -p:NuGetAudit=true
# Restore again as the project stands so the analyzer build uses the same assets as the plain one.
run 05-restore-again dotnet restore --force
# 3. Analyzers at their widest setting, security category as warnings, one SARIF log per project.
run 06-build-analyzers-api dotnet build "$API" --no-restore --no-incremental -p:EnableNETAnalyzers=true -p:AnalysisLevel=latest-all -p:AnalysisModeSecurity=All "-p:ErrorLog=$OUTW\\api.sarif%2Cversion=2.1"
run 07-build-analyzers-tests dotnet build "$TST" --no-restore --no-incremental --no-dependencies -p:EnableNETAnalyzers=true -p:AnalysisLevel=latest-all -p:AnalysisModeSecurity=All "-p:ErrorLog=$OUTW\\tests.sarif%2Cversion=2.1"
