@echo off
setlocal enabledelayedexpansion

:: 檢查輸入參數
if "%~1"=="" (
    echo 使用方式: version-inc.bat [inc ^| time ^| major]
    exit /b 1
)

:: 讀版本號（移除前後空白）
set /p VERSION=<version.txt
for /f "tokens=* delims= " %%v in ("%VERSION%") do set VERSION=%%v
echo Current version: %VERSION%

:: 拆字串（主.次.修訂）
for /f "tokens=1,2,3 delims=.-" %%a in ("%VERSION%") do (
    set MAJOR=%%a
    set MINOR=%%b
    set PATCH=%%c
)

:: inc 模式：遞增 patch，不動 minor
if /i "%~1"=="inc" (
    set /a PATCH=!PATCH!+1
    set VERSION=!MAJOR!.!MINOR!.!PATCH!
)

:: time 模式：遞增 patch，並加上 -time
:: time 模式：加上時間戳
if /i "%~1"=="time" (
    set /a PATCH=!PATCH!+1

    rem ---- 抽取數字日期 (YYMMDD) ----
    set "RAW_DATE=%date%"
    set "NUM_DATE="
    for /l %%i in (0,1,19) do (
        set "ch=!RAW_DATE:~%%i,1!"
        for %%c in (0 1 2 3 4 5 6 7 8 9) do if "!ch!"=="%%c" set "NUM_DATE=!NUM_DATE!!ch!"
    )
    rem 只取最後6位 (YYMMDD)
    set "YYMMDD=!NUM_DATE:~-6!"

    rem ---- 抽取時間數字 (HHMM) ----
    set "RAW_TIME=%time%"
    set "NUM_TIME="
    for /l %%i in (0,1,19) do (
        set "ch=!RAW_TIME:~%%i,1!"
        for %%c in (0 1 2 3 4 5 6 7 8 9) do if "!ch!"=="%%c" set "NUM_TIME=!NUM_TIME!!ch!"
    )
    rem 只取前4位 (HHMM)
    set "HHMM=!NUM_TIME:~0,4!"

    rem ---- 組合完整時間戳 ----
    set "DT=!YYMMDD!!HHMM!"
    set VERSION=!MAJOR!.!MINOR!.!PATCH!-!DT!
)

:: major 模式：遞增 major，不動 minor 和 patch
if /i "%~1"=="major" (
    set /a MAJOR=!MAJOR!+1
    set VERSION=!MAJOR!.!MINOR!.!PATCH!
)

echo New version: %VERSION%

:: 寫回檔案
>version.txt echo %VERSION%

endlocal
