#***************************************************************************
#*   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
#*                                                                         *
#*   This program is free software; you can redistribute it and/or modify  *
#*   it under the terms of the GNU General Public License as published by  *
#*   the Free Software Foundation; either version 2 of the License, or     *
#*   (at your option) any later version.                                   *
#*                                                                         *
#*   This program 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 General Public License for more details.                          *
#*                                                                         *
#*   You should have received a copy of the GNU General Public License     *
#*   along with this program.  If not, see <http://www.gnu.org/licenses/>  *
#***************************************************************************
MESSAGE( STATUS "..:: CMAKE ROOT ::..")

PROJECT(skrooge)

cmake_minimum_required(VERSION 2.8)
IF(POLICY CMP0017)
   CMAKE_POLICY(SET CMP0017 OLD) # or even better, NEW
ENDIF(POLICY CMP0017)
IF(POLICY CMP0002)
   CMAKE_POLICY(SET CMP0002 OLD) # to authorize the build with po files   
ENDIF(POLICY CMP0002)

FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE(KDE4Defaults)
INCLUDE(MacroLibrary)
MESSAGE(STATUS "     KDE4 support enabled : ${KDE4_DATA_DIR}")
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})

FIND_PACKAGE(Grantlee)
if( NOT Grantlee_FOUND  )
  message(FATAL_ERROR "Package Grantlee required, but not found!")
else( NOT Grantlee_FOUND )
  message(STATUS "Package Grantlee found!")
endif( NOT Grantlee_FOUND )

SET(GRANTLEE_VERSION ${Grantlee_VERSION_MAJOR}.${Grantlee_VERSION_MINOR}.${Grantlee_VERSION_PATCH})
MESSAGE( STATUS "     GRANTLEE VERSION      : " ${GRANTLEE_VERSION})

if(${GRANTLEE_VERSION} VERSION_LESS "0.1.7")
    MESSAGE( STATUS "     enable GRANTLEE 0.1.4 API")
else(${GRANTLEE_VERSION} VERSION_LESS "0.1.7")
    MESSAGE( STATUS "     enable GRANTLEE 0.1.7 API")
    add_definitions(-DSKGGRANTLEE17ENABLED=Y)
endif(${GRANTLEE_VERSION} VERSION_LESS "0.1.7")

SET(SKG_VERSION "1.7.2")
SET(SKG_BETA "BETA")
SET(SOVERSION 1)
SET( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${skrooge_SOURCE_DIR}/cmake/modules )

IF (CMAKE_BUILD_TYPE)
ELSE (CMAKE_BUILD_TYPE)
	SET(CMAKE_BUILD_TYPE profile)
#"release": optimized for speed, no debug symbols or debug messages
#"relwithdebinfo": optimized for speed, debug symbols for backtraces
#"debug": optimized, but debuggable
#"debugfull": no optimizations, full debug support
#"profile": adds coverage flags to debugfull
#"none": the build flags are manually set using the CMAKE_CXX_FLAGS option.
ENDIF (CMAKE_BUILD_TYPE)

FIND_PROGRAM(SKG_BASH bash)
MARK_AS_ADVANCED(SKG_BASH)

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
OPTION(SKG_BUILD_TEST "Build the test" OFF)
MESSAGE( STATUS "     PROJECT_SOURCE_DIR  : " ${PROJECT_SOURCE_DIR} )
MESSAGE( STATUS "     PROJECT_BINARY_DIR  : " ${PROJECT_BINARY_DIR} )
MESSAGE( STATUS "     BUILD_DIR           : " ${BUILD_DIR} )
MESSAGE( STATUS "     CMAKE_BUILD_TYPE    : " ${CMAKE_BUILD_TYPE} )
MESSAGE( STATUS "     CMAKE_INSTALL_PREFIX: " ${CMAKE_INSTALL_PREFIX} )
MESSAGE( STATUS "     SKG_BUILD_TEST      : " ${SKG_BUILD_TEST} )

IF(CMAKE_BUILD_TYPE MATCHES "profile")
  MESSAGE( STATUS "     TRACES              : All traces activated")
  #add_definitions(-DSKGFULLTRACES)
ELSE(CMAKE_BUILD_TYPE MATCHES "profile")
  IF(CMAKE_BUILD_TYPE MATCHES "debug" OR SKG_BETA MATCHES "BETA")
    MESSAGE( STATUS "     TRACES              : Some traces activated")
  ELSE(CMAKE_BUILD_TYPE MATCHES "debug" OR SKG_BETA MATCHES "BETA")
    MESSAGE( STATUS "     TRACES              : All traces disabled")
    add_definitions(-DSKGNOTRACES)
  ENDIF(CMAKE_BUILD_TYPE MATCHES "debug" OR SKG_BETA MATCHES "BETA")
ENDIF(CMAKE_BUILD_TYPE MATCHES "profile")

add_definitions(-DSKGVERSION=${SKG_VERSION}${SKG_BETA})

include_directories( ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES}
	${CMAKE_CURRENT_SOURCE_DIR}/skgbasemodeler/
	${CMAKE_CURRENT_SOURCE_DIR}/skgbasegui/
	${CMAKE_CURRENT_BINARY_DIR}/skgbasegui/
	${CMAKE_CURRENT_SOURCE_DIR}/skgbasemodelertest
	${CMAKE_CURRENT_SOURCE_DIR}/skgbankmodeler/
	${CMAKE_CURRENT_SOURCE_DIR}/skgbankgui
	${CMAKE_CURRENT_BINARY_DIR}/skgbankgui)

#Correction bug 207249 vvvv
#SET(CMAKE_CXX_FLAGS "-Wall" )
IF(CMAKE_COMPILER_IS_GNUCXX)
  SET(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
#Correction bug 207249 ^^^^

#Default installation path
set(CMAKE_INSTALL_PREFIX ${BUILD_DIR})

# Win32 specific configuration
IF(WIN32)
    ADD_DEFINITIONS(-DSKG_PLATFORM_WIN32)
    IF(CMAKE_COMPILER_IS_GNUCXX)
	SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-runtime-pseudo-reloc -Wl,--export-all-symbols" CACHE STRING "" FORCE)
    ENDIF(CMAKE_COMPILER_IS_GNUCXX)
    
    IF (CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
        ADD_DEFINITIONS( -DQT_DEBUG )
    ENDIF (CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)    
ELSE(WIN32)
#To solve Bug 209912:"ldd -u <library>""
#does not work on MACOS, must be done manually		SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed" CACHE STRING "" FORCE)
ENDIF(WIN32)
MESSAGE(STATUS "     CMAKE_SHARED_LINKER_FLAGS : ${CMAKE_SHARED_LINKER_FLAGS}")

ADD_SUBDIRECTORY(skg_dataengine) #<-- Experimental

#ADD_SUBDIRECTORY
ADD_SUBDIRECTORY(skgbasemodeler)
ADD_SUBDIRECTORY(skgbankmodeler)
ADD_SUBDIRECTORY(skgbasegui)
ADD_SUBDIRECTORY(skgbankgui)

ADD_SUBDIRECTORY(plugins)
ADD_SUBDIRECTORY(images)

IF(SKG_BUILD_TEST AND NOT WIN32)
	ADD_SUBDIRECTORY(tests)
ENDIF(SKG_BUILD_TEST AND NOT WIN32)

#Main application
ADD_SUBDIRECTORY(skrooge)
ADD_SUBDIRECTORY(skroogeconvert)
ADD_SUBDIRECTORY(skroogeakonadi)

CONFIGURE_FILE(scripts/addNewPlugin.sh.in scripts/addNewPlugin.sh @ONLY)
CONFIGURE_FILE(scripts/addNewImportPlugin.sh.in scripts/addNewImportPlugin.sh @ONLY)

#DOXYGEN
ADD_CUSTOM_TARGET(apidox "doxygen" "${PROJECT_SOURCE_DIR}/Doxyfile")

#ASTYLE
CONFIGURE_FILE(scripts/astyle.sh.in scripts/astyle.sh @ONLY)
ADD_CUSTOM_TARGET(astyle "scripts/astyle.sh")

#KRAZY
CONFIGURE_FILE(scripts/krazy.sh.in scripts/krazy.sh @ONLY)
ADD_CUSTOM_TARGET(krazy "scripts/krazy.sh")

#GENERATE ICONS
CONFIGURE_FILE(scripts/generateIcons.sh.in scripts/generateIcons.sh @ONLY)

#GENERATE SPLASH SCREEN
CONFIGURE_FILE(scripts/generateSplash.sh.in scripts/generateSplash.sh @ONLY)
ADD_CUSTOM_TARGET(splash "scripts/generateSplash.sh")

#LCOV
CONFIGURE_FILE(scripts/removeTmpFile.sh.in scripts/removeTmpFile.sh @ONLY)
CONFIGURE_FILE(scripts/lcov.sh.in scripts/lcov.sh @ONLY)
ADD_CUSTOM_TARGET(lcov "scripts/lcov.sh")
ADD_CUSTOM_TARGET(lcovclean "scripts/lcov.sh" "-b")

CONFIGURE_FILE(CTestCustom.cmake CTestCustom.cmake @ONLY)

#FULL
ADD_CUSTOM_TARGET(full "scripts/full.sh")


include(MacroOptionalAddSubdirectory)
macro_optional_add_subdirectory( po )

include(MacroOptionalAddSubdirectory)
macro_optional_add_subdirectory( doc )
