#!/bin/sh
# Run on the backup server with administrative credentials, never an agent key.
set -eu
: "${REPOSITORY:?REPOSITORY ausente}" "${MACHINE:?MACHINE ausente}"
SCHIPSAVE_BIN=${SCHIPSAVE_BIN:-/usr/bin/schipsave}
LOCK_WAIT=${LOCK_WAIT:-${SCHIPSAVE_LOCK_WAIT:-0}}
case "$LOCK_WAIT" in ''|*[!0-9]*) printf 'LOCK_WAIT deve ser inteiro entre 0 e 86400\n' >&2; exit 2 ;; esac
if ! [ "$LOCK_WAIT" -le 86400 ] 2>/dev/null; then printf 'LOCK_WAIT deve ser inteiro entre 0 e 86400\n' >&2; exit 2; fi
export SCHIPSAVE_LOCK_WAIT="$LOCK_WAIT"
run_schipsave() {
    if [ -n "${STORAGE_TYPE:-}${STORAGE_ID:-}${STORAGE_MOUNT:-}" ]; then
        SCHIPSAVE_BIN="$SCHIPSAVE_BIN" "${SCHIPSAVE_PROTECTED_BIN:-/usr/bin/schipsave-protected}" \
            "${STORAGE_TYPE:?}" "${STORAGE_ID:?}" "${STORAGE_MOUNT:?}" -- "$@"
    else
        "$SCHIPSAVE_BIN" "$@"
    fi
}
finish() {
    result=$?; trap - EXIT
    if [ -n "${JOB_STATE_DIR:-}" ]; then
        run_schipsave job-state-update "$JOB_STATE_DIR" "${JOB_STATE_JOB:-$MACHINE}" "$result" || result=1
    fi
    exit "$result"
}
trap finish EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
case "${COMPACT:-0}" in 0|1) ;; *) printf 'COMPACT deve ser 0 ou 1\n' >&2; exit 2 ;; esac
case "${APPLY_RETENTION:-0}" in 0|1) ;; *) printf 'APPLY_RETENTION deve ser 0 ou 1\n' >&2; exit 2 ;; esac
case "${POSTGRESQL_EXPIRE:-0}" in 0|1) ;; *) printf 'POSTGRESQL_EXPIRE deve ser 0 ou 1\n' >&2; exit 2 ;; esac
if [ "${POSTGRESQL_EXPIRE:-0}" = 1 ]; then
    : "${POSTGRESQL_WAL_MACHINE:?POSTGRESQL_EXPIRE requer POSTGRESQL_WAL_MACHINE}"
    if [ "${APPLY_RETENTION:-0}" = 1 ]; then
        : "${WAL_RECEIPT_OUTBOX:?}" "${WAL_RECEIPT_HOST:?}" "${WAL_RECEIPT_INBOX:?}"
        case "$WAL_RECEIPT_OUTBOX" in /*) ;; *) printf 'outbox deve ser absoluta\n' >&2; exit 2 ;; esac
        [ ! -L "$WAL_RECEIPT_OUTBOX" ] && [ -d "$WAL_RECEIPT_OUTBOX" ] &&
            [ "$(stat -c %u -- "$WAL_RECEIPT_OUTBOX")" = "$(id -u)" ] &&
            [ "$(stat -c %a -- "$WAL_RECEIPT_OUTBOX")" = 700 ] || { printf 'outbox requer diretório privado 0700 do usuário\n' >&2; exit 2; }
        if [ -n "${STORAGE_TYPE:-}${STORAGE_ID:-}${STORAGE_MOUNT:-}" ]; then run_schipsave --check "$WAL_RECEIPT_OUTBOX"; fi
        umask 077
        exec 8>>"$WAL_RECEIPT_OUTBOX/.maintenance.lock"
        flock -n 8 || { printf 'manutenção WAL em andamento\n' >&2; exit 1; }
    fi
fi
deliver_pending() {
    for receipt in "$WAL_RECEIPT_OUTBOX"/receipt-*.bin; do
        [ -e "$receipt" ] || continue
        "${SCHIPSAVE_WAL_DELIVER_BIN:-/usr/libexec/schipsave-wal-deliver}" "$receipt" || return 1
        # ACK is already durable on the receiver. A crash resurrecting this
        # unlink only causes duplicate delivery, accepted idempotently there.
        rm -- "$receipt" || return 1
    done
}
run_schipsave check-age "$REPOSITORY" "$MACHINE" "${MAX_AGE_HOURS:-24}"
if [ "${VERIFY:-1}" = 1 ]; then run_schipsave verify "$REPOSITORY"; fi
if [ -n "${POSTGRESQL_WAL_MACHINE:-}" ]; then
    run_schipsave check-age "$REPOSITORY" "$POSTGRESQL_WAL_MACHINE" "${WAL_MAX_AGE_HOURS:-24}"
    command=prune-postgresql
    if [ "${POSTGRESQL_EXPIRE:-0}" = 1 ]; then command=expire-postgresql; fi
    set -- "$command" "$REPOSITORY" "$MACHINE" "$POSTGRESQL_WAL_MACHINE" "${KEEP_LAST:-7}"
else
    set -- prune "$REPOSITORY" "$MACHINE" "${KEEP_LAST:-7}" "${KEEP_DAILY:-14}" "${KEEP_WEEKLY:-8}" "${KEEP_MONTHLY:-12}"
fi
if [ "${APPLY_RETENTION:-0}" = 1 ]; then set -- "$@" --apply; fi
if [ "${POSTGRESQL_EXPIRE:-0}" = 1 ] && [ "${APPLY_RETENTION:-0}" = 1 ]; then
    deliver_pending
    run_schipsave wal-recover-outbox "$REPOSITORY" "$WAL_RECEIPT_OUTBOX"
    run_schipsave "$@" --outbox "$WAL_RECEIPT_OUTBOX"
    deliver_pending
else
    run_schipsave "$@"
fi
if [ "${COMPACT:-0}" = 1 ]; then run_schipsave compact "$REPOSITORY" --apply; fi
if [ -n "${REPLICA_PARENT:-}" ]; then
    run_schipsave replicate "$REPOSITORY" "$REPLICA_PARENT/repository-$(date -u +%Y%m%dT%H%M%SZ)-$$"
fi
