#!/bin/bash
# omarchy:summary=Check reachability of each configured development registry
# omarchy:examples=omarchycn dev-mirror doctor

set -euo pipefail

source "$OMARCHY_PATH/cn/lib/dev-mirror.sh"

failures=0

for target in "${CN_DM_TARGETS[@]}"; do
  current=$("cn_dm_get_$target")
  if [[ -z $current ]]; then
    echo "INFO $target: using tool default"
    continue
  fi

  # Reduce to a probe-able https URL
  probe="${current#sparse+}"
  probe="${probe%%,*}"
  if curl -sSf -o /dev/null -m 8 --connect-timeout 5 "$probe" 2>/dev/null; then
    echo "PASS $target: $current"
  elif curl -sS -o /dev/null -m 8 --connect-timeout 5 -w '%{http_code}' "$probe" 2>/dev/null | grep -qE '^[34]'; then
    # Registry roots often answer 3xx/404 to bare GET while the service works
    echo "PASS $target: $current"
  else
    echo "FAIL $target: $current unreachable"
    failures=$((failures + 1))
  fi
done

exit $((failures > 0 ? 1 : 0))
