@echo off
REM Build the app image, pull postgres, save BOTH to a single tarball.
REM Usage:  docker-build-and-save.bat

setlocal
set OUT_DIR=DockerImages
if not exist %OUT_DIR% mkdir %OUT_DIR%

echo ==^> Building app image (atapoly-cbt-app:latest)...
docker compose build app
if errorlevel 1 goto :err

echo ==^> Pulling postgres:15-alpine...
docker pull postgres:15-alpine
if errorlevel 1 goto :err

echo ==^> Saving both images to %OUT_DIR%\atapoly-cbt-images.tar...
docker save -o %OUT_DIR%\atapoly-cbt-images.tar atapoly-cbt-app:latest postgres:15-alpine
if errorlevel 1 goto :err

copy /Y docker-compose.yml %OUT_DIR%\docker-compose.yml >nul

> %OUT_DIR%\README.txt echo ATAPOLY CBT - Docker Bundle
>> %OUT_DIR%\README.txt echo ============================
>> %OUT_DIR%\README.txt echo.
>> %OUT_DIR%\README.txt echo On the target machine (only Docker required):
>> %OUT_DIR%\README.txt echo   1. Copy this folder to the machine.
>> %OUT_DIR%\README.txt echo   2. docker load -i atapoly-cbt-images.tar
>> %OUT_DIR%\README.txt echo   3. docker compose up -d
>> %OUT_DIR%\README.txt echo   4. Open http://^<machine-ip^>:3001

echo.
echo Done. Bundle in: %OUT_DIR%\
echo Copy the folder to any machine with Docker and run:
echo    docker load -i atapoly-cbt-images.tar ^&^& docker compose up -d
goto :eof

:err
echo.
echo Build failed.
exit /b 1
