@echo off
setlocal EnableExtensions

rem Return codes:
rem   0   = allow install / requirement satisfied
rem   210 = installed FileWave Client is already >= 16.3.0 and not the target version

set "CLIENT_BIN_64=C:\Program Files\FileWave\client\fwcld.exe"
set "CLIENT_BIN_32=C:\Program Files (x86)\FileWave\fwcld.exe"
set "TARGET_VERSION=%~1"

if "%TARGET_VERSION%"=="" (
echo No target FileWave Client version supplied. Passing requirement.
exit /b 0
)

set "FWCLD="
if exist "%CLIENT_BIN_64%" set "FWCLD=%CLIENT_BIN_64%"
if not defined FWCLD if exist "%CLIENT_BIN_32%" set "FWCLD=%CLIENT_BIN_32%"

if not defined FWCLD (
echo FileWave Client binary not found. Passing requirement.
exit /b 0
)

for /f "tokens=2" %%V in ('"%FWCLD%" -V 2^>nul') do set "VERSION=%%V"

if not defined VERSION (
echo Could not detect FileWave Client version. Passing requirement.
exit /b 0
)

echo Detected FileWave Client version %VERSION%.

if "%VERSION%"=="%TARGET_VERSION%" (
echo FileWave Client already matches target %TARGET_VERSION%. Passing requirement.
exit /b 0
)

for /f "tokens=1,2,3 delims=." %%A in ("%VERSION%") do (
set "MAJOR=%%A"
set "MINOR=%%B"
)

if not defined MAJOR exit /b 0
if not defined MINOR exit /b 0

if %MAJOR% GTR 16 (
echo FileWave Client version is already above cutoff. Skipping install.
exit /b 210
)

if %MAJOR% LSS 16 (
echo FileWave Client version is below cutoff. Passing requirement.
exit /b 0
)

if %MINOR% GTR 3 (
echo FileWave Client version is already above cutoff. Skipping install.
exit /b 210
)

if %MINOR% LSS 3 (
echo FileWave Client version is below cutoff. Passing requirement.
exit /b 0
)

echo FileWave Client version is at or above cutoff and does not match target. Skipping install.
exit /b 210