@echo off

REM ============================================
REM  SMP Docker Container Package Update Script
REM  Usage: update_smp.bat [container_name]
REM  Default container name: smp
REM  Requires: at least one of ship-backend.jar / dist.zip in current directory
REM  Output is shown live AND saved to update_smp.log
REM ============================================

REM --------------------------------------------
REM Wrapper: run the main logic through PowerShell Tee-Object
REM so output goes to console AND to the log file.
REM The inner run is marked with the special arg ":LOGGED".
REM --------------------------------------------
if "%~1"==":LOGGED" goto :main

set "LOGFILE=%~dp0update_smp.log"
echo Starting update, logging to: %LOGFILE%
echo.
powershell -NoProfile -ExecutionPolicy Bypass -Command "& '%~f0' :LOGGED %~1 2>&1 | Tee-Object -FilePath '%LOGFILE%'; exit $LASTEXITCODE"

echo.
if errorlevel 1 (
    echo ****** FAILED - container was NOT updated. ******
) else (
    echo ****** SUCCESS - update finished. ******
)
echo ****** Full log saved to: %LOGFILE% ******
echo.
echo Press any key to close this window...
pause >nul
exit /b %ERRORLEVEL%


REM ============================================
REM Main logic (runs inside the logged session)
REM ============================================
:main
setlocal enabledelayedexpansion

REM Support optional container name argument (passed as %~2 by wrapper)
set "CONTAINER=%~2"
if "%CONTAINER%"=="" set "CONTAINER=smp"

echo ============================================
echo  Updating packages in container: %CONTAINER%
echo  Started at: %DATE% %TIME%
echo ============================================
echo.

REM --- Step 0: Check source files exist (at least one is required) ---
set "HAS_BACKEND=0"
set "HAS_FRONTEND=0"
if exist "ship-backend.jar" set "HAS_BACKEND=1"
if exist "dist.zip" set "HAS_FRONTEND=1"

if "%HAS_BACKEND%"=="0" if "%HAS_FRONTEND%"=="0" (
    echo [ERROR] Neither ship-backend.jar nor dist.zip found in current directory.
    echo          At least one package is required to update.
    goto :fail
)

if "%HAS_BACKEND%"=="1" echo   Found backend package : ship-backend.jar
if "%HAS_FRONTEND%"=="1" echo   Found frontend package: dist.zip
echo.

REM --- Step 0.1: Check docker is available ---
docker info >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Docker is not running or not installed.
    goto :fail
)

set "STEP=0"

REM --- Step 1: Remove old packages inside container (only the parts being updated) ---
set /a STEP+=1
if "%HAS_BACKEND%"=="1" if "%HAS_FRONTEND%"=="1" (
    echo [%STEP%] Removing old backend jar and frontend dist in container...
    docker exec %CONTAINER% sh -c "rm -rf /opt/seacon_pkg/ship-backend.jar /opt/seacon_pkg/dist /opt/seacon_pkg/dist.zip"
    if errorlevel 1 (
        echo [ERROR] Failed to remove old packages.
        goto :fail
    )
    goto :copy_backend
)
if "%HAS_BACKEND%"=="1" (
    echo [%STEP%] Removing old backend jar in container...
    docker exec %CONTAINER% sh -c "rm -rf /opt/seacon_pkg/ship-backend.jar"
    if errorlevel 1 (
        echo [ERROR] Failed to remove old backend jar.
        goto :fail
    )
    goto :copy_backend
)
echo [%STEP%] Removing old frontend dist in container...
docker exec %CONTAINER% sh -c "rm -rf /opt/seacon_pkg/dist /opt/seacon_pkg/dist.zip"
if errorlevel 1 (
    echo [ERROR] Failed to remove old frontend dist.
    goto :fail
)

:copy_backend
REM --- Step 2: Copy backend jar into container (if present) ---
if "%HAS_BACKEND%"=="0" goto :copy_frontend
set /a STEP+=1
echo [%STEP%] Copying ship-backend.jar ...
docker cp ship-backend.jar %CONTAINER%:/opt/seacon_pkg/
if errorlevel 1 (
    echo [ERROR] Failed to copy ship-backend.jar.
    goto :fail
)

:copy_frontend
REM --- Step 3: Copy frontend dist.zip into container (if present) ---
if "%HAS_FRONTEND%"=="0" goto :unzip_frontend
set /a STEP+=1
echo [%STEP%] Copying dist.zip ...
docker cp dist.zip %CONTAINER%:/opt/seacon_pkg/
if errorlevel 1 (
    echo [ERROR] Failed to copy dist.zip.
    goto :fail
)

:unzip_frontend
REM --- Step 4: Unzip frontend package inside container (if present) ---
if "%HAS_FRONTEND%"=="0" goto :restart
set /a STEP+=1
echo [%STEP%] Unzipping dist.zip inside container...
docker exec %CONTAINER% sh -c "unzip -o /opt/seacon_pkg/dist.zip -d /opt/seacon_pkg/dist/"
if errorlevel 1 (
    echo [ERROR] Failed to unzip dist.zip - check if unzip is installed in the container.
    echo          Also check the unzip error message above in this log.
    goto :fail
)

:restart
REM --- Final step: Restart container ---
set /a STEP+=1
echo [%STEP%] Restarting container...
docker restart %CONTAINER%
if errorlevel 1 (
    echo [ERROR] Failed to restart container.
    goto :fail
)

echo.
echo ============================================
echo  SUCCESS: update finished and container restarted.
echo  Finished at: %DATE% %TIME%
echo ============================================
endlocal
exit /b 0

:fail
echo.
echo  Update FAILED. Container was NOT restarted.
endlocal
exit /b 1
