#!/bin/bash
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation. See the LICENSE file for details.

# sb-publish: push the local package repository to the public mirror.
#
# Usage: sb-publish [PKGREPO]      (default /repo)
#
# Called by rsync_push_hook in slackrepo_SBo-danix.conf after a build, unless
# the build ran with SBB_PUBLISH=n; run it by hand (or from sb-builder's
# Publish action) to push such builds later. Settings come from the root-only
# SB_LOCAL file (default /etc/slackrepo/SBo-danix.local):
#   SB_RSYNC_TARGET          rsync destination (required)
#   SB_RSYNC_PASSWORD_FILE   --password-file, if the file exists
#   SB_RSYNC_EXCLUDES        --exclude-from, if the file exists

set -euo pipefail

SB_LOCAL=${SB_LOCAL:-/etc/slackrepo/SBo-danix.local}
[ -r "$SB_LOCAL" ] && . "$SB_LOCAL"
: "${SB_RSYNC_TARGET:?SB_RSYNC_TARGET is not set in $SB_LOCAL}"

pkgrepo=${1:-/repo}
args=( -havz --delete-after --stats )
[ -n "${SB_RSYNC_EXCLUDES:-}" ] && [ -f "$SB_RSYNC_EXCLUDES" ] && \
  args+=( --exclude-from="$SB_RSYNC_EXCLUDES" )
[ -n "${SB_RSYNC_PASSWORD_FILE:-}" ] && [ -f "$SB_RSYNC_PASSWORD_FILE" ] && \
  args+=( --password-file="$SB_RSYNC_PASSWORD_FILE" )

echo "sb-publish: pushing $pkgrepo to $SB_RSYNC_TARGET ..."
rsync "${args[@]}" "$pkgrepo"/ "$SB_RSYNC_TARGET"
echo "sb-publish: rsync push complete."
