#  Copyright (C) 2021 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/>.


# you.lu@stfc.ukri.org
# Nov 2021

if(NOT DEBUG)
    set(PIP_FLAGS -q)
endif()

# get the current pip version
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip --version
                OUTPUT_VARIABLE PIP_VERSION
                ERROR_QUIET)
if(NOT "${PIP_VERSION}" STREQUAL "")
    string(REGEX MATCH "[0-9]+[.]+[0-9]+([.]?[0-9])?" PIP_VERSION ${PIP_VERSION})
endif()
if("${PIP_VERSION}" STREQUAL "")
    message("pip is unavailable to the current Python. ChemShell will not install the plotting feature.")
    return()
    set(PIP_VERSION 20.0)
else()
    if(${PIP})
        message("pip version before upgrading: ${PIP_VERSION}")
    endif()
endif()

# --no-warn-script-location not available until pip v20.0
if(${PIP_VERSION} VERSION_GREATER_EQUAL 20.0)
    set(PIP_EXTRA_FLAGS --no-warn-script-location)
endif()

# YL 11/11/2021: first force an update of pip and numpy (dependency of matplotlib) so that pip won't try to
#                uninstall the existing system numpy (which is usually out of date) when installing matplotlib
if(${PIP})
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} -q --disable-pip-version-check --user --upgrade --force-reinstall ${PIP_EXTRA_FLAGS} pip)
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip --version
                    OUTPUT_VARIABLE PIP_VERSION_NEW
                    ERROR_QUIET)
endif()
if(NOT "${PIP_VERSION_NEW}" STREQUAL "")
    string(REGEX MATCH "[0-9]+[.]+[0-9]+([.]?[0-9])?" PIP_VERSION_NEW ${PIP_VERSION_NEW})
endif()
if(NOT "${PIP_VERSION}" STREQUAL "${PIP_VERSION_NEW}" AND ${PIP})
    message("pip version upgraded to: ${PIP_VERSION_NEW}")
endif()
# YL 04/08/2023: suppress pip DEPRECATION warnings by reinstalling these packages (NB: gpg requires `sudo apt install libgpgme-dev` which is not a pip package);
#                scipy is not a dependency, but just kept upgraded to resolve the conflict of newest version of NumPy
# YL 10/12/2023: latest dependencies including upgrades for suppressing version conflicts
if(${PIP})
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user --upgrade ${PIP_EXTRA_FLAGS} reportlab imageio)
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -q --user --upgrade --force-reinstall --no-warn-script-location distro-info scipy)
    message("Installing pip packages...")
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user --upgrade ${PIP_EXTRA_FLAGS} cycler fonttools pyparsing Pillow python_dateutil kiwisolver six)
    # YL 19/06/2024: restrained the NumPy version to 1.26.4 which comes with Ubuntu 24.04 by default (should be updated when NumPy 2.0 becomes mainstream)
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user --upgrade ${PIP_EXTRA_FLAGS} "numpy<=1.26.4")
endif()

# install matplotlib for plotting data
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import matplotlib; print(matplotlib.__version__)"
                OUTPUT_VARIABLE MATPLOTLIB_VERSION
                ERROR_QUIET)
if("${MATPLOTLIB_VERSION}" STREQUAL "" AND ${PIP})

    message("Installing Python matplotlib library for plotting...")
    # YL 12/05/2021: use --system to prevent --user as some versions throw out an error that --user and --prefix
    #                can't be used at the same time (but I didn't use --user at all!)
    if("${PYTHON_VERSION_MINOR}" STREQUAL "6")
        message("Command: ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check install --user matplotlib --no-deps")
        execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user matplotlib --no-deps)
    else()
        message("Command: ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user matplotlib --no-deps")
        execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PIP_FLAGS} --disable-pip-version-check --user matplotlib --no-deps)
    endif()
endif()



