# Copyright (C) 2009-2013 David Sugar, Tycho Softworks
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

cmake_minimum_required(VERSION 2.6)
PROJECT(ccscript)
set (VERSION 5.0.1)

set(RC_VERSION ${VERSION})
STRING(REGEX REPLACE "[.]" "," RC_VERSION ${RC_VERSION})
set(RC_VERSION "${RC_VERSION},0")

if(UNIX)
    execute_process(WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND "./cmake-abi.sh" OUTPUT_VARIABLE LIB_VERSION)
else()
    set(LIB_VERSION ${VERSION})
endif()

set(SOVERSION ${LIB_VERSION})
STRING(REGEX REPLACE "[.].*$" "" SOVERSION ${SOVERSION})

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckLibraryExists)

if(WIN32)
    set(INSTALL_MANDIR man)
    set(INSTALL_DOCDIR doc)
    set(INSTALL_LOCALE locale)
    set(INSTALL_LIBDIR bin)
    set(INSTALL_INFODIR info)
    set(SYSCONFDIR "etc" CACHE PATH "config directory")
    set(STATEDIR "var" CACHE PATH "state directory")
else()
    set(SYSCONFDIR "/etc" CACHE PATH "config directory")
    set(STATEDIR "/var" CACHE PATH "state directory")
endif()

if(NOT INSTALL_BINDIR)
    set(INSTALL_BINDIR bin)
endif()

if(NOT INSTALL_INCLUDEDIR)
    set(INSTALL_INCLUDEDIR include)
endif()

if(NOT INSTALL_MANDIR)
    set(INSTALL_MANDIR share/man)
endif()

if(NOT INSTALL_LOCALE)
    set(INSTALL_LOCALE share/locale)
endif()

if(NOT INSTALL_DOCDIR)
    set(INSTALL_DOCDIR share/doc/${PROJECT_NAME})
endif()

if(NOT INSTALL_LIBDIR)
    set(INSTALL_LIBDIR lib${LIB_SUFFIX})
endif()

if(NOT INSTALL_DATADIR)
    set(INSTALL_DATADIR data)
endif()

if(WIN32)
    set(DEFAULT_CFGPATH "${CMAKE_INSTALL_PREFIX}/${SYSCONFDIR}")
    set(DEFAULT_STATEDIR "${CMAKE_INSTALL_PREFIX}/${STATEDIR}")
else()
    set(DEFAULT_CFGPATH ${SYSCONFDIR})
    set(DEFAULT_STATEDIR ${STATEDIR})
endif()

set(DEFAULT_LIBPATH ${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBDIR})

if(MSVC60)
    MARK_AS_ADVANCED(BUILD_RUNTIME)
else()
    option(BUILD_RUNTIME "Set to OFF to build static runtime" ON)
endif()

option(BUILD_TESTING "Set to ON to build test programs" OFF)
MESSAGE( STATUS "Configuring GNU ccscript ${VERSION}...")

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_NAME} library")
SET(CPACK_PACKAGE_VENDOR              "David Sugar")
SET(CPACK_PACKAGE_DESCRIPTION_FILE    "${CMAKE_CURRENT_SOURCE_DIR}/README")
SET(CPACK_RESOURCE_FILE_LICENSE       "${CMAKE_CURRENT_SOURCE_DIR}/COPYING")
set(PACKAGE_FILE_NAME                 ${PROJECT_NAME})
set(PACKAGE_FILE_VERSION              ${VERSION})

IF (WIN32)
    SET(CPACK_PACKAGE_FILE_NAME "${PACKAGE_FILE_NAME}-${PACKAGE_FILE_VERSION}")
ELSE (WIN32)
    SET(CPACK_PACKAGE_FILE_NAME "${PACKAGE_FILE_NAME}-bin-${PACKAGE_FILE_VERSION}")
    if (NOT CPACK_GENERATOR)
        SET(CPACK_GENERATOR "TBZ2")
    endif()
ENDIF (WIN32)

IF(WIN32 AND NOT UNIX)
    SET(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_FILE_NAME}")

    # There is a bug in NSI that does not handle full unix paths properly. Make
    # sure there is at least one set of four (4) backlasshes.
    # SET(CPACK_PACKAGE_ICON "${CMake_SOURCE_DIR}/Utilities/Release\\\\InstallIcon.bmp")
    # SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\MyExecutable.exe")
    SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} ${PROJECT_NAME}$")
    SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\www.gnutelephony.org")
    SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\www.gnutelephony.org")
    SET(CPACK_NSIS_CONTACT "dyfet@gnutelephony.org")
    SET(CPACK_NSIS_MODIFY_PATH ON)

ENDIF(WIN32 AND NOT UNIX)

set(CPACK_COMPONENTS_ALL libraries headers)
set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME   "C++ Headers")
set(CPACK_COMPONENT_LIBRARIES_DESCRIPTION  "Dynamic library")
set(CPACK_COMPONENT_HEADERS_DESCRIPTION
    "Header files needed to build applications using ucommon library")
SET(CPACK_SOURCE_IGNORE_FILES ".bzr" "build" ".swp$" ".*~" ".svn" ".git")
SET(CPACK_SOURCE_GENERATOR "TBZ2")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${PACKAGE_FILE_NAME}-${PACKAGE_FILE_VERSION}")

# This must always be last!
INCLUDE(CPack)

# set to true for debug and trace during CMakeLists development
set(CMAKE_VERBOSE_MAKEFILE FALSE)

# Set defaults and pass common options.  Common build options can be passed
# to cmake using cmake -DWITH_CFLAGS="...", WITH_LIBS, and WITH_INCLUDES
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/inc ${WITH_INCLUDES})
add_definitions(${WITH_CFLAGS})
link_libraries(${WITH_LIBS})

if (USES_UCOMMON_INCLUDE_DIRS)
    message(STATUS "  Using local ucommon dependency")
else()
    find_package(PkgConfig)
    pkg_check_modules(USES_UCOMMON REQUIRED ucommon>=6.0.0)
endif()

include_directories(${USES_UCOMMON_INCLUDE_DIRS})
link_directories(${USES_UCOMMON_LIBRARY_DIRS})
add_definitions(${USES_UCOMMON_CFLAGS})

# by default we build static libs for windows, shared libs for unix.
# we may also set this from a top level cmake or -DWITH_XX_LIBS

if(BUILD_RUNTIME)
    set(BUILD_RUNTIME_TYPE SHARED)
else()
    set(BUILD_RUNTIME_TYPE STATIC)
endif()

check_include_files(endian.h HAVE_ENDIAN_H)
check_library_exists(m sqrt "" MATH_LIB)
if(MATH_LIB)
    set(MATH_LIB m)
endif()

configure_file(ccscript-config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/ccscript-config.h)

file(GLOB ccscript_src src/*.cpp)
set(ccscript_inc inc/ccscript.h)

add_library(ccscript ${BUILD_RUNTIME_TYPE} ${ccscript_src} ${ccscript_inc})
set_target_properties(ccscript PROPERTIES VERSION ${LIB_VERSION} SOVERSION ${SOVERSION})
add_dependencies(ccscript ucommon)
target_link_libraries(ccscript ${USES_UCOMMON_LIBRARIES} ${WITH_MATH_LIB} ${MATH_LIB})

if(BUILD_TESTING)
    ENABLE_TESTING()
    add_subdirectory(test)
endif()

if(UNIX)
    foreach(flag ${USES_UCOMMON_CFLAGS})
        set(PACKAGE_FLAGS "${PACKAGE_FLAGS} ${flag}")
    endforeach()
    foreach(lib ${USES_UCOMMON_LIBRARIES})
        STRING(REGEX REPLACE "^[-]l" "" slib ${lib})
        set(PACKAGE_LIBS "${PACKAGE_LIBS} -l${slib}")
    endforeach()

    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ccscript.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/ccscript.pc)

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ccscript.pc DESTINATION ${INSTALL_LIBDIR}/pkgconfig)
endif()

install(FILES inc/ccscript.h DESTINATION ${INSTALL_INCLUDEDIR})
install(TARGETS ccscript DESTINATION ${INSTALL_LIBDIR})

