cmake_minimum_required(VERSION 3.10)
project(mars_nwe)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)

enable_language(C)

SET (VERSION_MAJOR "0")
SET (VERSION_MINOR "99")
SET (VERSION_PATCH "28")

SET (MARS_NWE_VERSION_BASE "${VERSION_MAJOR}.${VERSION_MINOR}.pl${VERSION_PATCH}")
SET (MARS_NWE_VERSION "${MARS_NWE_VERSION_BASE}")

set(MARS_NWE_BUILD_DATE "" CACHE STRING "Mars NWE build date used in server info (DD-Mon-YY)")
if(MARS_NWE_BUILD_DATE STREQUAL "")
    string(TIMESTAMP MARS_NWE_BUILD_DATE "%d-%b-%y" UTC)
endif()

find_package(Git QUIET)

if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
    execute_process(
        COMMAND "${GIT_EXECUTABLE}" describe --tags --exact-match
        WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
        OUTPUT_VARIABLE GIT_EXACT_TAG
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )

    if(GIT_EXACT_TAG STREQUAL "v${MARS_NWE_VERSION_BASE}" OR
       GIT_EXACT_TAG STREQUAL "${MARS_NWE_VERSION_BASE}")
        set(MARS_NWE_VERSION "${MARS_NWE_VERSION_BASE}")
    else()
        execute_process(
            COMMAND "${GIT_EXECUTABLE}" rev-list --count HEAD
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE GIT_COMMIT_COUNT
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )

        execute_process(
            COMMAND "${GIT_EXECUTABLE}" rev-parse --short HEAD
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE GIT_SHORT_SHA
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )

        execute_process(
            COMMAND "${GIT_EXECUTABLE}" diff --quiet
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            RESULT_VARIABLE GIT_DIRTY_RESULT
        )

        set(MARS_NWE_VERSION
            "${MARS_NWE_VERSION_BASE}-dev.${GIT_COMMIT_COUNT}-g${GIT_SHORT_SHA}")

        if(NOT GIT_DIRTY_RESULT EQUAL 0)
            string(APPEND MARS_NWE_VERSION "-dirty")
        endif()
    endif()
endif()

INCLUDE(${CMAKE_ROOT}/Modules/GNUInstallDirs.cmake)
INCLUDE(${CMAKE_MODULE_PATH}/MarsNweInstallDirs.cmake)

# Add options for build
option(ENABLE_DEBUG "Compile in MARS NWE debug logging code (DO_DEBUG)" OFF)
option(ENABLE_DEBUG_BUILD "Build with debug compiler flags (-g3 -O0 -fno-omit-frame-pointer)" OFF)
option(ENABLE_DEBUG_DOSUTILS "Enable NCP 17/02 debugging support for mars_dosutils" OFF)
option(ENABLE_BURSTMODE "Enable experimental Packet Burst support" ON)
option(MAINTAINER_BUILD "Enable maintainer-only test helpers and diagnostics" OFF)
option(ENABLE_INTERNAL_RIP_SAP "Should we build Mars Nwe with Internal Router?" ON)
option(ENABLE_SHADOW_PWD "Should we build Mars Nwe with Shadow Password Support?" ON)
option(ENABLE_QUOTA_SUPPORT "Should we build Mars Nwe with Quota Support?" ON)
option(ENABLE_XATTR "Should we build Mars Nwe with extended attribute support?" ON)
option(ENABLE_NETATALK_LIBATALK "Enable optional Netatalk/libatalk AFP metadata backend" OFF)
option(MARS_NWE_INSTALL_DOSUTILS "Install DOS client utilities" ON)
option(MARS_NWE_INSTALL_NEW_DOSUTILS "Install the new/experimental DOS client utilities instead of legacy netold.exe" OFF)
option(MARS_NWE_BUILD_DOSUTILS "Build DOS client utilities with Open Watcom" OFF)

set(MARS_NWE_SMART_ADMIN_GROUP "root" CACHE STRING "Unix group allowed to log in to the SMArT/nwwebui admin interface")


# ENABLE_DEBUG controls the legacy DO_DEBUG code paths used by XDPRINTF().
# ENABLE_DEBUG_BUILD is separate and only changes compiler flags / build type.
# This allows release builds with compiled-in Mars debug logging, and also
# explicit -g/-O0 builds when debugging the server with gdb or valgrind.
if(ENABLE_DEBUG_BUILD)
    if(NOT CMAKE_CONFIGURATION_TYPES)
        set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
    endif()

    add_compile_options(
        $<$<COMPILE_LANGUAGE:C>:-g3>
        $<$<COMPILE_LANGUAGE:C>:-O0>
        $<$<COMPILE_LANGUAGE:C>:-fno-omit-frame-pointer>
    )
endif()

if(MAINTAINER_BUILD)
    add_compile_definitions(MAINTAINER_BUILD)
endif()

if(NOT CMAKE_CONFIGURATION_TYPES)
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
        "Debug" "Release" "RelWithDebInfo" "MinSizeRel")
endif()

IF (ENABLE_DEBUG)
    SET (MARS_NWE_DEBUG "1")
ELSE (ENABLE_DEBUG)
    SET (MARS_NWE_DEBUG "0")
ENDIF (ENABLE_DEBUG)

IF (ENABLE_DEBUG_DOSUTILS)
    SET (MARS_NWE_DEBUG_DOSUTILS "1")
ELSE (ENABLE_DEBUG_DOSUTILS)
    SET (MARS_NWE_DEBUG_DOSUTILS "0")
ENDIF (ENABLE_DEBUG_DOSUTILS)

IF (ENABLE_BURSTMODE)
    SET (MARS_NWE_ENABLE_BURSTMODE "1")
ELSE (ENABLE_BURSTMODE)
    SET (MARS_NWE_ENABLE_BURSTMODE "0")
ENDIF (ENABLE_BURSTMODE)

IF (ENABLE_INTERNAL_RIP_SAP)
    SET (MARS_NWE_INTERNAL_RIP_SAP "1")
ELSE (ENABLE_INTERNAL_RIP_SAP)
    SET (MARS_NWE_INTERNAL_RIP_SAP "0")
ENDIF (ENABLE_INTERNAL_RIP_SAP)

IF (ENABLE_SHADOW_PWD)
    SET (MARS_NWE_SHADOW_PWD "1")
ELSE (ENABLE_SHADOW_PWD)
    SET (MARS_NWE_SHADOW_PWD "0")
ENDIF (ENABLE_SHADOW_PWD)

find_package(Quota QUIET)
IF (ENABLE_QUOTA_SUPPORT AND QUOTA_FOUND)
    SET (MARS_NWE_QUOTA_SUPPORT "1")
ELSE (ENABLE_QUOTA_SUPPORT AND QUOTA_FOUND)
    SET (MARS_NWE_QUOTA_SUPPORT "0")
ENDIF (ENABLE_QUOTA_SUPPORT AND QUOTA_FOUND)

find_package(XAttr QUIET)
IF (ENABLE_XATTR AND XATTR_FOUND)
 SET (MARS_NWE_XATTR_SUPPORT "1")
ELSE (ENABLE_XATTR AND XATTR_FOUND)
 SET (MARS_NWE_XATTR_SUPPORT "0")
ENDIF (ENABLE_XATTR AND XATTR_FOUND)

find_package(Netatalk QUIET)
IF (ENABLE_NETATALK_LIBATALK AND NETATALK_FOUND)
 SET (MARS_NWE_NETATALK_SUPPORT "1")
ELSE (ENABLE_NETATALK_LIBATALK AND NETATALK_FOUND)
 SET (MARS_NWE_NETATALK_SUPPORT "0")
ENDIF (ENABLE_NETATALK_LIBATALK AND NETATALK_FOUND)

IF (NOT MAX_CONNECTIONS)
    SET (MAX_CONNECTIONS "50")
ENDIF (NOT MAX_CONNECTIONS)

IF (NOT MAX_VOLS)
    SET (MAX_VOLS "50")
ENDIF (NOT MAX_VOLS)

IF (NOT MAX_FILES)
    SET (MAX_FILES "256")
ENDIF (NOT MAX_FILES)

find_package(Crypt REQUIRED)
find_package(GDBM REQUIRED)

find_package(OpenSSL REQUIRED)
find_package(PAM REQUIRED)
find_package(DL REQUIRED)

if(MARS_NWE_QUOTA_SUPPORT)
 message(STATUS "Quota support: enabled")
else()
 if(ENABLE_QUOTA_SUPPORT)
  message(STATUS "Quota support: disabled (required headers/symbols not found)")
 else()
  message(STATUS "Quota support: disabled")
 endif()
endif()

if(MARS_NWE_XATTR_SUPPORT)
 message(STATUS "XAttr support: enabled")
else()
 message(STATUS "XAttr support: disabled")
endif()

if(MARS_NWE_NETATALK_SUPPORT)
 message(STATUS "Netatalk/libatalk AFP metadata backend: enabled")
else()
 if(ENABLE_NETATALK_LIBATALK)
  message(STATUS "Netatalk/libatalk AFP metadata backend: disabled (headers/library not found)")
 else()
  message(STATUS "Netatalk/libatalk AFP metadata backend: disabled")
 endif()
endif()

# we want to use systemd, if possible
set(SYSTEMD_SERVICES_INSTALL_DIR "" CACHE PATH "Directory for systemd service files")
INCLUDE(${CMAKE_MODULE_PATH}/systemdservice.cmake)
INCLUDE(${CMAKE_MODULE_PATH}/cupsutils.cmake)

message(STATUS "Mars Nwe version: ${MARS_NWE_VERSION}")
message(STATUS "bin: ${CMAKE_INSTALL_FULL_BINDIR}")
message(STATUS "sbin: ${CMAKE_INSTALL_FULL_SBINDIR}")
message(STATUS "lib: ${CMAKE_INSTALL_FULL_LIBDIR}")
message(STATUS "include: ${CMAKE_INSTALL_FULL_INCLUDEDIR}")
message(STATUS "libexec: ${CMAKE_INSTALL_FULL_LIBEXECDIR}")
message(STATUS "doc: ${CMAKE_INSTALL_FULL_DOCDIR}")
message(STATUS "man: ${CMAKE_INSTALL_FULL_MANDIR}")
message(STATUS "sysconf: ${CMAKE_INSTALL_FULL_SYSCONFDIR}")
message(STATUS "Mars Nwe libexec: ${MARS_NWE_INSTALL_FULL_LIBEXECDIR}")
message(STATUS "Mars Nwe config: ${MARS_NWE_INSTALL_FULL_CONFDIR}")
message(STATUS "Mars Nwe data: ${MARS_NWE_DATA_DIR}")
message(STATUS "Mars Nwe log: ${MARS_NWE_LOG_DIR}")
message(STATUS "Mars Nwe pid: ${MARS_NWE_PID_DIR}")
message(STATUS "SMArT admin group: ${MARS_NWE_SMART_ADMIN_GROUP}")
message(STATUS "Install DOS utilities: ${MARS_NWE_INSTALL_DOSUTILS}")
message(STATUS "Install new DOS utilities: ${MARS_NWE_INSTALL_NEW_DOSUTILS}")
message(STATUS "Build DOS utilities: ${MARS_NWE_BUILD_DOSUTILS}")
message(STATUS "Maintainer build: ${MAINTAINER_BUILD}")
message(STATUS "Packet Burst support: ${ENABLE_BURSTMODE}")

# put the include dirs which are in the source or build tree
# before all other include dirs, so the headers in the sources
# are prefered over the already installed ones
# since cmake 2.4.1
set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)

add_custom_target(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake")

add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)

add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(opt)
add_subdirectory(sys)
add_subdirectory(dosutils)
add_subdirectory(mail)
add_subdirectory(smart)
add_subdirectory(admin)

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MARtin Stovers NetWare-Emulator.")
SET(CPACK_PACKAGE_VENDOR "http://www.compu-art.de/mars_nwe/")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING")
set(CPACK_PACKAGE_VERSION_MAJOR "${VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "pl${VERSION_PATCH}")
set(CPACK_SOURCE_GENERATOR "TBZ2")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "mars_nwe-${MARS_NWE_VERSION}")
SET(CPACK_SOURCE_IGNORE_FILES "/build/" "/_build/" CMakeCache.txt CMakeFiles progress.make cmake_install.cmake CPackConfig.cmake CPackSourceConfig.cmake "\\\\.git" "\\\\.svn" "\\\\.swp$" "\\\\.cvs" "\\\\.tar.gz" "\\\\.o")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${MARS_NWE_VERSION}")
include(CPack)
