setlocal enabledelayedexpansion

if exist "%OSGEO4W_ROOT%\httpd.d\httpd_qgis.conf" ren "%OSGEO4W_ROOT%\httpd.d\httpd_qgis.conf" httpd_qgis.conf.off-qwc-services
if exist "%OSGEO4W_ROOT%\httpd.d\httpd_qgis-ltr.conf" ren "%OSGEO4W_ROOT%\httpd.d\httpd_qgis-ltr.conf" httpd_qgis-ltr.conf.off-qwc-services
if not exist "%OSGEO4W_ROOT%\apps\qwc-services\projects" mkdir %OSGEO4W_ROOT%\apps\qwc-services\projects

set QGIS_PKG=qgis-ltr

if not exist "%OSGEO4W_ROOT%\httpd.d\httpd_qwc-services.conf" (
	call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
	for /f "usebackq tokens=1" %%a in (`python3 -c "import secrets; print(secrets.token_urlsafe(36));"`) do set JWT_SECURE_KEY=%%a

	textreplace -std ^
		-map @osgeo4w@ "%OSGEO4W_ROOT%" ^
		-map @o4wroot@ "%OSGEO4W_ROOT:\=/%" ^
		-map @windir@ "%WINDIR%" ^
		-map @temp@ "%TEMP%" ^
		-map @userprofile@ "%USERPROFILE%" ^
		-map @qgispkg@ "%QGIS_PKG%" ^
		-map @securekey@ "!JWT_SECURE_KEY!" ^
		-t "%OSGEO4W_ROOT%/httpd.d/httpd_qwc-services.conf"
)

if not exist "%OSGEO4W_ROOT%\apps\qwc-services\config\in\default\tenantConfig.json" (
	textreplace -std ^
		-map @o4wroot@ %OSGEO4W_ROOT:\=/% ^
		-t "%OSGEO4W_ROOT%/apps/qwc-services/config/in/default/tenantConfig.json"
)

if not exist "%OSGEO4W_ROOT%\apps\qwc-services\config\in\default\adminGuiConfig.json" (
	textreplace -std ^
		-map @o4wroot@ %OSGEO4W_ROOT:\=/% ^
		-t "%OSGEO4W_ROOT%/apps/qwc-services/config/in/default/adminGuiConfig.json"
)

if not exist "%OSGEO4W_ROOT%\apps\qwc-services\config\out\default" mkdir "%OSGEO4W_ROOT%\apps\qwc-services\config\out\default"

textreplace -std ^
        -t "%OSGEO4W_ROOT%/apps/qwc-services/venv/pyvenv.cfg"

net stop "Apache OSGeo4W Web server"

reg add "HKLM\SYSTEM\CurrentControlSet\Services\ApacheOSGeo4WWebserver" ^
	/f ^
	/v Environment ^
	/t REG_MULTI_SZ ^
	/d "PATH=%OSGEO4W_ROOT%\apps\%QGIS_PKG%\bin;%PATH%\0PYTHONHOME=%OSGEO4W_ROOT%\apps\Python312\0"

copy "%OSGEO4W_ROOT%\bin\libcrypto-1_1-x64.dll" "%OSGEO4W_ROOT%\apps\Python312\DLLs"
copy "%OSGEO4W_ROOT%\bin\libssl-1_1-x64.dll" "%OSGEO4W_ROOT%\apps\Python312\DLLs"

net start "Apache OSGeo4W Web server"

REM curl -X POST "http://127.0.0.1/config/generate_configs?tenant=default"

endlocal
textreplace -std -t apps/qwc-services/venv/Scripts/activate.bat
textreplace -std -t apps/qwc-services/venv/Scripts/alembic.exe
textreplace -std -t apps/qwc-services/venv/Scripts/dotenv.exe
textreplace -std -t apps/qwc-services/venv/Scripts/email_validator.exe
textreplace -std -t apps/qwc-services/venv/Scripts/flask.exe
textreplace -std -t apps/qwc-services/venv/Scripts/fonttools.exe
textreplace -std -t apps/qwc-services/venv/Scripts/jsonschema.exe
textreplace -std -t apps/qwc-services/venv/Scripts/mako-render.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pip.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pip3.12.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pip-3.12.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pip3.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pyftmerge.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pyftsubset.exe
textreplace -std -t apps/qwc-services/venv/Scripts/python.exe
textreplace -std -t apps/qwc-services/venv/Scripts/pythonw.exe
textreplace -std -t apps/qwc-services/venv/Scripts/qr.exe
textreplace -std -t apps/qwc-services/venv/Scripts/ttx.exe
textreplace -std -t apps/qwc-services/venv/Scripts/weasyprint.exe
