#  Copyright (C) 2026 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

# YL 13/02/2026

# without OpenMM
if("${OPENMM}" STREQUAL "")
    return()
endif()

# install as ChemShell's local library
set(ENV{PYTHONUSERBASE} ${CHEMSH_DIR}/build)

# YL 13/02/2026: installing via pip is the easiest way otherwise it needs a few dependencies such as SWIG and Doxygen
# YL 17/02/2026: removed --break-system-packages for compatibility with older versions of pip (using PIP_BREAK_SYSTEM_PACKAGES=1 instead)
set(OPENMM_INSTALL_COMMAND ${CMAKE_COMMAND} -E env PYTHONUSERBASE=${CHEMSH_DIR}/build
                                                   PIP_BREAK_SYSTEM_PACKAGES=1
                           ${Python3_EXECUTABLE} -m pip install --user openmm)

# NOTE: some CMake's ALWAYS in ExternalProject_Add_Step only accepts 1 or 0!
if(${OPENMM_RECOMPILE})
    set(OPENMM_RECOMPILE 1)
    set(OPENMM_INSTALL_COMMAND ${CMAKE_COMMAND} -E env PYTHONUSERBASE=${CHEMSH_DIR}/build
                                                       PIP_BREAK_SYSTEM_PACKAGES=1
                               ${Python3_EXECUTABLE} -m pip install --user --force-reinstall --no-deps openmm)
    file(REMOVE_RECURSE ${CHEMSH_ARCH}/stamp)
else()
    execute_process(COMMAND ${CMAKE_COMMAND} -E env PYTHONUSERBASE=${CHEMSH_DIR}/build
                            ${Python3_EXECUTABLE} -c "import openmm"
                    RESULT_VARIABLE OPENMM_IMPORT_RESULT
                    OUTPUT_QUIET ERROR_QUIET
    )
    if(OPENMM_IMPORT_RESULT EQUAL 0)
        set(OPENMM_RECOMPILE 0)
        set(OPENMM_INSTALL_COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --switch=$(COLOR) --blue --bold "Skipped OpenMM installation")
        message("${BoldRed}OPENMM_RECOMPILE${EndColour} set ${BoldWhite}OFF${EndColour}: will not reinstall OpenMM")
    else()
        set(OPENMM_RECOMPILE 1)
        file(REMOVE_RECURSE ${CHEMSH_ARCH}/stamp)
        message("${Green}OPENMM_RECOMPILE${EndColour} set ${BoldWhite}OFF${EndColour} but OpenMM not importable: OpenMM will still be installed")
    endif()
endif()

include(ExternalProject)

ExternalProject_Add(openmm
                    DOWNLOAD_COMMAND  ""
                    PREFIX            ${CHEMSH_ARCH}
                    STAMP_DIR         ${CHEMSH_ARCH}/stamp
                    TMP_DIR           ${CHEMSH_ARCH}/tmp
                    BUILD_ALWAYS      ${OPENMM_RECOMPILE}
                    CONFIGURE_COMMAND ""
                    BUILD_COMMAND     ${CMAKE_COMMAND} -E cmake_echo_color --switch=$(COLOR) --magenta --bold ""
                    INSTALL_COMMAND   ""
)

ExternalProject_Add_Step(openmm OPENMM_PIP_INSTALL
                         COMMENT   "Installing OpenMM via pip..."
                         ALWAYS    ${OPENMM_RECOMPILE}
                         DEPENDEES configure
                         COMMAND   ${OPENMM_INSTALL_COMMAND}
)
