REM ========== prepare source
if "%ARCH%"=="64" (
    set PLATFORM=x64
    set DMSW=-DMS_WIN64
) else (
    set PLATFORM=Win32
    set DMSW=
)

REM %SYS_PREFIX%\Scripts\replace.exe "@DMSW@" "%DMSW%" Lib\distutils\cygwinccompiler.py
REM if errorlevel 1 exit 1

REM ========== actual compile step

set "tcltkDir=%LIBRARY_PREFIX%"
set "opensslRootDir=%LIBRARY_PREFIX%"

cd PCbuild
call build.bat -e -p %PLATFORM%
if errorlevel 1 exit 1
cd ..

REM ========== add stuff from official python.org msi

set MSI_DIR=.
for %%x in (Doc Tools) do (
    xcopy /s %MSI_DIR%\%%x %PREFIX%\%%x\
    if errorlevel 1 exit 1
)

if "%ARCH%"=="64" (
    set PCB=%SRC_DIR%\PCbuild\amd64
) else (
    set PCB=%SRC_DIR%\PCbuild\win32
)

mkdir %PREFIX%\DLLs
xcopy /s /y %PCB%\*.pyd %PREFIX%\DLLs\
if errorlevel 1 exit 1
copy /Y %PCB%\sqlite3.dll %PREFIX%\DLLs\
if errorlevel 1 exit 1
copy /Y %SRC_DIR%\PC\py.ico %PREFIX%\DLLs\
if errorlevel 1 exit 1
copy /Y %SRC_DIR%\PC\pyc.ico %PREFIX%\DLLs\
if errorlevel 1 exit 1

mkdir %PREFIX%\libs
copy /Y %PCB%\python27.lib %PREFIX%\libs\
if errorlevel 1 exit 1

REM ========== add stuff from our own build

xcopy /s %SRC_DIR%\Include %PREFIX%\include\
if errorlevel 1 exit 1
copy %SRC_DIR%\PC\pyconfig.h %PREFIX%\include\
if errorlevel 1 exit 1

for %%x in (python27.dll python.exe pythonw.exe) do (
    copy %PCB%\%%x %PREFIX%
    if errorlevel 1 exit 1
)
copy %PCB%\python27.lib %PREFIX%\libs\
if errorlevel 1 exit 1
del %PREFIX%\libs\libpython*.a
if errorlevel 1 exit 1

copy %PCB%\w9xpopen.exe %PREFIX%\
if errorlevel 1 exit 1

xcopy /s %SRC_DIR%\Lib %CONDA_DEFAULT_ENV%\Lib\
if errorlevel 1 exit 1
del /F /S /Q %PREFIX%\Lib\ensurepip 2>nul
if errorlevel 1 exit 1

REM ========== bytecode compile standard library

%PREFIX%\python.exe -Wi %CONDA_DEFAULT_ENV%\Lib\compileall.py -f -q -x "bad_coding|badsyntax|py3_" %CONDA_DEFAULT_ENV%\Lib
if errorlevel 1 exit 1

REM ========== add scripts

for %%x in (idle 2to3 pydoc) do (
    copy %SRC_DIR%\Tools\scripts\%%x %SCRIPTS%
    if errorlevel 1 exit 1
)

REM ========== generate grammar files for 2to3
%PREFIX%\python.exe %SCRIPTS%\2to3 -l
