#  Copyright (C) 2019 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.ac.uk
# Oct 2019

# YL 24/09/2020: FetchContent not available until 3.11
cmake_minimum_required(VERSION 3.11)

# YL 30/11/2023
#include(${CHEMSH_DIR}/chemsh/utils/cmake/fetch_content.cmake)
include(FetchContent)

# function to get the path/url to an optional module
function(get_module MODULE_NAME MODULE_LOCATION DEST DEST_PREFIX EXTRACT_TARBALL GIT_INFO)

    # try to convert to absolte full path: when it is a URL, it appears as "${CMAKE_CURRENT_SOURCE_DIR}${MODULE_LOCATION}" and will not be taken as "EXISTS"
    get_filename_component(MODULE_ABSOLUTE "${MODULE_LOCATION}" ABSOLUTE)

    # MODULE_LOCATION source location
    # if file or directory
    if(EXISTS ${MODULE_ABSOLUTE})

        # if directory
        if(IS_DIRECTORY ${MODULE_ABSOLUTE})
            # TODO: should be obsolete
            # stop if not absolute path
            if(NOT IS_ABSOLUTE ${MODULE_ABSOLUTE})
                message(FATAL_ERROR "${BoldRed}MODULE_REPO not given in full absolute path. Exiting...${EndColour}")
            endif()
            set(MODULE_DIR     ${MODULE_ABSOLUTE})
            set(MODULE_SRC_DIR ${MODULE_ABSOLUTE})
            set(MODULE_SRC_DIR ${MODULE_ABSOLUTE} PARENT_SCOPE)
            set(MODULE_TARBALL "")

        # if file (tar ball)
        else()
            set(MODULE_TARBALL ${MODULE_LOCATION})
            set(MODULE_DIR           ${CHEMSH_DIR}/chemsh/interfaces/pdb2pqr)
            set(MODULE_SRC_DIR       ${MODULE_DIR})
            set(MODULE_SRC_DIR       ${MODULE_DIR} PARENT_SCOPE)
            set(MODULE_BINARY_DIR    ${MODULE_DIR}/${CHEMSH_ARCH}/pdb2pqr)
            file(MAKE_DIRECTORY ${MODULE_BINARY_DIR})
            FetchContent_Populate(
                module_source
                URL          ${MODULE_TARBALL}
                SOURCE_DIR   ${MODULE_SRC_DIR}
                BINARY_DIR   ${MODULE_BINARY_DIR}/build
                SUBBUILD_DIR ${MODULE_BINARY_DIR}/subbuild
            )
            FetchContent_Declare(
                module_ID
                GIT_REPOSITORY git@mycompany.com/git/projD.git
                GIT_TAG        7d9a17ad2c962aa13e2fbb8043fb6b8a
            )
            FetchContent_Populate(module_ID)
        endif()

    # else: use git repository
    else()
        message("${BoldWhite}${MODULE_NAME}${EndColour} repository URL: ${MODULE_LOCATION}")
        set(MODULE_GIT      ON)
        set(MODULE_GIT_REPO ${MODULE_LOCATION})
        set(MODULE_TARBALL  "")
        set(MODULE_DIR      ${CMAKE_CURRENT_LIST_DIR}/git)
        set(MODULE_SRC_DIR  ${MODULE_DIR})
        if("DEST_PREFIX" STREQUAL "")
            set(DEST_PREFIX src)
        endif()
        # YL 29/11/2023: optional argument ${ARGV4} is used as Git tag
        FetchContent_Declare(
            "${MODULE_NAME}"
            PREFIX               "${DEST}"
            STAMP_DIR            stamp
            TMP_DIR              tmp
            DOWNLOAD_DIR         download
            SOURCE_DIR           "${DEST_PREFIX}"
            URL                  "${MODULE_LOCATION}"
            TIMEOUT              600
            DOWNLOAD_NO_PROGRESS NO
            GIT_PROGRESS         YES
            GIT_TAG              "${GIT_INFO}"
        )
        if(EXTRACT_TARBALL)
            FetchContent_Populate(${MODULE_NAME})
        endif()
    endif()

endfunction(get_module)
