@echo off

setlocal enableextensions disabledelayedexpansion

set TMP_DIR=C:\temp\backups
set APP_NAME=suite
set ARCHIVE=%APP_NAME%_%1.tgz

IF NOT [%1]==[] GOTO :specified

set "ARCHIVE_="

for /f "delims=" %%a in ('dir /b /o-d "%TMP_DIR%\%APP_NAME%_*.tgz" 2^>nul') do (
    if not defined ARCHIVE_ set "ARCHIVE=%%a"
    if not defined ARCHIVE_ set "ARCHIVE_=%%a"
)


:specified

set PSQL=C:\Software\PostgreSQL\9.6\bin\psql.exe
set GZIP=C:\Software\7-Zip\7z.exe

echo Using file %ARCHIVE%


set DATA_SRC_DIR=C:\data\%APP_NAME%

docker stop %APP_NAME%

%GZIP% x -so %TMP_DIR%\%ARCHIVE% | %GZIP% x -si -o%TMP_DIR%\%ARCHIVE%_tmp  -ttar

xcopy %TMP_DIR%\%ARCHIVE%_tmp\data %DATA_SRC_DIR% /s /e /y

%PSQL% -U postgres -d template1 < %TMP_DIR%\%ARCHIVE%_tmp\db\db.sql


rd /s/q "%TMP_DIR%\%ARCHIVE%_tmp"

docker start %APP_NAME%


echo Backup %ARCHIVE% restored


rem C:\Software\PostgreSQL\9.6\bin\psql.exe -U postgres -d template1 < C:\temp\db.sql