@echo off
setlocal enabledelayedexpansion

REM ============================================================
REM  PostgreSQL backup (custom compressed format, -Fc)
REM  Place this file in the PostgreSQL install dir (next to bin) and run.
REM  Backups are written to the dumps\ subfolder next to this bat.
REM ============================================================

REM ---------- Connection config (from settings.yaml production) ----------
set "PGHOST=127.0.0.1"
set "PGPORT=8432"
set "PGDATABASE=dcp"
set "PGUSER=postgres"
set "PGPASSWORD=rootroot"

REM ---------- Paths (no need to change) ----------
set "PGBIN=%~dp0bin"
set "OUTDIR=%~dp0dumps"
if not exist "%OUTDIR%" mkdir "%OUTDIR%"

REM ---------- Timestamped filename (PowerShell avoids locale issues) ----------
for /f "delims=" %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "TS=%%i"
set "OUTFILE=%OUTDIR%\%PGDATABASE%_%TS%.dump"

echo.
echo [DUMP] %PGDATABASE% @ %PGHOST%:%PGPORT%
echo [OUT]  %OUTFILE%
echo.

REM -Fc custom compressed; -Z 6 level(0-9); --no-owner/--no-privileges for cross-env restore
"%PGBIN%\pg_dump.exe" --no-owner --no-privileges -Fc -Z 6 -f "%OUTFILE%" "%PGDATABASE%"

if errorlevel 1 (
    echo.
    echo [FAIL] dump failed, check the output above.
    pause
    exit /b 1
)

echo.
echo [OK] dump finished.
pause
