#  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
# Aug 2019

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

# get ATEN source as external project
include(ExternalProject)

# name for external project
set(ATEN_NAME "aten")

if("${ATEN}" STREQUAL "")
    return()
endif()


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

# ATEN source location
# if file or directory 
if(EXISTS ${ATEN_ABSOLUTE})
    # do not use git
    set(ATEN_GIT_REPO "")
    set(ATEN_GIT      OFF)
    # set a git revision number (notice the missing '$'!)
    if(DEFINED ENV{ATEN_GIT_REVISION})
      set(ATEN_GIT_REVISION $ENV{ATEN_GIT_REVISION})
    else()
      set(ATEN_GIT_REVISION "unknown")
    endif()
    # if directory
    if(IS_DIRECTORY ${ATEN_ABSOLUTE})
        # TODO: should be obsolete
        # stop if not absolute path
        if(NOT IS_ABSOLUTE ${ATEN_ABSOLUTE})
            message(FATAL_ERROR "${BoldRed}ATEN_REPO not given in full absolute path. Exiting...${EndColour}")
        endif()
        set(ATEN_DIR     ${ATEN_ABSOLUTE})
        set(ATEN_SRC_DIR ${ATEN_ABSOLUTE})
        set(ATEN_TARBALL "")
    # if file (tar ball)
    else()
        set(ATEN_TARBALL ${ATEN})
    endif()
# else: use git repository
else()
    message("${BoldWhite}ATEN${EndColour} repository URL: ${ATEN_ABSOLUTE}")
    set(ATEN_GIT      ON)
    set(ATEN_GIT_REPO ${ATEN})
    set(ATEN_TARBALL  "")
    set(ATEN_DIR      ${CMAKE_CURRENT_LIST_DIR}/${CHEMSH_ARCH}/src/${ATEN_NAME})
    set(ATEN_SRC_DIR  ${ATEN_DIR})
endif()

set(ATEN_CMAKE_PREFIX ${CMAKE_CURRENT_LIST_DIR}/${CHEMSH_ARCH})

#                                            -DCMAKE_Fortran_COMPILER:STRING=${DLP_Fortran_COMPILER}
set(ATEN_CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
                                            ${ATEN_DIR})

set(ATEN_MAKE_COMMAND make -j${CHEMSH_MAKE_NJOBS})

# recompile
if(${ATEN_RECOMPILE})
    set(ATEN_RECOMPILE 1)
#    file(REMOVE_RECURSE ${ATEN_CMAKE_PREFIX}/stamp)
#    file(REMOVE_RECURSE ${ATEN_CMAKE_PREFIX}/tmp)
#    set(ATEN_CLEAN_COMMAND make clean)
    set(ATEN_CLEAN_COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --switch=$(COLOR) --blue --bold "Skipped make clean")
else()
    set(ATEN_RECOMPILE 0)
    set(ATEN_CLEAN_COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --switch=$(COLOR) --blue --bold "Skipped recompile step")
endif()

# SOURCE_DIR is the destination of extraction
#   BUILD_ALWAYS:
#   UPDATE_DISCONNECTED: ${ATEN_NO_UPDATE}, supported only after cmake 3.0
#   URL:                 file://${ATEN_TARBALL}
ExternalProject_Add(
    ${ATEN_NAME}
#    WORKING_DIRECTORY   ${ATEN_DIR}
    GIT_REPOSITORY      ${ATEN_GIT_REPO}
    GIT_SHALLOW         1
    ${ATEN_GIT_OPTIONS}
    # we temporarily disable updating until we find a proper way to avoid conflicting with chemsh's git
    UPDATE_DISCONNECTED 1
#    UPDATE_COMMAND      git pull --allow-unrelated-histories ${ATEN_GIT_REPO}
    UPDATE_COMMAND      pwd
    PREFIX              ${ATEN_CMAKE_PREFIX}
    STAMP_DIR           ${ATEN_CMAKE_PREFIX}/stamp
    TMP_DIR             ${ATEN_CMAKE_PREFIX}/tmp
    CONFIGURE_COMMAND   ""
    BUILD_COMMAND       ${CMAKE_COMMAND} -E cmake_echo_color
                                         --switch=$(COLOR)
                                         --magenta
                                         --bold ""
    INSTALL_COMMAND     ""
)

ExternalProject_Add_Step(
    ${ATEN_NAME} ATEN_CONFIGURE
    COMMENT           "Cleaning up previous binaries for '${ATEN_NAME}'..."
    ALWAYS            ${ATEN_RECOMPILE}
    DEPENDEES         configure
    COMMAND           ${ATEN_CONFIGURE_COMMAND}
    WORKING_DIRECTORY ${ATEN_DIR}
)

ExternalProject_Add_Step(
    ${ATEN_NAME} ATEN_BUILD
    COMMENT           "Building static library for '${ATEN_NAME}'..."
    ALWAYS            ${ATEN_RECOMPILE}
    # always preceded by ATEN_CONFIGURE
    DEPENDEES         ATEN_CONFIGURE
    COMMAND           ${ATEN_CLEAN_COMMAND} COMMAND ${ATEN_MAKE_COMMAND}
    WORKING_DIRECTORY ${ATEN_DIR}
)

configure_file(aten-chemsh.py.in aten-chemsh @ONLY)

file(INSTALL aten-chemsh
     DESTINATION ${CHEMSH_DIR}/bin/${CHEMSH_ARCH}
     FILE_PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
     NO_SOURCE_PERMISSIONS)
