@echo off
cd /D "%~dp0"

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

@REM Prompt the user for desired version
set /p atrpackageversion="Which version of haskoning-atr-tools would you like to install? (Press enter to install latest): "
if [%atrpackageversion%]==[] (set installcommand=haskoning_atr_tools --upgrade) else (set installcommand=haskoning_atr_tools==%atrpackageversion%)

@REM Check if virtual environment venv is present
if exist venv/ (set envname=venv& goto updatevirtualenv) else if exist .venv\ (set envname=.venv) else (echo No virtual environment found with name venv & goto setvirtualenvname)
echo.

@REM Prompt the user for the name of their virtual environment
:setvirtualenvname
set /p envname="Name of virtual environment: "
if not exist %envname%/ (echo Virtual environment with name %envname% not found & goto setvirtualenvname)
echo.

@REM Update the art_tools
:updatevirtualenv
call %envname%/scripts/activate.bat

@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.

pip install --upgrade pip
pip install --upgrade artifacts-keyring
pip install %installcommand% --extra-index-url=%index_url%
pip cache purge

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