@echo off
cd /d "%~dp0"
setlocal enabledelayedexpansion

REM === 載入 .env 變數（忽略註解與空行） ===
for /f "usebackq tokens=1,* delims==" %%a in (".env") do (
    if "%%a" neq "" (
        if not "%%a"=="REM" (
            set "%%a=%%b"
        )
    )
)

REM === 讀取版本 ========================
:: 呼叫 version-inc.bat 來更新版本（參數傳遞 inc 或 time）
call version-inc.bat time
:: 再次讀取 version.txt
set /p VERSION=<version.txt
REM ====================================

REM === 檢查變數 ========================
echo -----------------------------------
echo VERSION=!VERSION!
echo IMAGE_PROJ_NAME=!IMAGE_PROJ_NAME!
echo IMAGE_NAME=!IMAGE_NAME!
echo -----------------------------------
echo.

REM === 流程 ============================

@REM for /f "usebackq tokens=*" %%i in (`git describe --tags --always`) do (
@REM     set VERSION=%%i
@REM )
@REM echo Building version !VERSION!

:: 0. 移除舊 Image
docker rmi !IMAGE_PROJ_NAME!/!IMAGE_NAME!:!VERSION!

:: 1. 打包 映像檔 ( -f: 文件位置(注:是相對於目錄) -t: 標簽 ..: 上一層作爲根目錄(.: 表示當前當作根目錄))
docker build --no-cache -f ../Dockerfile -t !IMAGE_PROJ_NAME!/!IMAGE_NAME!:!VERSION! ../
