#!/usr/bin/env bash
# Agent S: ZAP baseline scan (spider + passive), default options, against the app on host port 16310.
# $1 = baseline or reference; $2 = start path (default /); $3 = run label (default root).
# The app must already be running (start-app.ps1).
d="$1"
startpath="${2:-/}"
label="${3:-root}"
export MSYS_NO_PATHCONV=1
SCAN=<scratch>/sec/scan
OUT=<scratch>/sec/scan/out/zap/$d/$label/wrk
mkdir -p "$OUT"
chmod 777 "$OUT" 2>/dev/null
args=(run --rm --name "sec-scan-zap-$d-$label" -v "$SCAN/out/zap/$d/$label/wrk:/zap/wrk/:rw" -t zaproxy/zap-stable:latest
      zap-baseline.py -t "http://host.docker.internal:16310$startpath" -r report.html -J report.json -w report.md)
echo "docker ${args[*]}" > "$OUT/../command.txt"
t0=$(date +%s%N)
docker "${args[@]}" > "$OUT/../console.txt" 2>&1
ec=$?
t1=$(date +%s%N)
echo "exit=$ec ms=$(( (t1 - t0) / 1000000 ))" >> "$OUT/../console.txt"
