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

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

branch="${EVERY_CHANNEL_MIRROR_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}"
push_tags="${EVERY_CHANNEL_MIRROR_PUSH_TAGS:-true}"
remotes="${EVERY_CHANNEL_MIRROR_REMOTES:-mirror-codeberg mirror-github}"

for remote in ${remotes}; do
  if ! git remote get-url "${remote}" >/dev/null 2>&1; then
    echo "warn: remote not configured, skipping: ${remote}" >&2
    continue
  fi
  echo "sync: ${remote} (${branch})"
  git push "${remote}" "${branch}:${branch}"
  if [[ "${push_tags}" == "true" || "${push_tags}" == "1" ]]; then
    git push "${remote}" --tags
  fi
done

echo "ok: mirror push complete"
