# vim: noautoindent:
cmake_minimum_required(VERSION 3.18...3.26 FATAL_ERROR)

project(TangoSourceDistribution LANGUAGES CXX)

set(SHELL "/usr/bin/env sh")

get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if(NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# switches
option(TSD_LIB "Install and use bundled libtango, if OFF use libtango found on system" ON)
option(TSD_CPP "Installation of cppTango and C++ applications" ON)
option(TSD_JAVA "Installation of java applications" ON)
option(TSD_DATABASE "Installation of tango database server" ON)
option(TSD_TAC "Installation of access control server" OFF)
option(TSD_HTML_DOCUMENTATION "Include the HTML documentation" ON)

# non-boolean knobs
SET(TSD_JAVA_PATH "" CACHE STRING "Path to the java interpreter")

if(TSD_JAVA)
  set(JAVA_HOME ${TSD_JAVA_PATH})

  find_package(Java 17 COMPONENTS Runtime REQUIRED)

  set(TSD_JAVA_VERSION ${Java_VERSION_STRING})
  set(TSD_JAVA_EXEC ${Java_JAVA_EXECUTABLE})
else()
  set(TSD_JAVA_VERSION "unknown")
  set(TSD_JAVA_EXEC "unknown")
endif()

SET(TSD_RC_FILE "/etc/tangorc")

if(TSD_CPP)
  # Overwrite settings of included projects
  option(BUILD_TESTING "Enable building the cppTango tests" OFF)

  if(TSD_LIB)
    set(tangoidl_FOUND ON CACHE BOOL "")
    set(IDL_VERSION_FOUND "6.0.4" CACHE STRING "")

    add_library(tangoidl::tangoidl INTERFACE IMPORTED)
    set_target_properties(tangoidl::tangoidl PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/lib/idl/include")

    list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  endif()
else()
  if(TSD_DATABASE)
    message(STATUS "Forcing TSD_DATABASE to OFF as TSD_CPP is disabled")
    set(TSD_DATABASE OFF)
  endif()

  if(TSD_TAC)
    message(STATUS "Forcing TSD_TAC to OFF as TSD_CPP is disabled")
    set(TSD_TAC OFF)
  endif()
endif()

add_subdirectory(lib)

add_subdirectory(cppserver)
add_subdirectory(doc)
add_subdirectory(pogo)

if(TSD_DATABASE)
  add_subdirectory(scripts)
endif()

add_subdirectory(utils)

# mysql/mariadb server is found in TangoDatabase cmake logic

set(TSD_OMNIORB_VERSION ${OMNIORB_VERSION})
set(TSD_OMNIORB_PATH ${omniORB4_INCLUDE_DIR})

set(TSD_ZMQ_PATH ${ZeroMQ_INCLUDE_DIR})
if(_ZeroMQ_PKG_VERSION)
  set(TSD_ZMQ_VERSION ${_ZeroMQ_PKG_VERSION})
else()
  set(TSD_ZMQ_VERSION "not-yet-supported")
endif()

set(TSD_CPPZMQ_PATH ${cppzmq_INCLUDE_DIR})
if(_cppzmq_PKG_VERSION)
  set(TSD_CPPZMQ_VERSION ${_cppzmq_PKG_VERSION})
else()
  set(TSD_CPPZMQ_VERSION "not-yet-supported")
endif()

# Unfortunately, TangoDatabase's FindMySQL.cmake does not cache MySQL_LIBRARY,
# only MySQL_LIBRARY_RELEASE and MySQL_LIBRARY_DEBUG.
if (NOT MySQL_LIBRARY)
  include(SelectLibraryConfigurations)
  select_library_configurations(MySQL)
endif()

set(TSD_MYSQL_CLIENT_LDLIBS ${MySQL_LIBRARY})
set(TSD_MYSQL_CLIENT_VERSION ${MySQL_VERSION})
set(TSD_MYSQL_SERVER_VERSION ${TDB_DB_VERSION})
set(TSD_MYSQL_SERVER_CONNECTION ${TDB_DB_CONNECTION_RESULT})

message(STATUS                                                                                                               "\
Summary:                                                                                                                   \n \
Configuration (TangoSourceDistribution):                                                                                   \n \
                                                                                                                           \n \
Source code location: ${CMAKE_SOURCE_DIR}                                                                                  \n \
Version: 10.1.2                                                                                                     \n \
Compiler: C++ ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}                                                       \n \
                                                                                                                           \n \
OMNIORB PATH: ${TSD_OMNIORB_PATH}                                                                                          \n \
OMNIORB VERSION: ${TSD_OMNIORB_VERSION}                                                                                    \n \
                                                                                                                           \n \
CPPZMQ PATH: ${TSD_CPPZMQ_PATH}                                                                                            \n \
CPPZMQ VERSION: ${TSD_CPPZMQ_VERSION}                                                                                      \n \
                                                                                                                           \n \
ZMQ PATH: ${TSD_ZMQ_PATH}                                                                                                  \n \
ZMQ VERSION: ${TSD_ZMQ_VERSION}                                                                                            \n \
                                                                                                                           \n \
JAVA PATH: ${TSD_JAVA_EXEC}                                                                                                \n \
JAVA VERSION: ${TSD_JAVA_VERSION}                                                                                          \n \
                                                                                                                           \n \
Database:                                                                                                                  \n \
                                                                                                                           \n \
DB NAME: ${TANGO_DB_NAME}                                                                                                  \n \
CLIENT LIB: ${TSD_MYSQL_CLIENT_LDLIBS}                                                                                     \n \
CLIENT VERSION: ${TSD_MYSQL_CLIENT_VERSION}                                                                                \n \
SERVER VERSION: ${TSD_MYSQL_SERVER_VERSION}                                                                                \n \
CONNECTION: ${TSD_MYSQL_SERVER_CONNECTION}                                                                                 \n \
                                                                                                                           \n \
Build:                                                                                                                     \n \
                                                                                                                           \n \
Use packaged cppTango:            ${TSD_LIB}                                                                               \n \
C++ applications and libraries :  ${TSD_CPP}                                                                               \n \
Java application :                ${TSD_JAVA}                                                                              \n \
Access control server:            ${TSD_TAC}                                                                               \n \
Database server:                  ${TSD_DATABASE}                                                                          \n \
HTML documentation:               ${TSD_HTML_DOCUMENTATION}                                                                \n \
Database schema create:           ${TDB_DATABASE_SCHEMA}                                                                   \n \
                                                                                                                           \n \
Please check whether the configuration I detected matches what you would like to have.")
