#*** Powershell Build Script ***
# This script is only intended for debugging purposes on MS-Windows platform.

# execute under windows:
# $ powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -f <build-script>
# build a particular MSBuild project:
# $ msbuild BornAgainBase.vcxproj [-target:BornAgainBase] -property:Configuration=Release

# stop on first error
$ErrorActionPreference = "Stop"

$SRC_DIR = "C:/tmp/bornagain";
$OPT_DIR = "C:/opt/x64";
$QT_MSVC_DIR = "C:/Qt/6.2.4/msvc2019_64";
$QTCMake_DIR = "$QT_MSVC_DIR/lib/cmake";

# change the system PATH temporarily (needed for tests)
$PY_PLATFORM_DIR = "C:/Users/admin/.pyenv/pyenv-win/versions/3.11.5/";
# $PY_PLATFORM_DIR = "C:/Users/admin/.pyenv/pyenv-win/versions/3.10.11/";
# $PY_PLATFORM_DIR = "C:/Users/admin/.pyenv/pyenv-win/versions/3.9.13/";
# $PY_PLATFORM_DIR = "C:/Users/admin/.pyenv/pyenv-win/versions/3.8.10/";
# $PY_PLATFORM_DIR = "C:/Users/admin/.conda/envs/py311";
# $PY_PLATFORM_DIR = "C:/opt/multipython/Python310";

# vcpkg configuration
$VCPKG_DIR = "C:/opt/vcpkg/installed/x64-windows";
$VCPKG_INCLUDE_DIR = "$VCPKG_DIR/include";
$VCPKG_LIB_DIR = "$VCPKG_DIR/lib";  # .lib files
$VCPKG_BIN_DIR = "$VCPKG_DIR/bin"; # .dll files

# build directory
$BUILD_DIR = "build";

# set minimal path for build
# $Env:Path = "%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;$QT_MSVC_DIR\bin;C:\Program Files\Python39;$OPT_DIR\lib;$BOOST_LIB_DIR;C:\Program Files\Git\cmd;C:\Program Files\CMake\bin;C:\msys64\usr\bin;C:\msys64\mingw64\bin;C:\Program Files\ninja-win\;C:\Program Files (x86)\NSIS;"

echo "#--- DIAGNOSTICS ---"

# list powershell properties
echo "# Powershell <$PSHOME>`n  PS Profile <$PROFILE>`n  PS Command-Path <$PSCOMMANDPATH>"

# list all environmental variables
echo "----------------------------------------"
dir Env:

# make the CMake build directory
echo "#--- CONFIGURE ---"
cmake --version

# use Ninja

# Visual Studio path <https://github.com/microsoft/vswhere/wiki/Find-VC>

$vsPath = &"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationpath

echo "Microsoft Visual Studio path = '$vsPath'"

Import-Module (Get-ChildItem $vsPath -Recurse -File -Filter Microsoft.VisualStudio.DevShell.dll).FullName

Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation -DevCmdArguments '-arch=x64'

Set-Item -Path "env:CC" -Value "cl.exe"
Set-Item -Path "env:CXX" -Value "cl.exe"

# cmake -G "Ninja" -B"$BUILD_DIR" -S.
cmake -G "Visual Studio 17 2022" -A x64 -T host=x64 -B"$BUILD_DIR" -S. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$OPT_DIR;$PY_PLATFORM_DIR;$QT_CMAKE_DIR;$VCPKG_LIB_DIR" -DCMAKE_INCLUDE_PATH="$OPT_DIR/include;$VCPKG_INCLUDE_DIR" -DPython3_ROOT_DIR="$PY_PLATFORM_DIR" -DBA_PY_PACK=ON -DCPACK_IFW_ROOT="$QT_INSTALLER_DIR" -DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe"

echo "#--- BUILD ---"
cd "$BUILD_DIR"
pwd

$build_timer =  [system.diagnostics.stopwatch]::StartNew()
# MSBuild
# cmake --build . --config Release -j20  -- /fl /flp:logfile=BornAgainMSBuild.log

# Ninja
cmake --build . --config Release -j20

cmake --build . --config Release --target ba_wheel

$build_timer.Stop()

echo "#--- TEST ---"
$PATH0 = $env:PATH;
$env:PYTHONHOME = "$PY_PLATFORM_DIR";
$env:PYTHONPATH = "$BUILD_DIR/py/src/";
$env:PATH = "$PY_PLATFORM_DIR;$VCPKG_BIN_DIR;$OPT_DIR/lib;$QT_MSVC_DIR/bin;$PATH0";

$test_timer =  [system.diagnostics.stopwatch]::StartNew()
ctest -C Release --parallel 8 --output-on-failure
$test_timer.Stop()

echo "#--- PACKAGING ---"
# build package
cpack -C Release -B ./installer .

echo "----------------------------------------"
echo "#--- Total Build Time = $($build_timer.Elapsed.TotalMinutes) min(s)."
echo "#--- Total Test Time = $($test_timer.Elapsed.TotalMinutes) min(s)."
