cmake_minimum_required(VERSION 2.8.12)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../cmake")
include(../cmake/macros.cmake)
include(../cmake/install_layout.cmake)
enable_testing()
set_variables()

project("RabbitMQ Consumer")

find_package(RabbitMQ)
find_package(MySQLClient)

if(NOT (RABBITMQ_FOUND AND MYSQLCLIENT_FOUND))
  message(FATAL_ERROR "Both the librabbitmq-c and mysqlclient libraries are required libraries.")
endif()

set(CMAKE_INSTALL_PREFIX "/usr")
message(STATUS "Installing to ${CMAKE_INSTALL_PREFIX}")

configure_file(${CMAKE_SOURCE_DIR}/consumer.c ${CMAKE_BINARY_DIR}/consumer.c)

set(CMAKE_C_FLAGS "-Wall -fPIC")
set(CMAKE_CXX_FLAGS "-Wall -fPIC")
include_directories(${MYSQLCLIENT_HEADERS})
include_directories(${RABBITMQ_HEADERS})
include_directories(inih)
add_subdirectory(inih)
add_executable (consumer ${CMAKE_BINARY_DIR}/consumer.c)

if(MYSQLCLIENT_STATIC_FOUND)
  target_link_libraries(consumer ${MARIADB_CONNECTOR_STATIC_LIB} rabbitmq inih ssl  crypt crypto dl z m pthread)
else()
  target_link_libraries(consumer ${MARIADB_CONNECTOR_LIB} rabbitmq inih ssl crypt crypto dl z m pthread)
endif()

install(TARGETS consumer DESTINATION ${MAXSCALE_BINDIR})
install(FILES consumer.cnf DESTINATION ${MAXSCALE_CONFDIR})

# See if we are on a RPM-capable or DEB-capable system
find_program(RPMBUILD rpmbuild)
find_program(DEBBUILD dpkg-buildpackage)
set(CPACK_GENERATOR "TGZ")
if(NOT (${RPMBUILD} MATCHES "NOTFOUND" ))
  message(STATUS "Generating RPM packages")
  set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
elseif(NOT (${DEBBUILD} MATCHES "NOTFOUND" ) )
  set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
  execute_process(COMMAND dpgk --print-architecture OUTPUT_VARIABLE DEB_ARCHITECTURE)
  set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEB_ARCHITECTURE})
  set (CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
  message(STATUS "Generating DEB packages for ${DEB_ARCHITECTURE}")
endif()

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "RabbitMQ Consumer")
set(CPACK_SET_DESTDIR ON)
set(CPACK_PACKAGE_VERSION_MAJOR "1")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_CONTACT "MariaDB Corporation Ab")
set(CPACK_PACKAGE_FILE_NAME "rabbitmq-consumer-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
set(CPACK_PACKAGE_NAME "rabbitmq-consumer")
set(CPACK_PACKAGE_VENDOR "MariaDB Corporation Ab")
set(CPACK_RPM_PACKAGE_NAME "rabbitmq-consumer")
set(CPACK_RPM_PACKAGE_VENDOR "MariaDB Corporation Ab")
set(CPACK_RPM_PACKAGE_LICENSE "MariaDB BSL")
include(CPack)
