SRC=10.10.142.70:30003
DST=10.10.145.4:30003
SRC_CREDS="admin:password"
DST_CREDS="admin:password"

# Optional first arg: only sync repos whose name starts with this prefix. If not provided, sync all.
PREFIX="${1:-}"

# 1) List repositories (Catalog API; may require auth and may be blocked by config)
repos=$(curl -sk -u "$SRC_CREDS" "https://$SRC/v2/_catalog?n=10000" | jq -r '.repositories[]')

# 2) Mirror each repo (copies all tags in that repo)
for r in $repos; do
  echo "PREFIX: $PREFIX"
  echo "repo: $r"

  sync="false"
  if [[ -z "$PREFIX" ]]; then
    sync="true"
  elif [[ -n "$PREFIX" && "$r" == "$PREFIX"* ]]; then
    sync="true"
  fi

  echo "sync? $sync"
  if [[ $sync != "true" ]]; then
    continue
  fi

  echo "Syncing repo: $r"
  if [[ "$r" == */* ]]; then
    dest="$DST/${r%/*}/"
  else
    dest="$DST/"
  fi
  skopeo sync --preserve-digests --src docker --dest docker \
    --src-creds "$SRC_CREDS" --dest-creds "$DST_CREDS" \
    --src-tls-verify=false --dest-tls-verify=false \
    "$SRC/$r" "$dest"
done

