#!/bin/sh
# Persistent selection of immutable archived WAL, consumed by schipsave-job.
set -eu
[ "$#" = 2 ] || exit 2
: "${PG_WAL_ARCHIVE:?configure PG_WAL_ARCHIVE por cluster}"
case "$1" in /*) ;; *) echo 'SOURCE deve ser staging absoluto' >&2; exit 2 ;; esac
if [ -n "${PG_STORAGE_TYPE:-}${PG_STORAGE_ID:-}${PG_STORAGE_MOUNT:-}" ]; then
    for directory in "$PG_WAL_ARCHIVE" "$1"; do
        "${SCHIPSAVE_PROTECTED_BIN:-/usr/bin/schipsave-protected}" \
            "${PG_STORAGE_TYPE:?}" "${PG_STORAGE_ID:?}" "${PG_STORAGE_MOUNT:?}" -- --check "$directory"
    done
fi
exec "${SCHIPSAVE_BIN:-/usr/bin/schipsave}" wal-stage "$PG_WAL_ARCHIVE" "$1"
