#!/usr/bin/env bash
set -euo pipefail

usage() {
  printf '%s\n' \
    'usage: finalize-task-route.sh <build|review> <local|cloud> <scope> <state> <blast> <evidence> <verification>' >&2
  exit 2
}

if [[ $# -ne 7 ]]; then
  usage
fi

target=$1
lane=$2
scores=("${@:3:5}")

case "$target" in
  build)
    prefix=PLAN
    ;;
  review)
    prefix=CODE_REVIEW
    ;;
  *)
    usage
    ;;
esac

case "$lane" in
  local|cloud)
    ;;
  *)
    usage
    ;;
esac

sum=0
for score in "${scores[@]}"; do
  case "$score" in
    0|1|2)
      sum=$((sum + score))
      ;;
    *)
      usage
      ;;
  esac
done

if [[ $sum -lt 1 ]]; then
  sum=1
fi

printf 'target=%s\n' "$target"
printf 'lane=%s\n' "$lane"
printf 'grade=G%02d\n' "$sum"
printf 'filename=%s-%s-G%02d.md\n' "$prefix" "$lane" "$sum"
