#!/bin/bash
# publish-registry <dir>: upload every built package in <dir> (debug packages
# skipped) to the [uhhm] Gitea Arch registry. HTTP 409 means that
# pkgver-pkgrel is already published, which is fine on re-runs.
set -euo pipefail
: "${REGISTRY_TOKEN:?}" "${REGISTRY_URL:?}"
shopt -s nullglob
pkgs=("$1"/*.pkg.tar.{zst,xz,gz})
[ ${#pkgs[@]} -gt 0 ] || { echo "no packages in $1" >&2; exit 1; }
for pkg in "${pkgs[@]}"; do
    case "$pkg" in *-debug-*) continue ;; esac
    code=$(curl -sS -o /tmp/registry-resp -w '%{http_code}' \
        --user "bl:$REGISTRY_TOKEN" --upload-file "$pkg" "$REGISTRY_URL")
    case "$code" in
        2??) echo "uploaded $(basename "$pkg")" ;;
        409) echo "already published: $(basename "$pkg")" ;;
        *)   echo "::error::$(basename "$pkg"): HTTP $code"; cat /tmp/registry-resp; exit 1 ;;
    esac
done
