#!/usr/bin/env bash
# Checks a student workstation before session 1 and prints one line per check.
#
#   TEAM=t05 TEAM_HOST=<hostname> bash check-setup.sh
#
# Ships at the root of the student repository; the course site serves a copy
# at https://course.epimarket.fr/check-setup.sh.
#
# Optional: DD_API_KEY (your org's `student` key) adds the Datadog check.
# Exit status is 1 when any required check fails. WARN and SKIP never fail.
set -u

failed=0

pass() { printf 'PASS  %s\n' "$*"; }
warn() { printf 'WARN  %s\n' "$*"; }
skip() { printf 'SKIP  %s\n' "$*"; }
fail() { printf 'FAIL  %s\n' "$*"; failed=1; }

have() { command -v "$1" >/dev/null 2>&1; }

# Prints the first "major.minor" found in its input, e.g. 1.25 from go1.25.3.
minor_version() { grep -Eo '[0-9]+\.[0-9]+' | head -n 1; }

# at_least 1.25 1.24 is false, at_least 1.25 1.30 is true.
at_least() {
  local want=$1 got=$2
  [ "${got%%.*}" -gt "${want%%.*}" ] && return 0
  [ "${got%%.*}" -eq "${want%%.*}" ] && [ "${got#*.}" -ge "${want#*.}" ]
}

check_tool() {
  local name=$1 want=$2 required=$3 version
  shift 3
  if ! have "$name"; then
    if [ "$required" = yes ]; then fail "$name: not installed"; else warn "$name: not installed (optional)"; fi
    return
  fi
  version=$("$@" 2>/dev/null | minor_version)
  if [ -z "$version" ]; then
    warn "$name: installed, version unreadable"
  elif [ -n "$want" ] && ! at_least "$want" "$version"; then
    if [ "$required" = yes ]; then fail "$name: $version, need $want or newer"; else warn "$name: $version, $want or newer recommended"; fi
  else
    pass "$name: $version"
  fi
}

echo "== Tools"
check_tool git "" yes git --version
check_tool curl "" yes curl --version
check_tool kubectl 1.34 yes kubectl version --client
check_tool helm 3.19 yes helm version --short
check_tool go 1.25 no go version
check_tool docker "" no docker --version
if have docker; then
  if docker compose version >/dev/null 2>&1; then pass "docker compose: plugin present"; else warn "docker compose: plugin missing (optional)"; fi
fi

echo "== Team"
if [ -z "${TEAM:-}" ] || [ -z "${TEAM_HOST:-}" ]; then
  fail "TEAM and TEAM_HOST must be set, e.g. TEAM=t05 TEAM_HOST=example.teams.epimarket.fr"
else
  status=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "https://$TEAM_HOST/")
  if [ "$status" = 200 ]; then pass "storefront: https://$TEAM_HOST/ answers 200"; else fail "storefront: https://$TEAM_HOST/ answers ${status:-nothing}"; fi

  if ! have kubectl; then
    skip "kubectl access: kubectl not installed"
  elif ! kubectl config current-context >/dev/null 2>&1; then
    skip "kubectl access: no kubeconfig set; use the one attached to your welcome email"
  elif kubectl -n "$TEAM" get deployment web >/dev/null 2>&1; then
    pass "kubectl access: sees your stack in namespace $TEAM"
  else
    fail "kubectl access: no web deployment in namespace $TEAM on context $(kubectl config current-context); wrong kubeconfig?"
  fi
fi

echo "== Accounts"
if ssh -T -o BatchMode=yes -o ConnectTimeout=5 git@gitlab.cri.epita.fr 2>&1 | grep -q 'Welcome to GitLab'; then
  pass "GitLab: SSH key accepted by gitlab.cri.epita.fr"
else
  warn "GitLab: SSH key not accepted (fine if you clone over HTTPS)"
fi

if [ -z "${DD_API_KEY:-}" ]; then
  skip "Datadog: set DD_API_KEY to your org's student key to check it"
elif curl -s --max-time 10 -H "DD-API-KEY: $DD_API_KEY" https://api.datadoghq.eu/api/v1/validate | grep -q '"valid":true'; then
  pass "Datadog: API key valid on datadoghq.eu"
else
  fail "Datadog: API key rejected by datadoghq.eu (check the site and the key)"
fi

echo
if [ "$failed" -eq 0 ]; then echo "Ready for session 1."; else echo "Fix every FAIL line before session 1."; fi
exit "$failed"
