#!/bin/sh

set -eu

if [ $# -ne 2 ]; then
  echo "usage: $0 TEAM PROJECT"
  exit 1
fi

TEAM="$1"
PROJECT="$2"

checkdep() {
  if ! which "$1" > /dev/null; then
    echo "E: $1 not found; please install"
    exit 1
  fi
}

# dependencies
checkdep curl
checkdep jq
checkdep openssl

rand() {
  local max="$1"
  echo $((0x$(openssl rand -hex 1) % ${max} + 1))
}

rand_pass_fail() {
  if [ "$(rand 1)" -eq 1 ]; then
    echo "pass"
  else
    echo "fail"
  fi
}

hit_api() {
  local endpoint="$1"
  shift
  curl --fail \
    "http://localhost:8000${endpoint}" "$@"
}

build=$(date '+%Y.%m.%d')

metrics=$(mktemp)
cat > "${metrics}" <<METRICS
{
  "ungroupedmetric1": [$(rand 3),$(rand 3),$(rand 3)],
  "ungroupedmetric2": [$(rand 3),$(rand 3),$(rand 3)],
  "bgroup1/metric1": [$(rand 3),$(rand 3),$(rand 3)],
  "bgroup1/metric2": [$(rand 4),$(rand 4),$(rand 4)],
  "bgroup2/metric3": [$(rand 4),$(rand 4),$(rand 4)]
}
METRICS

tests=$(mktemp)
cat > "${tests}" <<TESTS
{
  "tgroup1/test1": "$(rand_pass_fail)",
  "tgroup1/test2": "$(rand_pass_fail)",
  "tgroup1/test3": "$(rand_pass_fail)",
  "tgroup1/test4": "$(rand_pass_fail)",
  "tgroup1/test5": "$(rand_pass_fail)"
}
TESTS

metadata=$(mktemp)
cat > "${metadata}" <<METADATA
{
  "datetime": "$(date --iso-8601=seconds)",
  "job_id": "1",
  "job_url": "http://example.com/$build/1",
  "resubmit_url": "http://example.com/$build/1/resubmit"
}
METADATA

trap "rm -rf $metrics $tests $metadata" INT TERM EXIT


hit_api "/api/submit/$TEAM/$PROJECT/$build/testenv" \
  --fail \
  --header "Auth-Token: $AUTH_TOKEN" \
  --silent --output /dev/null \
  --form metadata=@${metadata} \
  --form metrics=@${metrics} \
  --form tests=@${tests} \
