#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# NB For python the SWIG module name must have the same name as the
# input .i file for CMake to generate the correct dependencies

set(CMAKE_SWIG_FLAGS "-threads")

include_directories (${PYTHON_INCLUDE_PATH})
if (BUILD_WITH_CXX)
   SET_SOURCE_FILES_PROPERTIES(cproton.i PROPERTIES CPLUSPLUS ON)
endif (BUILD_WITH_CXX)

list(APPEND SWIG_MODULE_cproton_EXTRA_DEPS
    ${CMAKE_SOURCE_DIR}/proton-c/include/proton/cproton.i
)

swig_add_module(cproton python cproton.i)
swig_link_libraries(cproton ${BINDING_DEPS} ${PYTHON_LIBRARIES})
set_target_properties(${SWIG_MODULE_cproton_REAL_NAME}
    PROPERTIES
    LINK_FLAGS "${CATCH_UNDEFINED}")

find_package(PythonInterp REQUIRED)

# configure the files needed for generating Pypi packages. Packages
# can be generated by running "python setup.py" from the build
# directory.
get_filename_component(PN_SWIG_PYTHON_C_WRAPPER
                       ${swig_generated_file_fullname} NAME)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/proton.py
               ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
               ${CMAKE_CURRENT_BINARY_DIR}/setup.py @ONLY)

find_program(EPYDOC_EXE epydoc)
mark_as_advanced (EPYDOC_EXE)
if (EPYDOC_EXE)
   add_custom_target(docs-py COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../../env.py --
     PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}:${CMAKE_CURRENT_SOURCE_DIR}
     ${EPYDOC_EXE} -v --no-private --html -o ${CMAKE_CURRENT_BINARY_DIR}/html
     ${CMAKE_CURRENT_SOURCE_DIR}/proton.py)
   add_dependencies(docs docs-py)
   install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html/"
           DESTINATION "${PROTON_SHARE}/docs/api-py"
           COMPONENT documentation
           ${OPTIONAL_ARG})
endif (EPYDOC_EXE)

if (CHECK_SYSINSTALL_PYTHON)
  # use the python-native install paths:

  install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} setup.py sdist
                                WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
  install(CODE "execute_process(COMMAND
   ${PYTHON_EXECUTABLE} setup.py --proton-install-prefix ${CMAKE_INSTALL_PREFIX} install
                          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")

else ()
  # install the bindings using the CMAKE path variables:
  set (PYTHON_SITEARCH_PACKAGES ${BINDINGS_DIR}/python)

  install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m py_compile cproton.py
                                WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
  install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -O -m py_compile cproton.py
                                WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
  install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m py_compile proton.py
                                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})")
  install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -O -m py_compile proton.py
                                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})")

  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cproton.py
                ${CMAKE_CURRENT_BINARY_DIR}/cproton.pyc
                ${CMAKE_CURRENT_BINARY_DIR}/cproton.pyo
                ${CMAKE_CURRENT_SOURCE_DIR}/proton.py
                ${CMAKE_CURRENT_SOURCE_DIR}/proton.pyc
                ${CMAKE_CURRENT_SOURCE_DIR}/proton.pyo
          DESTINATION ${PYTHON_SITEARCH_PACKAGES}
          COMPONENT Python)
  install(TARGETS ${SWIG_MODULE_cproton_REAL_NAME}
          DESTINATION ${PYTHON_SITEARCH_PACKAGES}
          COMPONENT Python)
endif()
