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

rem obvci_appveyor_python_build_env.cmd is from conda-forge/obviousci,
rem so we shouldn't include it finally. I use it to set the correct
rem Windows SDK, but conda-build should take care of that (somehow?)
rem Set TARGET_ARCH for obvci_appveyor_python_build_env.cmd
echo "*** env before obvici ***"
set
if "%ARCH%" == "64" (
  set TARGET_ARCH=x64
) else (
  set TARGET_ARCH=x86
)

call %RECIPE_DIR%\obvci_appveyor_python_build_env.cmd
echo "*** env after obvici ***"
set

:: NOTE: you must have the DirectX SDK installed for ANGLE to compile:
::    https://www.microsoft.com/en-us/download/details.aspx?id=6812
::    (572 MB, not availabe through Conda)
::   You should also set the DXSDK_DIR environment variable once you have the SDK installed.

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 1
)

set _BLDTMP=C:\qt5-%ARCH%-%CONDA_PY%
set WEBTHING=qtwebengine
if "%DIRTY%" == "" (
    :: Shorten our build path as much as possible
    :: cd ..
    if %VS_MAJOR% LSS 14 (
        curl -LO "http://download.qt.io/community_releases/%SHORT_VERSION%/%PKG_VERSION%/qtwebkit-opensource-src-%PKG_VERSION%.tar.xz"
        if errorlevel 1 exit 1
        7za x -so qtwebkit-opensource-src-%PKG_VERSION%.tar.xz | 7za x -si -aoa -ttar > NUL 2>&1
        if errorlevel 1 exit 1
        MOVE qtwebkit-opensource-src-%PKG_VERSION% qtwebkit
        set WEBTHING=qtwebkit
    )
    if exist %_BLDTMP% (
      rmdir /s /q %_BLDTMP%
    )
    mkdir %_BLDTMP%
    :: Copy all files except bld.bat, which needs to stay in place while script runs
    robocopy %SRC_DIR%\ %_BLDTMP%\ *.* /E /NFL /NDL /xf bld.bat
)
pushd %_BLDTMP%

:: set path to find resources shipped with qt-5
:: see http://doc-snapshot.qt-project.org/qt5-5.4/windows-building.html
set "PATH=%CD%\qtbase\bin;%CD%\gnuwin32\bin;C:\Ruby23\bin;%PATH%"

:: Install a custom python 27 environment for us, to use in building webengine, but avoid feature activation
:: At present (5th July 2016) calling `conda create -y -n python27_qt5_build python=2.7` causes the build to
:: fail immediately after, so I'm bodging around that by not doing it if it exists.  This means you must run
:: the builds twice. Sorry. Time is not on my side here.
if "%VS_MAJOR%" == "14" (
  if not exist %SYS_PREFIX%\envs\python27_qt5_build (
    conda create -y -n python27_qt5_build python=2.7
  )
  set "PATH=%PREFIX%\..\python27_qt5_build;%PREFIX%\..\python27_qt5_build\Scripts;%PREFIX%\..\python27_qt5_build\Library\bin;%PATH%"
)

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

:: WebEngine (Chromium) specific definitions.  Only build this with VS 2015 (no support for python < 3.5)
IF %VS_MAJOR% LSS 14 GOTO NOWEBENGINE
    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%"=="64" goto x64
    set "PATH=%WSDK8%\bin\x86;%WDK$%\bin\x86;%PATH%"
    set "LIB=%LIB%;%WSDK8%\Lib\winv6.3\um\x86"
    goto done
    :x64
    set "PATH=%WSDK8%\bin\x64;%WDK$%\bin\amd64;%PATH%"
    set "LIB=%LIB%;%WSDK8%\Lib\winv6.3\um\x64"
    :done

    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%"
    GOTO WEBENGINEDONE
:NOWEBENGINE
    :: do not build webengine for VS 2008/2010
    RD /s /q qtwebengine
:WEBENGINEDONE

IF %VS_MAJOR% LSS 10 GOTO NOANGLE
    set OPENGLVER=dynamic
    GOTO ANGLEDONE
:NOANGLE
    set OPENGLVER=desktop
:ANGLEDONE

:: make sure we can find sqlite3:
set SQLITE3SRCDIR=%CD%\qtbase\src\3rdparty\sqlite

:: debugging: show env vars
set

:: 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\

:: 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 ^
     -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%VS_YEAR% ^
     -release ^
     -shared ^
     -system-libjpeg ^
     -system-libpng ^
     -system-zlib

rem     To get a quick turnaround you can add this: (remember also to add the hat symbol after -system-zlib)
rem     -skip %WEBTHING% -skip qtwebsockets -skip qtwebchannel -skip qtwayland -skip qtwinextras -skip qtsvg -skip qtsensors -skip qtcanvas3d -skip qtconnectivity -skip declarative -skip multimedia -skip qttools

:: jom is nmake alternative with multicore support, uses all cores by default
echo "jom release"
jom release
if errorlevel 1 exit /b 1
echo "jom install"
jom install

:: See above. AFAIK, `conda remove` also causes immediate build failure (and I want it kept for next time anyway).
rem conda remove -y -n python27_qt5_build --all

REM copy %PREFIX%\Library\lib\qt5 %PREFIX%\Library\lib\qt5.orig
echo "%PYTHON% %RECIPE_DIR%\patch_prefix_files.py %_BLDTMP%"
%PYTHON% %RECIPE_DIR%\patch_prefix_files.py %_BLDTMP%

:: To rewrite qt.conf contents per conda environment
copy "%RECIPE_DIR%\write_qtconf.bat" "%PREFIX%\Scripts\.qt-post-link.bat"

popd

rem RD /S /Q %_BLDTMP%
