@echo off
rem __________________________________________________________________________
rem
rem INIT - set all Delphi-version-related variables to 0
rem __________________________________________________________________________
rem
if "%~1"=="init" (
  set is_delphi5=0
  set is_delphi6=0
  set is_delphi7=0
  set is_delphi8=0
  set is_delphi2005=0
  set is_delphi2006=0
  set is_delphi2007=0
  set is_delphi2009=0
  set is_delphi2010=0
  set is_delphixe=0
  set is_delphixe2=0
  set is_delphixe3=0
  set is_delphixe4=0
  set is_delphixe5=0
  set is_delphixe6=0
  set is_delphixe7=0
  set is_delphixe8=0
  set is_delphixe10=0
  set is_delphixe10_1=0
  set is_delphixe10_2=0
  set is_delphixe10_3=0
  set is_delphixe10_4=0
  set is_delphi5_up=0
  set is_delphi6_up=0
  set is_delphi7_up=0
  set is_delphi8_up=0
  set is_delphi2005_up=0
  set is_delphi2006_up=0
  set is_delphi2007_up=0
  set is_delphi2009_up=0
  set is_delphi2010_up=0
  set is_delphixe_up=0
  set is_delphixe2_up=0
  set is_delphixe3_up=0
  set is_delphixe4_up=0
  set is_delphixe5_up=0
  set is_delphixe6_up=0
  set is_delphixe7_up=0
  set is_delphixe8_up=0
  set is_delphixe10_up=0
  set is_delphixe10_1_up=0
  set is_delphixe10_2_up=0
  set is_delphixe10_3_up=0
  set is_delphixe10_4_up=0
  set has_unicode=0
  set has_namespaces=0
  exit /b 0
rem __________________________________________________________________________
rem
rem CHECK - set all relevant variables to their correct value
rem __________________________________________________________________________
rem
) else if "%~1"=="check" (
  if "%~2"=="" (
    call %0 "version" dcc32
  ) else (
    call %0 "version" %2
  )
  if not errorlevel 13 exit /b 1
  if errorlevel 13 (
    if not errorlevel 14 set is_delphi5=1
    set is_delphi5_up=1
  )
  if errorlevel 14 (
    if not errorlevel 15 set is_delphi6=1
    set is_delphi6_up=1
  )
  if errorlevel 15 (
    if not errorlevel 16 set is_delphi7=1
    set is_delphi7_up=1
  )
  if errorlevel 16 (
    if not errorlevel 17 set is_delphi8=1
    set is_delphi8_up=1
  )
  if errorlevel 17 (
    if not errorlevel 18 set is_delphi2005=1
    set is_delphi2005_up=1
  )
  if errorlevel 18 (
    if not errorlevel 19 set is_delphi2006=1
    set is_delphi2006_up=1
  )
  if errorlevel 19 (
    if not errorlevel 20 set is_delphi2007=1
    set is_delphi2007_up=1
  )
  if errorlevel 20 (
    if not errorlevel 21 set is_delphi2009=1
    set is_delphi2009_up=1
    set has_unicode=1
  )
  if errorlevel 21 (
    if not errorlevel 22 set is_delphi2010=1
    set is_delphi2010_up=1
  )
  if errorlevel 22 (
    if not errorlevel 23 set is_delphixe=1
    set is_delphixe_up=1
  )
  if errorlevel 23 (
    if not errorlevel 24 set is_delphixe2=1
    set is_delphixe2_up=1
    set has_namespaces=1
  )
  if errorlevel 24 (
    if not errorlevel 25 set is_delphixe3=1
    set is_delphixe3_up=1
  )
  if errorlevel 25 (
    if not errorlevel 26 set is_delphixe4=1
    set is_delphixe4_up=1
  )
  if errorlevel 26 (
    if not errorlevel 27 set is_delphixe5=1
    set is_delphixe5_up=1
  )
  if errorlevel 27 (
    if not errorlevel 28 set is_delphixe6=1
    set is_delphixe6_up=1
  )
  if errorlevel 28 (
    if not errorlevel 29 set is_delphixe7=1
    set is_delphixe7_up=1
  )
  if errorlevel 29 (
    if not errorlevel 30 set is_delphixe8=1
    set is_delphixe8_up=1
  )
  if errorlevel 30 (
    if not errorlevel 31 set is_delphixe10=1
    set is_delphixe10_up=1
  )
  if errorlevel 31 (
    if not errorlevel 32 set is_delphixe10_1=1
    set is_delphixe10_1_up=1
  )
  if errorlevel 32 (
    if not errorlevel 33 set is_delphixe10_2=1
    set is_delphixe10_2_up=1
  )
  if errorlevel 33 (
    if not errorlevel 34 set is_delphixe10_3=1
    set is_delphixe10_3_up=1
  )
  if errorlevel 34 (
    if not errorlevel 35 set is_delphixe10_4=1
    set is_delphixe10_4_up=1
  )
  exit /b 0
rem __________________________________________________________________________
rem
rem VERSION - perform the actual version check and return it as errorlevel
rem __________________________________________________________________________
rem
) else if "%~1"=="version" (
  setlocal enabledelayedexpansion
  for /f "tokens=*" %%i in ('%2') do (
    set line=%%i
    if not "!line: version =!"=="!line!" (
      if /i not "!line: version 13.0=!"=="!line!" endlocal && exit /b 13
      if /i not "!line: version 14.0=!"=="!line!" endlocal && exit /b 14
      if /i not "!line: version 15.0=!"=="!line!" endlocal && exit /b 15
      if /i not "!line: version 16.0=!"=="!line!" endlocal && exit /b 16
      if /i not "!line: version 17.0=!"=="!line!" endlocal && exit /b 17
      if /i not "!line: version 18.0=!"=="!line!" endlocal && exit /b 18
      if /i not "!line: version 18.5=!"=="!line!" endlocal && exit /b 18
      if /i not "!line: version 19.0=!"=="!line!" endlocal && exit /b 19
      if /i not "!line: version 20.0=!"=="!line!" endlocal && exit /b 20
      if /i not "!line: version 21.0=!"=="!line!" endlocal && exit /b 21
      if /i not "!line: version 22.0=!"=="!line!" endlocal && exit /b 22
      if /i not "!line: version 23.0=!"=="!line!" endlocal && exit /b 23
      if /i not "!line: version 24.0=!"=="!line!" endlocal && exit /b 24
      if /i not "!line: version 25.0=!"=="!line!" endlocal && exit /b 25
      if /i not "!line: version 26.0=!"=="!line!" endlocal && exit /b 26
      if /i not "!line: version 27.0=!"=="!line!" endlocal && exit /b 27
      if /i not "!line: version 28.0=!"=="!line!" endlocal && exit /b 28
      if /i not "!line: version 29.0=!"=="!line!" endlocal && exit /b 29
      if /i not "!line: version 30.0=!"=="!line!" endlocal && exit /b 30
      if /i not "!line: version 31.0=!"=="!line!" endlocal && exit /b 31
      if /i not "!line: version 32.0=!"=="!line!" endlocal && exit /b 32
      if /i not "!line: version 33.0=!"=="!line!" endlocal && exit /b 33
      if /i not "!line: version 34.0=!"=="!line!" endlocal && exit /b 34
      endlocal
      exit /b 0
    )
  )
  endlocal
)
exit /b 1
