@echo off
cd /D "%~dp0"

:: Set index url for installing haskoning_atr_tools
set index_url="https://corporateroot.pkgs.visualstudio.com/_packaging/haskoning-py/pypi/simple/"

:: Remove old dump file if they exist
if exist interpreter_version.txt (
   del interpreter_version.txt
   )
if exist path_interpreters.txt (
   del path_interpreters.txt
   )

setlocal enabledelayedexpansion

:: Get all global python interpreters
where python > path_interpreters.txt

:: Check if the user folder can be found
set user_folder="C:\Users\%USERNAME%\AppData\Local\Programs\Python"
if not exist %user_folder% (
   goto list_versions
)

for /R %user_folder% %%f in (*.exe) do (
   echo %%f | findstr /C:"python.exe">nul && (
      echo %%f | findstr /C:"Lib\venv\scripts">nul || (
         echo %%f >> path_interpreters.txt
      )
   )
)


:list_versions
:: Find valid interpreters among the found interpreters
echo.Retrieving interpreters binded to the system environment...
echo.
set count=0
set min_version=Python 3.11
set max_version=Python 3.13
for /f "tokens=*" %%i in (path_interpreters.txt) do (
   :: Check if interpreter is not empty string, exists and is not if size 0
   if not %%i == "" if exist %%i if not %%~zi == 0 (
      "%%i" --version > interpreter_version.txt
      for /f "tokens=*" %%f in (interpreter_version.txt) do (
         :: Check for valid versions
         echo %%f | findstr /C:"Python 3.7">nul && (
            echo %%f of %%i is not compatiable with haskoning_atr_tools. Minimal version is %min_version%
            echo.
            )
         echo %%f | findstr /C:"Python 3.8">nul && (
            echo %%f of %%i is not compatiable with haskoning_atr_tools. Minimal version is %min_version%
            echo.
            )
         echo %%f | findstr /C:"Python 3.9">nul && (
            echo %%f of %%i is not compatiable with haskoning_atr_tools. Minimal version is %min_version%
            echo.
            )
         echo %%f | findstr /C:"Python 3.10">nul && (
            echo %%f of %%i is not compatiable with haskoning_atr_tools. Minimal version is %min_version%
            echo.
            )
         echo %%f | findstr /C:"Python 3.11">nul && (
            set /a count=count+1
            set AVAILABLE_VERSION[!count!]=%%f
            set AVAILABLE_INTERPRETERS[!count!]=%%i
            )
         echo %%f | findstr /C:"Python 3.12">nul && (
            set /a count=count+1
            set AVAILABLE_VERSION[!count!]=%%f
            set AVAILABLE_INTERPRETERS[!count!]=%%i
            )
         echo %%f | findstr /C:"Python 3.13">nul && (
            set /a count=count+1
            set AVAILABLE_VERSION[!count!]=%%f
            set AVAILABLE_INTERPRETERS[!count!]=%%i
            )
         echo %%f | findstr /C:"Python 3.14">nul && (
            echo %%f of %%i is not compatiable with haskoning_atr_tools. Maximal version is %max_version%
            echo.
            )
         )
      )
   )


:: Check if interpreters are found
if count==0 (
   echo.No Interpreters that are binded to the system environment are found. Manual input is required or see https://datafusrfem.azurewebsites.net/developer_resources/add_python_to_path.html
   echo.
   goto userinput
   ) else (
   echo.Found valid interpreters are:
   echo.
   for /l %%x in (1,1,!count!) do (
   echo [%%x] !AVAILABLE_VERSION[%%x]! at !AVAILABLE_INTERPRETERS[%%x]!
   )
   echo.
)

:: Ask for found interpreter or manual one
:user_or_path
:: Set default in case of using enter instead of y
set USE_BINDED_INTERPRETER='y'
set /p "USE_BINDED_INTERPRETER=%count% interpreters are found, would you like to use one of them (default=%USE_BINDED_INTERPRETER%)? (y/n): "
echo.
if %USE_BINDED_INTERPRETER% equ n (
   goto userinput
   )

:: Loop through python interpreters
:path_interpreter_selection
set USE_SELECTED=1
set INTERPRETER="aa"
echo.Found interpreters are:
echo.
for /l %%x in (1,1,!count!) do (
   echo !AVAILABLE_VERSION[%%x]! | findstr /C:"Python 3.11">nul && (
      echo [%%x] !AVAILABLE_VERSION[%%x]! [DEFAULT] at !AVAILABLE_INTERPRETERS[%%x]!
      :: Reset default choice if default interpreter version is found
      set USE_SELECTED=%%x
      ) || (
      echo [%%x] !AVAILABLE_VERSION[%%x]! at !AVAILABLE_INTERPRETERS[%%x]!
      )
   )
echo.
set /p "USE_SELECTED= Give the list index of the version that you want to use (default=%USE_SELECTED%): "
echo.
set INT_VERSION=!AVAILABLE_VERSION[%USE_SELECTED%]!
set INTERPRETER="!AVAILABLE_INTERPRETERS[%USE_SELECTED%]!"

if not exist %INTERPRETER% (
   echo.The given interpreter number is not recognised. The selection procedure will be restarted.
   echo.
   goto user_or_path
)

goto end_interpreter_selection

:userinput
set /p "INTERPRETER=Give the path of the interpreter that you want to use (with quotation marks): "
echo.

if not exist %INTERPRETER% (
    echo.The given interpeter %INTERPRETER% does not exist, please retry.
    echo.
    goto userinput
)

:check_python_version
:: Get version of the python
%INTERPRETER% --version > interpreter_version.txt
set /p INT_VERSION=<interpreter_version.txt

:: Check if version is valid
echo %INT_VERSION% | findstr /C:"Python 3.11">nul && (goto end_interpreter_selection)
echo %INT_VERSION% | findstr /C:"Python 3.12">nul && (goto end_interpreter_selection)
echo %INT_VERSION% | findstr /C:"Python 3.13">nul && (goto end_interpreter_selection)
echo.Only versions between python 3.11 and 3.13 are valid for haskoning_atr_tools.
echo.The provided interpreter (%INT_VERSION%) is of the wrong version. Please retry.
echo.
goto userinput

:end_interpreter_selection
del interpreter_version.txt
del path_interpreters.txt
echo.Valid interpreter version %INT_VERSION% of %INTERPRETER% is selected
echo.

@REM Look for existing virtual environment, otherwise create new
if exist venv/ (echo Existing python environment found in venv/) else (echo Creating new virtual environment.. & %INTERPRETER% -m venv venv)
call venv/scripts/activate
echo.

@REM Opening of possible required pages
echo.For the [94mfirst installation[0m this webpage should be opened: https://corporateroot.visualstudio.com
echo.The devicelogin page will be opened automatically this might be required for installing. This page can be closed [94mAFTER[0m when the installation of the ATR tools is [94mFINISHED[0m.
pause
echo.

start "" https://corporateroot.visualstudio.com
start "" https://microsoft.com/devicelogin
pause
echo.

@REM Install artifacts keyring and haskoning_atr_tools
echo Now installing in virtual environment venv/..
echo.
python -m pip install --upgrade pip
pip install artifacts-keyring --upgrade
pip install haskoning_atr_tools --upgrade --extra-index-url=%index_url%
pip cache purge

@REM Show installed modules in environment
echo "Currently installed modules in venv/: "
pip list
echo.
echo "Installation script finished, you may now close this window"
pause
endlocal enabledelayedexpansion