@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION

REM This file serves as a translation/setup layer for windows scripts. It reads in the
REM variables from the configuration file in the root of the repo, and outputs them as
REM SET commands in a batch file. It then outputs paths based on the settings in that
REM batch file. The main reason for this file is because batch scripting with delayed
REM expansion can only go one level deep. Paths that are derivatives of other dynamic
REM paths just don't work. It's easier to build them here, then use them in the various
REM other scripts (configure.cmd, download.bat, quarto-bld.cmd).

REM Reads the configuration file line by line
REM and convert any export statements into set statements
REM (allows reuse of variables)

SET "FIND=export "
SET REPLACE=

IF EXIST %~dp0\..\..\win_configuration.bat DEL %~dp0\..\..\win_configuration.bat

ECHO REM This file is generated by the package/src/store_win_configuration.bat script. >> %~dp0\..\..\win_configuration.bat
ECHO REM   Do not edit it manually.  >> %~dp0\..\..\win_configuration.bat

FOR /F "tokens=*" %%A IN (%~dp0\..\..\configuration) DO (
  SET LINE=%%A

  IF "!LINE:~0,7!"=="%FIND%" (
    ECHO SET "!LINE:%FIND%=%REPLACE%!" >> %~dp0\..\..\win_configuration.bat
  )
)

echo SET "WIN_CONFIG_TRANSLATED=1" >> %~dp0\..\..\win_configuration.bat

echo set "QUARTO_ROOT=%%~dp0" >> %~dp0\..\..\win_configuration.bat

:: Full paths are what the Quarto code uses internally. You should never see a _DIR entry in there.
::    Note that the conda recipe defines these itself, so these are just for the
::    zip file archives that we build.

:: These are the paths within the Quarto source tree - the "package" subfolder.
echo set "QUARTO_SRC_PATH=%%~dp0src" >> %~dp0\..\..\win_configuration.bat
echo set "QUARTO_PACKAGE_PATH=%%~dp0%%QUARTO_PACKAGE_DIR%%" >> %~dp0\..\..\win_configuration.bat

:: These paths end up in the output package or conda build prefix.
echo set "QUARTO_DIST_PATH=%%~dp0%%QUARTO_PACKAGE_DIR%%\%%QUARTO_DIST_DIR%%" >> %~dp0\..\..\win_configuration.bat
echo set "QUARTO_SHARE_PATH=%%~dp0%%QUARTO_PACKAGE_DIR%%\%%QUARTO_DIST_DIR%%\%%QUARTO_SHARE_DIR%%" >> %~dp0\..\..\win_configuration.bat
echo set "QUARTO_BIN_PATH=%%~dp0%%QUARTO_PACKAGE_DIR%%\%%QUARTO_DIST_DIR%%\%%QUARTO_BIN_DIR%%" >> %~dp0\..\..\win_configuration.bat

echo "Translated/recorded configuration settings from unix configuration script"
