#!/bin/bash
# sync-to-mirror.sh — regenerate deps.conf, generate amos64 and global
# CHECKSUMS.md5, sign them, then rsync to the mirror host.

set -euo pipefail
PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
export PATH

WITH_DEPS="${1:-false}"
WITH_ISO="${2:-false}"

BUILD_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
REPO="$(cd -- "$BUILD_DIR/.." && pwd -P)"
# REPO="${REPO:-/home/annestar/amos-current/repo}"
TREE="$REPO/amos64"
GPG_KEY="${GPG_KEY:-radanne@anne.media}"
MIRROR_HOST="${MIRROR_HOST:-webroot@116.203.230.116}"
MIRROR_PATH="${MIRROR_PATH:-/home/webroot/public_html/mirror/amos/}"
SSH_PORT="${SSH_PORT:-22394}"

[ -d "$REPO/amos64" ] || { echo "No $REPO/amos64" >&2; exit 1; }

# --- 1. Regenerate dependency graph ---
if [ "$WITH_DEPS" = true ]; then
    "$REPO/audit/gen-deps.sh" "$TREE" "$TREE/deps.conf"
fi
# --- 2. Generate amos64 CHECKSUMS.md5 (for slackpkg) ---
CS="$REPO/amos64/CHECKSUMS.md5"
ASC="$REPO/amos64/CHECKSUMS.md5.asc"
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT

{
    cat <<'HDR'
These are the MD5 message digests for the files in this directory.
If you want to test your files, use 'md5sum' and compare the values to
the ones listed here.

To test all these files, use this command:

tail +13 CHECKSUMS.md5 | md5sum -c --quiet - | less

'md5sum' can be found in the GNU coreutils package on ftp.gnu.org in
/pub/gnu, or at any GNU mirror site.

MD5 message digest                Filename
HDR

    ( cd "$REPO" && \
      find ./amos64 -type f \
          \( -name '*.txz' -o -name '*.tgz' -o -name '*.txt' \) \
          ! -name 'CHECKSUMS.md5' \
          ! -name 'CHECKSUMS.md5.asc' \
          ! -name 'deps.conf' \
          -print0 \
        | sort -z \
        | xargs -0 md5sum )
} > "$TMP"

mv "$TMP" "$CS"
chmod 644 "$CS"
echo "$CS updated ($(($(wc -l < "$CS") - 12)) entries)"

# --- 3. Generate Global CHECKSUMS.md5 (for mirror verification) ---
GLOBAL_CS="$REPO/CHECKSUMS.md5"
GLOBAL_ASC="$REPO/CHECKSUMS.md5.asc"
TMP_GLOBAL="$(mktemp)"

{
    cat <<'HDR'
These are the MD5 message digests for all files in this mirror.

MD5 message digest                Filename
HDR

    ( cd "$REPO" && \
      find . -type f \
          ! -name 'CHECKSUMS.md5' \
          ! -name 'CHECKSUMS.md5.asc' \
          ! -path './slackware64/*' \
          ! -name 'nohup.out' \
          ! -name '*.orig' \
          ! -name '*.upstream' \
          -print0 \
        | sort -z \
        | xargs -0 md5sum )
} > "$TMP_GLOBAL"

mv "$TMP_GLOBAL" "$GLOBAL_CS"
chmod 644 "$GLOBAL_CS"
echo "$GLOBAL_CS updated ($(($(wc -l < "$GLOBAL_CS") - 4)) entries)"

# --- 4. Sign both checksums ---
if gpg --list-secret-keys "$GPG_KEY" >/dev/null 2>&1; then
    gpg --yes --detach-sign --armor --local-user "$GPG_KEY" --output "$ASC" "$CS"
    gpg --yes --detach-sign --armor --local-user "$GPG_KEY" --output "$GLOBAL_ASC" "$GLOBAL_CS"
    echo "Checksums signed."
else
    echo "WARNING: no secret key for '$GPG_KEY'; checksums unsigned" >&2
fi

if [ "$WITH_ISO" = true ]; then
# --- 5. Sync to mirror ---
echo "=== rsync -> $MIRROR_HOST:$MIRROR_PATH ==="
rsync -aAXvz --delete --partial --info=progress2 \
    --exclude='/slackware64/' \
    --exclude='nohup.out' --exclude='*.orig' --exclude='*.upstream' --delete-excluded \
    -e "ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
    "$REPO"/ "$MIRROR_HOST:$MIRROR_PATH"

ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $MIRROR_HOST "cp $MIRROR_PATH/amos-current-edge-x86_64.iso /home/webroot/public_html/mirror/"
else
  rsync -aAXvz --delete --partial --info=progress2 \
    --exclude='/slackware64/' --exclude='*.iso' --exclude='initrd.img' \
    --exclude='nohup.out' --exclude='*.orig' --exclude='*.upstream' --delete-excluded \
    -e "ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
    "$REPO"/ "$MIRROR_HOST:$MIRROR_PATH"
fi
echo "=== done ==="