#
# Copyright (c) 2010-2015, Gilles Caulier, <caulier dot gilles at gmail dot com>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

project(kipi-plugins)

message(STATUS "----------------------------------------------------------------------------------")
message(STATUS "Starting CMake configuration for: ${PROJECT_NAME}")

# =======================================================
# Information to update before to release this package.

# kipi-plugins version
set(KIPIPLUGINS_MAJOR_VERSION "5")
set(KIPIPLUGINS_MINOR_VERSION "0")
set(KIPIPLUGINS_PATCH_VERSION "0")

# kipi-plugins release date
set(KIPIPLUGINS_RELEASE_DATE "2015-11-29")

# Suffix to add at end of version string. Usual values are:
# "-git"   : alpha code unstable from git. Do not use in production
# "-beta1" : beta1 release.
# "-beta2" : beta2 release.
# "-beta3" : beta3 release.
# "-rc"    : release candidate.
# ""       : final relase. Can be used in production.
set(KIPIPLUGINS_SUFFIX_VERSION "-beta2")

# =======================================================
# Set env. variables accordinly.

set(KIPIPLUGINS_VERSION_STRING
    "${KIPIPLUGINS_MAJOR_VERSION}.${KIPIPLUGINS_MINOR_VERSION}.${KIPIPLUGINS_PATCH_VERSION}${KIPIPLUGINS_SUFFIX_VERSION}"
)

# NOTE: This string is used to set libkipiplugins SO version ID
set(KIPIPLUGINS_LIB_SO_VERSION_STRING
    "${KIPIPLUGINS_MAJOR_VERSION}.${KIPIPLUGINS_MINOR_VERSION}.${KIPIPLUGINS_PATCH_VERSION}"
)

# =======================================================

set(CMAKE_MIN_VERSION   "2.8.12")
set(ECM_MIN_VERSION     "1.5.0")
set(KF5_MIN_VERSION     "5.1.0")
set(QT_MIN_VERSION      "5.2.0")
set(KIPI_MIN_VERSION    "5.0.0")
cmake_minimum_required(VERSION ${CMAKE_MIN_VERSION})

############## ECM setup ######################

find_package(ECM ${ECM_MIN_VERSION} CONFIG REQUIRED)
set(CMAKE_MODULE_PATH ${kipi-plugins_SOURCE_DIR}/cmake/modules ${ECM_MODULE_PATH})

# Cmake macros
include(GenerateExportHeader)
include(CheckFunctionExists)
include(FeatureSummary)
# ECM macros
include(ECMOptionalAddSubdirectory)
include(ECMAddTests)
include(ECMMarkNonGuiExecutable)
include(ECMGenerateHeaders)
include(ECMGeneratePriFile)
include(ECMPackageConfigHelpers)
include(ECMSetupVersion)
include(ECMInstallIcons)
include(ECMAddAppIcon)
# KDE macros
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDEFrameworkCompilerSettings)
include(KDECompilerSettings)
# Local macros
include(MacroUtils)
include(MacroLocalLibs)

############## Find Packages ###################

find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS
             Core
             Widgets
             PrintSupport
             Gui
             Xml
             XmlPatterns
             Svg
             Concurrent
             WebKitWidgets
             Network
)

find_package(Qt5 ${QT_MIN_VERSION} OPTIONAL_COMPONENTS
             OpenGL
)

if(BUILD_TESTING)
    find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS
                 Test)
endif()

find_package(KF5 ${KF5_MIN_VERSION}
             REQUIRED COMPONENTS
             Config
             WindowSystem
             XmlGui
             I18n
             KIO
             Service      # for SendImages (KToolInvocation)
             IconThemes   # for GoogleServices (KIO/PixmapLoader)
)

find_package(KF5 ${KF5_MIN_VERSION}
             OPTIONAL_COMPONENTS
             Archive      # for FlashExport
             ThreadWeaver # for Panorama
             CalendarCore # for Calendar
)

# Dependencies detection required by all plugins

DETECT_LIBKIPI(${KIPI_MIN_VERSION})

if(KF5Kipi_FOUND)

    # -- Optional dependencies detection required by some plugins -------------------------------------

    DETECT_LIBMEDIAWIKI(5.0.0)                                                 # For Mediawiki.
    DETECT_LIBKVKONTAKTE(4.70.0)                                               # For VKontakte.
    find_package(OpenGL)                                                       # For AdvancedSlideshow and ImageViewer.
    find_package(FLEX)                                                         # For Panorama (must be found before KdepimLibs for MacOS).
    find_package(BISON)                                                        # For Panorama (must be found before KdepimLibs for MacOS).
    find_package(QtKOAuth)                                                     # For Imgur Auhtenticated API.
    find_package(Phonon4Qt5)                                                   # For AdvancedSlideShow.

    # -- X11 check, LINUX Only -------------------------------------------------------------------------

    if(NOT WIN32 AND NOT APPLE)
        find_package(X11)                                                      # For AdvancedSlideshow and ImageViewer.
    endif()

    # ==================================================================================================
    # Log messages

    message(STATUS "")
    message(STATUS "----------------------------------------------------------------------------------")
    message(STATUS " ${PROJECT_NAME} ${KIPIPLUGINS_VERSION_STRING} dependencies results   <http://www.digikam.org>")
    message(STATUS "")

    # Require shared libraries results.

    PRINT_LIBRARY_STATUS("libqt"     "http://www.qtsoftware.com"                                        "(version >= ${QT_MIN_VERSION})"     Qt5_FOUND)
    PRINT_LIBRARY_STATUS("libkde"    "http://www.kde.org"                                               "(version >= ${KF5_MIN_VERSION})"    KF5_FOUND)
    PRINT_LIBRARY_STATUS("libkipi"   "https://projects.kde.org/projects/kde/kdegraphics/libs/libkipi"   "(version >= ${KIPI_MIN_VERSION}))"  KF5Kipi_FOUND)

    # Optional shared libraries results.

    PRINT_OPTIONAL_LIBRARY_STATUS("libkarchive"     "https://projects.kde.org/projects/frameworks/karchive"           "(version >= ${KF5_MIN_VERSION})"                  "Kipi-plugins will be compiled without libkarchive support."     KF5Archive_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libthreadweaver" "https://projects.kde.org/projects/frameworks/threadweaver"       "(version >= ${KF5_MIN_VERSION})"                  "Kipi-plugins will be compiled without libthreadweaver support." KF5ThreadWeaver_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libmediawiki"    "https://projects.kde.org/projects/extragear/libs/libmediawiki"   "(version >= ${KF5_MIN_VERSION})"                  "Kipi-plugins will be compiled without libmediawiki support."    KF5MediaWiki_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libkvkontakte"   "https://projects.kde.org/projects/extragear/libs/libkvkontakte"  "(version >= 4.70.0)"                 "Kipi-plugins will be compiled without libkvkontakte support."   KF5Vkontakte_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libkcalcore"     "https://projects.kde.org/projects/kde/pim/kcalcore"              "(version >= 4.81.0)"                 "Kipi-plugins will be compiled without libkcalcore support."     KF5CalendarCore_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libphonon"       "https://projects.kde.org/projects/kdesupport/phonon"             "(version >= 4.8.3)"                  "Kipi-plugins will be compiled without libphonon support."       Phonon4Qt5_FOUND)
    PRINT_OPTIONAL_LIBRARY_STATUS("libqtkoauth"     "https://github.com/kypeli/kQOAuth"                               "(version >= 0.97)"                   "Kipi-plugins will be compiled without libqtkoauth support."     QTKOAUTH_FOUND)

    PRINT_OPTIONAL_LIBRARY_STATUS("native Opengl support"  "" "" "Kipi-plugins will be compiled without native OpenGL support."  OPENGL_FOUND AND OPENGL_GLU_FOUND)
    PRINT_OPTIONAL_QTMODULE_STATUS("Qt5 OpenGL"            Qt5OpenGL_FOUND)

    # LINUX Only.
    if(NOT WIN32 AND NOT APPLE)
        PRINT_OPTIONAL_LIBRARY_STATUS("libX11" "http://www.x11.org" "(version >= 1.1.5)" "Kipi-plugins will be compiled without libX11 support." X11_FOUND AND X11_Xrandr_FOUND)
    endif()

    # Optional plugins results ----------------------------------------------------------------------------

     message(STATUS "")

     if(Qt5_FOUND AND KF5_FOUND AND KF5Kipi_FOUND)
         set(KIPI_COMPILE_MESSAGE "kipi-plugins will be compiled")
         FILL_WITH_DOTS(KIPI_COMPILE_MESSAGE)
         message(STATUS " ${KIPI_COMPILE_MESSAGE} YES")
         set(KIPIPLUGINS_CAN_BE_COMPILED true)
     else()
         message(FATAL_ERROR " kipi-plugins will not be compiled! Look README file for more details about dependencies...")
         set(KIPIPLUGINS_CAN_BE_COMPILED false)
     endif()

     # decide if AdvancedSlideshow and ImageViewer can be built
     if(NOT WIN32 AND NOT APPLE)
         if(X11_FOUND AND X11_Xrandr_FOUND AND OPENGL_FOUND AND OPENGL_GLU_FOUND AND Qt5OpenGL_FOUND)
             set(BUILD_VIEWERS TRUE)
         else()
             set(BUILD_VIEWERS FALSE)
         endif()
     else()
         if(OPENGL_FOUND AND OPENGL_GLU_FOUND AND Qt5OpenGL_FOUND)
             set(BUILD_VIEWERS TRUE)
         elseif()
             set(BUILD_VIEWERS FALSE)
         endif()
     endif()

    PRINT_COMPONENT_COMPILE_STATUS("ImageViewer"        BUILD_VIEWERS)
    PRINT_COMPONENT_COMPILE_STATUS("Panorama"           FLEX_FOUND AND BISON_FOUND AND KF5ThreadWeaver_FOUND)
    PRINT_COMPONENT_COMPILE_STATUS("VKontakte"          KF5Vkontakte_FOUND)
    PRINT_COMPONENT_COMPILE_STATUS("Mediawiki"          KF5MediaWiki_FOUND)
    PRINT_COMPONENT_COMPILE_STATUS("AdvancedSlideshow"  BUILD_VIEWERS)
    PRINT_COMPONENT_COMPILE_STATUS("Calendar"           KF5CalendarCore_FOUND)
    PRINT_COMPONENT_COMPILE_STATUS("FlashExport"        KF5Archive_FOUND)

    # ==================================================================================================

    if(KIPIPLUGINS_CAN_BE_COMPILED)

        # ==================================================================================================
        # Create git version header

        # We only do this IF we are in a .git dir
        find_file(GIT_MARKER entries PATHS ${CMAKE_SOURCE_DIR}/.git)

        if(NOT GIT_MARKER)
            set (GIT_MARKER ${CMAKE_SOURCE_DIR}/CMakeLists.txt)  # Dummy file
        endif()

        # Add a custom command to drive the git script whenever the git entries
        # file changes.
        configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/gitscript.cmake.in"
                       "${CMAKE_CURRENT_BINARY_DIR}/gitscript.cmake"
                       @ONLY)

        # Add a custom target to drive the custom command.
        add_custom_target(kipiplugins-gitversion
                          ALL COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/gitscript.cmake")

        # ==================================================================================================

        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/common/libkipiplugins
                            ${CMAKE_CURRENT_BINARY_DIR}/common/libkipiplugins
                            ${CMAKE_CURRENT_SOURCE_DIR}/common/libkipiplugins/dialogs
                            ${CMAKE_CURRENT_SOURCE_DIR}/common/libkipiplugins/widgets
                            ${CMAKE_CURRENT_SOURCE_DIR}/common/libkipiplugins/tools
                            ${CMAKE_CURRENT_SOURCE_DIR}/common/libkipiplugins/tools/imageio
                            ${KF5Kipi_INCLUDE_DIRS}
                        )

        # To prevent warnings from M$ compiler

        if(WIN32 AND MSVC)
            add_definitions(-D_CRT_SECURE_NO_WARNINGS)
            add_definitions(-D_ATL_SECURE_NO_WARNINGS)
            add_definitions(-D_AFX_SECURE_NO_WARNINGS)
        endif()

        if(NOT WIN32)
            configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/kipi-plugins.lsm.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/kipi-plugins.lsm)
        endif()

        add_subdirectory(common)
        add_subdirectory(printimages)
        add_subdirectory(kmlexport)
        add_subdirectory(sendimages)
        add_subdirectory(expoblending)
        add_subdirectory(flickr)
        add_subdirectory(smug)
        add_subdirectory(imageshack)
        add_subdirectory(piwigo)
        add_subdirectory(remotestorage)
        add_subdirectory(yandexfotki)
        add_subdirectory(rajce)
        add_subdirectory(googleservices)
        add_subdirectory(dropbox)
        add_subdirectory(imgur)
        add_subdirectory(facebook)

        if(KF5Archive_FOUND)
            add_subdirectory(flashexport)
        endif()

        if(BUILD_VIEWERS)
            add_subdirectory(advancedslideshow)
            add_subdirectory(imageviewer)
        endif()

        if(FLEX_FOUND AND BISON_FOUND AND KF5ThreadWeaver_FOUND)
            add_subdirectory(panorama)
        endif()

        if(KF5MediaWiki_FOUND)
            add_subdirectory(mediawiki)
        endif()

        if(KF5CalendarCore_FOUND)
            add_subdirectory(calendar)
        endif()

        if(KF5Vkontakte_FOUND)
            add_subdirectory(vkontakte)
        endif()

        if (BUILD_TESTING)
            add_subdirectory(tests)
        endif()

    endif()

else()

    message(STATUS "")
    message(STATUS "----------------------------------------------------------------------------------")
    message(STATUS " libkipi not found. ${PROJECT_NAME} ${KIPIPLUGINS_VERSION_STRING} will not be compiled")
    message(STATUS "")

endif()
