#!/bin/sh
# Pre-commit hook: bump the 3rd version number (patch/sequence) and zero the 4th (build).
# The 3rd number is the monotonic commit count -- never resets across major/minor bumps.
# The 4th number tracks local CI builds between commits and resets here.
#
# Uses Invoke-DelphiIncVer from delphi-powershell-ci.
# Pattern matches the 4-part version string 

pwsh -NoProfile -Command '
  $result = Invoke-DelphiIncVer -File "projects/ide-plugin/Delphi.Terminal.Version.rc" -IncverTarget "RC" -IncverStyle "WinVer" -IncverPart "patch"
  if (-not $result.Success) {
    Write-Error "IncVer failed: $($result.Message)"
    exit 1
  }
  # Compile .rc to .res
    $brcc = $null
    if ($env:BDS) {
      $brcc = Join-Path $env:BDS "bin\brcc32.exe"
    }
    if (-not $brcc -or -not (Test-Path $brcc)) {
      $brcc = "C:\Delphi\RADStudio37\bin\brcc32.exe"
    }
    if (Test-Path $brcc) {
      $output = & $brcc "projects/ide-plugin/Delphi.Terminal.Version.rc" 2>&1
      if ($LASTEXITCODE -ne 0) {
        Write-Error "brcc32 failed: $output"
        exit 1
      }
    } else {
      Write-Warning "brcc32.exe not found -- .res not recompiled"
    }
  Write-Host "Version: $($result.OldVersion) -> $($result.NewVersion)"
'

# Stage the version file so it's included in the commit.
git add projects/ide-plugin/Delphi.Terminal.Version.rc projects/ide-plugin/Delphi.Terminal.Version.RES
