#############################################################################
#
#  $Id$
#
#  Copyright 2010 - 2016  Richard Hacker (lerichi at gmx dot net)
#                         Florian Pose <fp@igh-essen.com>
#
#  This file is part of the pdserv library.
#
#  The pdserv library 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.
#
#  The pdserv library 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 the pdserv library. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
#
#  This is the main cmake file for building process data communications server
#
#  The following options can be specified on the command line of cmake to
#  change the installation paths (Defaults are in <>)
#
#       -DCMAKE_INSTALL_PREFIX=</usr/local>
#       -DCMAKE_INSTALL_SYSCONFDIR=<${CMAKE_INSTALL_PREFIX}/etc>
#       -DCMAKE_INSTALL_INCLUDEDIR=<${CMAKE_INSTALL_PREFIX}/include>
#       -DCMAKE_INSTALL_BINDIR=<${CMAKE_INSTALL_PREFIX}/bin>
#       -DCMAKE_INSTALL_LIBDIR=<${CMAKE_INSTALL_PREFIX}/lib>
#
#       -DENABLE_EXAMPLE=<0>      # Compile examples
#
#  When a mercurial directory is detected, the library name will be set
#  to the repository ID. This prevents incompatabilites during development
#  and/or intermediate versions. To disable this, set
#       -DUSESOVERSION=1
#  
#  For debugging, use
#
#       -DCMAKE_BUILD_TYPE=Debug|Release|RelWithDebInfo|MinSizeRel
#       -DDEBUG=1               # prints debug messages in terminal
#       -DENABLE_TEST=1         # Compile tests (Requires
#                               #     -DCMAKE_BUILD_TYPE=Debug)
#
##############################################################################

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
PROJECT (pdserv C CXX)
SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)

IF (POLICY CMP0054)
    CMAKE_POLICY (SET CMP0054 NEW)  # Required by FindPkgConfig
ENDIF ()

#
# Release Instructions
#
# - Update version numbers below
# - Update ChangeLog (hg log --branch <branch> --style=changelog > ChangeLog)
# - Update NEWS file
# - commit
# - make dist
# - add tag release-x.x.x
#
SET (MAJOR_VERSION 3)
SET (MINOR_VERSION 1)
SET (PATCH_LEVEL 0)
MARK_AS_ADVANCED (MAJOR_VERSION MINOR_VERSION PATCH_LEVEL)

LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

# GNU Paths:
#       CMAKE_INSTALL_*
IF (${CMAKE_VERSION} VERSION_LESS "2.8.5")
    LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/compat/")
ENDIF ()

INCLUDE (GNUInstallDirs)
INCLUDE (CMakeDependentOption)
INCLUDE (InstallRequiredSystemLibraries)

SET (ENABLE_BUDDY "${ENABLE_BUDDY}" CACHE BOOL
    "Build etherlab_buddy" FORCE)

ENABLE_TESTING()

SET (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Process data communications library")
SET (CPACK_PACKAGE_VENDOR "Richard Hacker")
SET (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.txt")
SET (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING")
SET (CPACK_PACKAGE_VERSION_MAJOR ${MAJOR_VERSION})
SET (CPACK_PACKAGE_VERSION_MINOR ${MINOR_VERSION})
SET (CPACK_PACKAGE_VERSION_PATCH ${PATCH_LEVEL})
SET (CPACK_PACKAGE_INSTALL_DIRECTORY "CMake ${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}")
SET (CPACK_SOURCE_GENERATOR "TBZ2")
SET (CPACK_SOURCE_PACKAGE_FILE_NAME
      "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET (CPACK_GENERATOR "TGZ")
SET (CPACK_GENERATOR "DEB")
SET (CPACK_DEBIAN_PACKAGE_MAINTAINER "Richard Hacker") #required
LIST (APPEND CPACK_SOURCE_IGNORE_FILES
    "/build"
    "/\\\\.hg"
    "\\\\.sw.$"
    "~$"
    "\\\\.orig$"
    "\\\\.rej$"
    )

INCLUDE (CPack)
ADD_CUSTOM_TARGET (dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)

# Set up pthreads
SET (CMAKE_THREAD_PREFER_PTHREAD TRUE)
FIND_PACKAGE (Threads REQUIRED)

# Search for mercurial, used to add ID information to the library
FIND_PACKAGE (Mercurial)
IF (MERCURIAL_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.hg")
    MERCURIAL_HG_INFO (${PROJECT_SOURCE_DIR} Package)
ENDIF ()

# Require CommonC++
FIND_PACKAGE (Log4cplus REQUIRED)
FIND_PACKAGE (Yaml REQUIRED)
FIND_PACKAGE (BerkeleyDB REQUIRED)
FIND_PACKAGE (CyrusSASL REQUIRED)
FIND_PACKAGE (GnuTLS)

IF (UNIX)
    ADD_CUSTOM_TARGET (tags etags -R ${CMAKE_CURRENT_SOURCE_DIR})
    ADD_CUSTOM_TARGET (etags DEPENDS tags)
ENDIF ()

IF (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    ADD_COMPILE_OPTIONS (-Wall -Wextra)
ENDIF ()
    
ADD_SUBDIRECTORY (src)
ADD_SUBDIRECTORY (doc)

STRING (COMPARE EQUAL "${CMAKE_BUILD_TYPE}" "Debug" DEBUGON)
CMAKE_DEPENDENT_OPTION (ENABLE_TEST "Compile test subdirectory" ON
    "DEBUGON" OFF)
IF (ENABLE_TEST)
    ADD_SUBDIRECTORY (test)
ENDIF ()

OPTION (ENABLE_EXAMPLE "Compile examples subdirectory" OFF)
IF (ENABLE_EXAMPLE)
    ADD_SUBDIRECTORY (example)
ENDIF ()

OPTION ( DEBUG "Debugging output" OFF )
SET  (PDS_DEBUG ${DEBUG})

if (DEBUG)
    string(LENGTH "${PROJECT_SOURCE_DIR}/src/" SRC_PATH_LENGTH)
endif (DEBUG)

OPTION ( USESOVERSION "Set soversion to MAJOR.MINOR" OFF )

CONFIGURE_FILE (
    "${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
    )

CONFIGURE_FILE (
    "${PROJECT_SOURCE_DIR}/include/pdserv.h.in"
    "${PROJECT_BINARY_DIR}/include/pdserv.h"
    )

INSTALL (FILES "${PROJECT_BINARY_DIR}/include/pdserv.h"
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
    )

set (CONFFILE pdserv.conf)
IF (NOT EXISTS ${CMAKE_INSTALL_FULL_SYSCONFDIR}/${CONFFILE})
    INSTALL (FILES "${CMAKE_CURRENT_SOURCE_DIR}/${CONFFILE}"
        DESTINATION "${CMAKE_INSTALL_FULL_SYSCONFDIR}"
        )
ENDIF ()
