#!/usr/bin/env bash
# HISTORICAL — k6 runner kept for reference. The canonical load test is wrk
# (load-test/run-wrk.sh). See load-test/README.md.
#
# Reads load-test/.env and runs k6 with all variables injected.
# Usage: ./load-test/run.sh

set -euo pipefail

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$DIR/.env"
SCRIPT="$DIR/loadtest.js"

if [ ! -f "$ENV_FILE" ]; then
  echo "Error: $ENV_FILE not found — fill in your values first." >&2
  exit 1
fi

args=("run")

while IFS= read -r line; do
  line="${line#"${line%%[![:space:]]*}"}"  # trim leading whitespace
  if [ -n "$line" ] && [ "${line:0:1}" != "#" ]; then
    args+=("-e" "$line")
  fi
done < "$ENV_FILE"

args+=("$SCRIPT")

exec k6 "${args[@]}"
