@echo on
setlocal EnableDelayedExpansion
set SHORT_VERSION=%PKG_VERSION:~0,-2%

:: if "%DXSDK_DIR%" == "" (
::   echo You do not appear to have the DirectX SDK installed.  Please get it from
::   echo    https://www.microsoft.com/en-us/download/details.aspx?id=6812
::   echo and try this build again.  If you have installed it, and are still seeing
::   echo this message, please open a new console to refresh your environment variables.
::   exit /b 1
:: )

:: Webengine requires either working OpenGL drivers or
:: Angle (therefore DirectX >= 11). This works on some
:: VMs and not others.  Windows 7 VirtualBox instantly
:: aborts when loading Spyder sSo we've had to disable
:: it globally.
:: Using Mesa from MSYS2 is mentioned as a workaround:
::  http://wiki.qt.io/Cross-compiling-Mesa-for-Windows
:: `set QT_OPENGL=software` forces Qt5 to use that but
:: but when I tried it it was too buggy; Spyder crashed a
:: little bit later, though it worked for Carlos.
set WEBBACKEND=qtwebengine

where perl.exe
if %ERRORLEVEL% neq 0 (
  echo Could not find perl.exe
  exit /b 1
)

:: make sure we can find ICU and openssl:
set "INCLUDE=%LIBRARY_INC%;%INCLUDE%"
set "LIB=%LIBRARY_LIB%;%LIB%"

:: Support systems with neither capable OpenGL (desktop mode) nor DirectX 11 (ANGLE mode) drivers
:: https://github.com/ContinuumIO/anaconda-issues/issues/9142
if not exist "%LIBRARY_BIN%" mkdir "%LIBRARY_BIN%"
copy opengl32sw\opengl32sw.dll  %LIBRARY_BIN%\opengl32sw.dll
if errorlevel 1 exit /b 1
if not exist %LIBRARY_BIN%\opengl32sw.dll exit /b 1

:: WebEngine (Chromium) specific definitions.  Only build this with VS 2015 (no support for python < 3.5)
if "%WEBBACKEND%" == "qtwebengine" (
  set "WSDK8=C:\\Program\ Files\ (x86)\\Windows\ Kits\\8.1"
  set "WDK=C:\\WinDDK\\7600.16385.1"
  set "INCLUDE=%WSDK8%\Include;%WDK%\inc;%INCLUDE%"
  if "%ARCH%"=="32" (
    set "PATH=%WSDK8%\bin\x86;%WDK$%\bin\x86;%PATH%"
    set "LIB=%LIB%;%WSDK8%\Lib\winv6.3\um\x86"
  ) else (
    set "PATH=%WSDK8%\bin\x64;%WDK$%\bin\amd64;%PATH%"
    set "LIB=%LIB%;%WSDK8%\Lib\winv6.3\um\x64"
  )
  set "GYP_DEFINES=windows_sdk_path='%WSDK8%'"
  set GYP_MSVS_VERSION=2015
  set GYP_GENERATORS=ninja
  set GYP_PARALLEL=1
  set "WDK_DIR=%WDK%"
  set "WindowsSDKDir=%WSDK8%"
) else (
  rmdir /s /q qtwebengine
)

set OPENGLVER=dynamic

:: Patch does not apply cleanly.  Copy file.
:: https://codereview.qt-project.org/#/c/141019/
copy %RECIPE_DIR%\tst_compiler.cpp qtbase\tests\auto\other\compiler\

:: TODO: should we always be rebuilding configure.exe anyway
:: Mentioned patch no longer applied
goto SKIP_REBUILD_CONFIGURE_EXE
:: If applying 0009-Win32-Re-permit-fontconfig-and-qt-freetype.patch (or
:: any patch that changes configureapp.cpp or any of the bootstrap files
:: in any way that alters the configure result) then configure.exe needs
:: to be rebuilt. Here I duplicate logic from configure.bat because that
:: file conflates needing to rebuild configure.exe with using a git repo
:: clone (OK, we should really remove that conflation instead and always
:: just rebuild configure.exe).
pushd qtbase
del /q configure.exe
set QTSRC=%CD%\
pushd tools\configure
set make=jom
set QTVERSION=%PKG_VERSION%
for /f "tokens=1,2,3,4 delims=.= " %%i in ('echo Qt.%QTVERSION%') do (
    set QTVERMAJ=%%j
    set QTVERMIN=%%k
    set QTVERPAT=%%l
)
:: .. end of specifically this bit is untested
echo #### Generated by configure.bat - DO NOT EDIT! ####> Makefile
echo/>> Makefile
set 
echo QTVERSION = %QTVERSION%>> Makefile
rem These must have trailing spaces to avoid misinterpretation as 5>>, etc.
echo QT_VERSION_MAJOR = %QTVERMAJ% >> Makefile
echo QT_VERSION_MINOR = %QTVERMIN% >> Makefile
echo QT_VERSION_PATCH = %QTVERPAT% >> Makefile
echo CXX = cl>>Makefile
echo EXTRA_CXXFLAGS =>>Makefile
rem This must have a trailing space.
echo QTSRC = %QTSRC% >> Makefile
set tmpl=win32
echo/>> Makefile
type Makefile.%tmpl% >> Makefile
%make%
popd
popd
:SKIP_REBUILD_CONFIGURE_EXE

:: See http://doc-snapshot.qt-project.org/qt5-5.4/windows-requirements.html
:: this needs to be CALLed due to an exit statement at the end of configure:
call configure ^
     -prefix %LIBRARY_PREFIX% ^
     -libdir %LIBRARY_LIB% ^
     -bindir %LIBRARY_BIN% ^
     -headerdir %LIBRARY_INC%\qt ^
     -archdatadir %LIBRARY_PREFIX% ^
     -datadir %LIBRARY_PREFIX% ^
     -L %LIBRARY_LIB% ^
     -I %LIBRARY_INC% ^
     -confirm-license ^
     -no-fontconfig ^
     -icu ^
     -no-separate-debug-info ^
     -no-warnings-are-errors ^
     -nomake examples ^
     -nomake tests ^
     -no-warnings-are-errors ^
     -opengl %OPENGLVER% ^
     -opensource ^
     -openssl ^
     -platform win32-msvc ^
     -release ^
     -shared ^
     -qt-freetype ^
     -system-libjpeg ^
     -system-libpng ^
     -system-zlib ^
     -system-sqlite ^
     -plugin-sql-sqlite

if errorlevel 1 exit /b 1

:: re-enable echoing which is disabled by configure
echo on
     
:: To get a much quicker turnaround you can add this: (remember also to add the hat symbol after -plugin-sql-sqlite)
::     -skip %WEBBACKEND% -skip qtwebsockets -skip qtwebchannel -skip qtwayland -skip qtwinextras -skip qtsvg -skip qtsensors ^
::     -skip qtcanvas3d -skip qtconnectivity -skip declarative -skip multimedia -skip qttools

jom -U release
if errorlevel 1 exit /b 1
echo Finished `jom -U release`
jom -U install
if errorlevel 1 exit /b 1
echo Finished `jom -U install`
pushd qtbase
  jom -U install_mkspecs
popd
if errorlevel 1 exit /b 1
echo Finished `jom -U install_mkspecs`

if exist %LIBRARY_BIN%\qmake.exe goto ok_qmake_exists
echo Warning %LIBRARY_BIN%\qmake.exe does not exist jom -U install failed, very strange. Copying it from qtbase\bin\qmake.exe
copy qtbase\bin\qmake.exe %LIBRARY_BIN%\qmake.exe
:ok_qmake_exists

:: To rewrite qt.conf contents per conda environment
copy "%RECIPE_DIR%\write_qtconf.bat" "%PREFIX%\Scripts\.qt-post-link.bat"
if errorlevel 1 exit /b 1
