# Copyright (C) 2020-2021  CZ.NIC, z. s. p. o.
#
# This file is part of FRED.
#
# FRED is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FRED 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FRED.  If not, see <https://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.5)

# project details

include("cmake/package.cmake")

package(libpg
    FULL_VERSION "2.2.1")

project(LibPg
    VERSION ${PACKAGE_VERSION}
    LANGUAGES CXX)

##############################################
# Declare dependencies
set(PostgreSQL_ADDITIONAL_VERSIONS "13")
find_package(PostgreSQL ${PostgreSQL_ADDITIONAL_VERSIONS} REQUIRED)
find_package(Boost 1.53.0
    COMPONENTS
        system
        date_time
    REQUIRED)
find_package(PkgConfig REQUIRED)

include("cmake/package_dependencies.cmake")

add_package_dependencies(FROM_FILE "dependencies.txt")

configure_file(pg.pc.in pg.pc @ONLY)

##############################################
# Create target and set properties
add_library(pg STATIC
    src/extendable_query_arguments.cc
    src/libpq_layer.cc
    src/pg_connection.cc
    src/pg_result.cc
    src/pg_ro_transaction.cc
    src/pg_rw_transaction.cc
    src/pg_transaction.cc
    src/prepared_transaction.cc
    src/psql_type.cc
    src/query.cc
    src/query_parameter.cc
    src/time_zone.cc
    src/detail/libpq_layer_interface.cc
    src/detail/libpq_layer_impl.cc
    src/detail/unwrapper.cc
    src/detail/wrapper.cc
    src/sql_state/show_code.cc)

#Add an alias so that library can be used inside the build tree, e.g. when testing
add_library(LibPg::library ALIAS pg)

#Set target properties
target_include_directories(pg
    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        ${PostgreSQL_INCLUDE_DIRS}
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

target_compile_features(pg PRIVATE cxx_decltype_auto)
target_compile_options(pg PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra -ggdb -grecord-gcc-switches>)
set_target_properties(pg PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")

target_link_libraries(pg
    PUBLIC
        Boost::system
        Boost::date_time
        LibStrong::library
    PRIVATE
        ${PostgreSQL_LIBRARIES}
)

if(LibPg_BUILD_TESTS)
    add_subdirectory(test)
endif()

# install

get_directory_property(HAS_PARENT "PARENT_DIRECTORY")
if(NOT HAS_PARENT)
    include(GNUInstallDirs)

    install(TARGETS pg
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pg.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

    add_custom_target(uninstall
        COMMAND xargs -L10 rm -v < ${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt
        COMMAND cat ${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt | xargs -L1 dirname | sort | uniq | xargs -L10 rmdir -p --ignore-fail-on-non-empty)
endif()


# dist

include("cmake/package_dist.cmake")
package_dist(PACKAGE_NAME "${PACKAGE_NAME}"
             PACKAGE_PREFIX "${LIBPG_PREFIX}"
             PACKAGE_TARNAME "${PACKAGE_TARNAME}")
