#  Copyright (C) 2018 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 2018
# Mar 2021 revised

set(PROPKA_DEFAULT_VERSION 3.4.0)

# paths
set(CMAKE_Fortran_MODULE_DIRECTORY ${CHEMSH_BUILD_DIR}/modules/propka)
include_directories(${CHEMSH_BUILD_DIR}/modules
                    ${CHEMSH_BUILD_DIR}/modules/parallel
                    ${CHEMSH_BUILD_DIR}/modules/dl_py2f)

# get PROPKA source as external project
include(ExternalProject)

# name for external project
set(PROPKA_NAME "PROPKA")

if("${PROPKA}" STREQUAL "")

else()

    # try to convert to absolte full path: when it is a URL, it appears as "${CMAKE_CURRENT_SOURCE_DIR}${PROPKA}" and will not be taken as "EXISTS"
    get_filename_component(PROPKA_ABSOLUTE "${PROPKA}" ABSOLUTE)
    
    # PROPKA source location
    # if file or directory 
    if(EXISTS ${PROPKA_ABSOLUTE})
        # do not use git
        set(PROPKA_GIT_REPO "")
        set(PROPKA_GIT      OFF)
        # set a git revision number (notice the missing '$'!)
        if(DEFINED ENV{PROPKA_GIT_REVISION})
          set(PROPKA_GIT_REVISION $ENV{PROPKA_GIT_REVISION})
        else()
          set(PROPKA_GIT_REVISION "unknown")
        endif()
        # if directory
        if(IS_DIRECTORY ${PROPKA_ABSOLUTE})
            # TODO: should be obsolete
            # stop if not absolute path
            if(NOT IS_ABSOLUTE ${PROPKA_ABSOLUTE})
                message(FATAL_ERROR "${BoldRed}PROPKA_REPO not given in full absolute path. Exiting...${EndColour}")
            endif()
            set(PROPKA_DIR     ${PROPKA_ABSOLUTE})
            set(PROPKA_SRC_DIR ${PROPKA_ABSOLUTE})
            set(PROPKA_TARBALL "")
        # if file (tar ball)
        else()
            set(PROPKA_TARBALL ${PROPKA})
    #    set(PROPKA_DIR ${CHEMSH_ARCH}/src)
        endif()
    # else: use git repository
    else()
        message("${BoldWhite}PROPKA${EndColour} repository URL: ${PROPKA_ABSOLUTE}")
        set(PROPKA_GIT      ON)
        set(PROPKA_GIT_REPO ${PROPKA})
        set(PROPKA_GIT_TAG  "v${PROPKA_DEFAULT_VERSION}")
        set(PROPKA_TARBALL  "")
        set(PROPKA_DIR      ${CMAKE_CURRENT_LIST_DIR}/propka)
        set(PROPKA_SRC_DIR  ${PROPKA_DIR})
    endif()
    
    set(STAMPS_DIR ${PROPKA_DIR})
    
    # SOURCE_DIR is the destination of extraction
    #   BUILD_ALWAYS:
    #   UPDATE_DISCONNECTED: ${PROPKA_NO_UPDATE}, supported only after cmake 3.0
    #   URL:                 file://${PROPKA_TARBALL}
    ExternalProject_Add(
        ${PROPKA_NAME}
        PREFIX               ${CMAKE_CURRENT_SOURCE_DIR}
        STAMP_DIR            stamp
        TMP_DIR              tmp
        DOWNLOAD_DIR         download
        SOURCE_DIR           src
        GIT_REPOSITORY       ${PROPKA_GIT_REPO}
        GIT_TAG              ${PROPKA_GIT_TAG}
        GIT_SHALLOW          1
        GIT_PROGRESS         ${DEBUG}
        GIT_CONFIG           advice.detachedHead=false
        TIMEOUT              600
        DOWNLOAD_NO_PROGRESS YES
        CONFIGURE_COMMAND    ""
        BUILD_COMMAND        ""
        BUILD_IN_SOURCE      YES # important to prevent looking for directory PROPKA-build
        INSTALL_COMMAND      ""
        WORKING_DIRECTORY    ${CMAKE_CURRENT_SOURCE_DIR}
    )

endif()
