#!/bin/bash
# Setup WAL continuous archiving for PostgreSQL.
# Run once on the production server, then restart PostgreSQL.
#
# This enables point-in-time recovery (PITR): restore to any moment,
# not just the last daily pg_dump. Caps data loss at ~5 minutes.
#
# Usage:
#   scp deploy/setup-wal-archiving.sh root@100.120.174.96:/opt/makenotwork/
#   ssh root@100.120.174.96 "bash /opt/makenotwork/setup-wal-archiving.sh"

set -euo pipefail

WAL_ARCHIVE_DIR="/opt/makenotwork/wal-archive"
PG_CONF_DIR="/etc/postgresql/16/main"
PG_CONF="${PG_CONF_DIR}/postgresql.conf"
CONF_OVERRIDE="${PG_CONF_DIR}/conf.d/wal_archiving.conf"

echo "[$(date -Iseconds)] Setting up WAL continuous archiving..."

# Verify PostgreSQL config exists
if [ ! -f "$PG_CONF" ]; then
    # Try version 15 (Debian 11)
    PG_CONF_DIR="/etc/postgresql/15/main"
    PG_CONF="${PG_CONF_DIR}/postgresql.conf"
    CONF_OVERRIDE="${PG_CONF_DIR}/conf.d/wal_archiving.conf"
    if [ ! -f "$PG_CONF" ]; then
        echo "ERROR: PostgreSQL config not found at /etc/postgresql/{15,16}/main/postgresql.conf"
        exit 1
    fi
fi

# Ensure conf.d directory exists and is included
mkdir -p "${PG_CONF_DIR}/conf.d"
if ! grep -q "include_dir = 'conf.d'" "$PG_CONF" 2>/dev/null; then
    echo "include_dir = 'conf.d'" >> "$PG_CONF"
    echo "[$(date -Iseconds)] Added conf.d include directive to postgresql.conf"
fi

# Create WAL archive directory
mkdir -p "$WAL_ARCHIVE_DIR"
chown postgres:postgres "$WAL_ARCHIVE_DIR"
chmod 700 "$WAL_ARCHIVE_DIR"
echo "[$(date -Iseconds)] Created WAL archive directory: $WAL_ARCHIVE_DIR"

# Write WAL archiving configuration
cat > "$CONF_OVERRIDE" <<'PGCONF'
# WAL continuous archiving for PITR
# Caps data loss at ~5 minutes (archive_timeout).
# Applied: setup-wal-archiving.sh

wal_level = replica
archive_mode = on
archive_command = 'test ! -f /opt/makenotwork/wal-archive/%f && cp %p /opt/makenotwork/wal-archive/%f'
archive_timeout = 300
PGCONF

chown postgres:postgres "$CONF_OVERRIDE"
echo "[$(date -Iseconds)] Wrote WAL config to $CONF_OVERRIDE"

echo ""
echo "WAL archiving configured. To activate:"
echo ""
echo "  sudo systemctl restart postgresql"
echo ""
echo "Verify after restart:"
echo "  sudo -u postgres psql -c \"SHOW wal_level;\"        -- expect: replica"
echo "  sudo -u postgres psql -c \"SHOW archive_mode;\"     -- expect: on"
echo "  sudo -u postgres psql -c \"SHOW archive_timeout;\"  -- expect: 300"
echo ""
echo "Then add cron jobs for WAL maintenance and offsite sync:"
echo "  # Prune WAL segments older than 7 days (hourly):"
echo "  0 * * * * find /opt/makenotwork/wal-archive -name '0*' -mtime +7 -delete"
echo "  # Offsite sync (every 10 minutes):"
echo "  */10 * * * * /opt/makenotwork/sync-wal-offsite.sh >> /opt/makenotwork/wal-archive/sync.log 2>&1"
