@echo off
setlocal enabledelayedexpansion
rem #########################################################################
rem
rem Swaps java files and jar files with jdk-compatible code and jar files.
rem
rem Environment Variables:
rem   NMROOT - Current NeXtMidas distribution
rem
rem Author: Zachary Matthews
rem Since:  NeXtMidas 4.1.1
rem
rem ##########################################################################

rem get java major version passed in from the nm.bat script
set /a java_version=%1

rem Loop over all option trees and check for the jdk_config.txt file in each one
for /f %%o in ('dir /b /a:d %NMROOT%\nxm') do (
  set opt=%%o
  set jdk_config=%NMROOT%\nxm\!opt!\cfg\jdk_config.txt
  if exist "!jdk_config!" (
    for /f "tokens=*" %%l in (!jdk_config!) do (
      set line=%%l
      echo !line! | findstr /r "#^*" >nul 2>&1 && (set continue=false) || (set continue=true)
      if !continue! == true (
         echo !line! | findstr /r '!^*' >nul 2>&1 && (set continue=false) || (set continue=true)
      )
      if !continue! == true (
        rem Get package and class or jar from each line read in
        for /f "tokens=1,* delims=." %%p in ("!line!") do (
          set pkg=%%p
          set class_or_jar=%%q
        )

        rem reset all local variables in between loops
        set files_with_ver=
        set num_files_with_ver=
        set last_file_with_ver=
        set class_or_jar_filepath=
        set max_ver_str=
        set max_ver_num=
        set file_ver_str=
        set file_ver_num=
        set file_ext=

        rem Get and sort all files for the given class or jar in the given package
        for /f "tokens=* delims=" %%f in ('dir %NMROOT%\nxm\!opt!\!pkg! /b /a-d /o:n ^| findstr /R ".jdk[0-9][0-9]"') do (
          set files_with_ver=!files_with_ver! %%f
          set /a num_files_with_ver=!num_files_with_ver!+1
          set last_file_with_ver=%%f
        )

        if not "!files_with_ver!" == "" (
          rem Make the full path to the given class or jar file
          set class_or_jar_filepath=%NMROOT%\nxm\!opt!\!pkg!\!class_or_jar!
          set max_ver_str=!last_file_with_ver:~-2!
          call :NORMALIZE_VER !max_ver_str! max_ver

          rem Determine the file extension
          if "!pkg!" == "jars" (
            set file_ext=jar
          ) else (
            set file_ext=java
          )

          rem If none of the swap-jdk files with version numbers support the current java version,
          rem then just use the corresponding swap-jdk file ending in .jdk
          rem Otherwise, loop over the whole set of swap-jdk files with version numbers
          if !max_ver! lss !java_version! (
            copy !class_or_jar_filepath!.jdk !class_or_jar_filepath!.!file_ext! >nul
          ) else (
            if !max_ver! equ !java_version! (
              copy !class_or_jar_filepath!.jdk!max_ver_str! !class_or_jar_filepath!.!file_ext! >nul
            ) else (
              set continue_ver_check=true
              for %%v in (!files_with_ver!) do (
                if !continue_ver_check! == true (
                  set file_with_ver=%%v
                  set file_ver_str=!file_with_ver:~-2!
                  call :NORMALIZE_VER !file_ver_str! file_ver
                  if !file_ver! geq !java_version! (
                    copy !class_or_jar_filepath!.jdk!file_ver_str! !class_or_jar_filepath!.!file_ext! >nul
                    set continue_ver_check=false
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)
goto :DONE

rem Normalize the version string to be a standard base10 integer not starting with 0
:NORMALIZE_VER
  set ver_str=%1
  set /a ver_first_digit=!ver_str:~0,1!
  if !ver_first_digit! == 0 (
    set /a ver=!ver_str:~1,1!
  ) else (
    set /a ver=!ver_str!
  )
  set /a %2=!ver!

  rem reset local variables
  set ver_str=
  set ver_first_digit=
  set ver=
exit /B 0

rem reset all local variables upon finishing the script
:DONE
set continue=
set continue_ver_check=
set opt=
set jdk_config=
set pkg=
set class_or_jar=
set files_with_ver=
set num_files_with_ver=
set last_file_with_ver=
set class_or_jar_filepath=
set max_ver_str=
set max_ver=
set file_ver_str=
set file_ver=
set file_ext=
