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

: "${BUILD_NUMBER:?BUILD_NUMBER is required}"

repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${repo_root}"

source_dir="${BURROW_RELEASE_OUT:-${repo_root}/dist/builds/${BUILD_NUMBER}}"

if [[ ! -d "$source_dir" ]]; then
  echo "release artifact directory not found: $source_dir" >&2
  exit 1
fi

bucket="${BURROW_RELEASE_GCS_BUCKET:-burrow-net-releases}"
if ! command -v gcloud >/dev/null 2>&1; then
  echo "gcloud is required for GCS release upload" >&2
  exit 127
fi
gcloud auth list --filter=status:ACTIVE --format='value(account)' | grep -q . || {
  echo "gcloud has no active account; run Scripts/ci/google-wif-auth.sh first" >&2
  exit 1
}
gcloud storage rsync --recursive "$source_dir" "gs://${bucket}/builds/${BUILD_NUMBER}"
