SET (GENDIR "${CMAKE_SOURCE_DIR}/src/API_generated/" )

INCLUDE_DIRECTORIES(
                    "${CMAKE_SOURCE_DIR}"
                    "${CMAKE_SOURCE_DIR}/src"
                    "${CMAKE_SOURCE_DIR}/src/include"
                    "${CMAKE_BINARY_DIR}/src/include"
                    "${CMAKE_SOURCE_DIR}/src/include_private"
                    "${CMAKE_BINARY_DIR}/src/include_private"
                    "${CMAKE_SOURCE_DIR}/src/liboyranos_core"
                    "${CMAKE_SOURCE_DIR}/src/liboyranos_object"
                    "${CMAKE_SOURCE_DIR}/src/liboyranos_modules"
                    "${CMAKE_SOURCE_DIR}/src/liboyranos_config"
                    "${CMAKE_SOURCE_DIR}/src/liboyranos"
                    "${GENDIR}"
)

# dependencies
INCLUDE(CheckLibraryExists)
INCLUDE(CheckIncludeFile)
CHECK_INCLUDE_FILE(dirent.h HAVE_DIRENT_H)
CHECK_INCLUDE_FILE(langinfo.h HAVE_LANGINFO_H)

CHECK_INCLUDE_FILE(libintl.h HAVE_LIBINTL_H)
FIND_LIBRARY( LIBINTL_LIBRARIES NAMES intl libintl libintl-8 )
IF(LIBINTL_LIBRARIES)
  SET( EXTRA_LIBS ${EXTRA_LIBS} ${LIBINTL_LIBRARIES} )
  SET( EXTRA_LIBS_CORE ${EXTRA_LIBS_CORE} ${LIBINTL_LIBRARIES} )
ELSE(LIBINTL_LIBRARIES)
  MESSAGE( "-- libintl not found" )
ENDIF(LIBINTL_LIBRARIES)

CHECK_LIBRARY_EXISTS( iconv libiconv_open "iconv.h" HAVE_ICONV )
IF( HAVE_ICONV )
  FIND_LIBRARY( LIBICONV_LIBRARIES NAMES libiconv-2 libiconv iconv )
  IF(LIBICONV_LIBRARIES)
    SET( ICONV_LIBS ${LIBICONV_LIBRARIES} )
  ELSE(LIBICONV_LIBRARIES)
    SET( ICONV_LIBS iconv )
  ENDIF(LIBICONV_LIBRARIES)
  SET( EXTRA_LIBS ${EXTRA_LIBS} ${ICONV_LIBS} )
  SET( EXTRA_LIBS_CORE ${EXTRA_LIBS_CORE} ${ICONV_LIBS} )
ELSE( HAVE_ICONV )
  CHECK_LIBRARY_EXISTS( iconv iconv_open "iconv.h" HAVE_ICONV_UNIX )
  IF( HAVE_ICONV_UNIX )
    SET( EXTRA_LIBS ${EXTRA_LIBS} iconv )
    SET( EXTRA_LIBS_CORE ${EXTRA_LIBS_CORE} iconv )
    SET( HAVE_ICONV TRUE )
  ENDIF( HAVE_ICONV_UNIX )
ENDIF( HAVE_ICONV )


CHECK_LIBRARY_EXISTS(m pow "math.h" HAVE_M)
IF(HAVE_M)
  SET( EXTRA_LIBS ${EXTRA_LIBS} m )
  SET( EXTRA_LIBS_CORE ${EXTRA_LIBS_CORE} m )
ENDIF(HAVE_M)

CHECK_LIBRARY_EXISTS(dl dlopen "dlfcn.h" HAVE_DL)
IF(HAVE_DL)
  SET( EXTRA_LIBS ${EXTRA_LIBS} dl )
ELSE(HAVE_DL)
  FIND_PATH(DL_INCLUDE_DIR dlfcn.h)
  IF(DL_INCLUDE_DIR)
    SET( HAVE_DL 1 )
    SET( EXTRA_LIBS ${EXTRA_LIBS} dl )
  ENDIF(DL_INCLUDE_DIR)
ENDIF(HAVE_DL)

FIND_PATH(LTDL_INCLUDE_DIR ltdl.h )
IF(LTDL_INCLUDE_DIR)
  SET( HAVE_LTDL 1 )
  SET( EXTRA_LIBS ${EXTRA_LIBS} ltdl )
ENDIF(LTDL_INCLUDE_DIR)

IF(NOT HAVE_DL AND NOT HAVE_LTDL)
  MESSAGE( SEND_ERROR "need one of libdl with dlfcn.h or libltdl with ltdl.h" )
ENDIF(NOT HAVE_DL AND NOT HAVE_LTDL)

CHECK_LIBRARY_EXISTS(c malloc "stdio.h" HAVE_C)
IF(HAVE_C)
  SET( EXTRA_LIBS ${EXTRA_LIBS} c )
ENDIF(HAVE_C)

FIND_PATH( STDINT_INCLUDE_DIR stdint.h
           HINTS /mingw ${INCLUDE_DIRECTORIES}
           PATH_SUFFIXES include )
IF(STDINT_INCLUDE_DIR)
  SET( HAVE_STDINT_H 1 )
  MESSAGE( "-- stdint.h: " ${STDINT_INCLUDE_DIR} )
ELSE(STDINT_INCLUDE_DIR)
  MESSAGE( "-- stdint.h not found" )
ENDIF(STDINT_INCLUDE_DIR)

FIND_PATH( INTTYPES_INCLUDE_DIR inttypes.h
           HINTS /mingw ${INCLUDE_DIRECTORIES}
           PATH_SUFFIXES include )
IF(INTTYPES_INCLUDE_DIR)
  SET( HAVE_INTTYPES_H 1 )
  MESSAGE( "-- inttypes.h: " ${INTTYPES_INCLUDE_DIR} )
ELSE(INTTYPES_INCLUDE_DIR)
  MESSAGE( "-- inttypes.h not found" )
ENDIF(INTTYPES_INCLUDE_DIR)

FIND_PACKAGE( LCMS )
IF( HAVE_LCMS )
  INCLUDE_DIRECTORIES( ${LCMS_INCLUDE_DIRS} )
  LINK_DIRECTORIES( ${LCMS_LIBRARY_DIRS} )
ENDIF()

FIND_PACKAGE( LCMS2 )
IF( HAVE_LCMS2 )
  INCLUDE_DIRECTORIES( ${LCMS2_INCLUDE_DIRS} )
  LINK_DIRECTORIES( ${LCMS2_LIBRARY_DIRS} )
ENDIF()

IF(NOT LCMS_FOUND AND NOT LCMS2_FOUND )
  MESSAGE( SEND_ERROR "Need at least one little CMS installation. lcms2 is prefered; Download http://www.littlecms.com" )
ENDIF()

FIND_PACKAGE( OpenMP )
IF(OPENMP_FOUND)
  INCLUDE_DIRECTORIES( ${OPENMP_INCLUDE_DIRS} )
  LINK_DIRECTORIES( ${OPENMP_LIBRARY_DIRS} )
  SET( EXTRA_LIBS ${EXTRA_LIBS} ${OPENMP_LIBRARIES} )
  FIND_LIBRARY( LIBGOMP_LIBRARIES NAMES gomp gomp-1 )
  IF( LIBGOMP_LIBRARIES )
    SET( EXTRA_LIBS ${EXTRA_LIBS} ${LIBGOMP_LIBRARIES} )
  ENDIF( LIBGOMP_LIBRARIES )
  SET( HAVE_OPENMP 1 )
  SET( CMAKE_C_FLAGS_CORE "${CMAKE_C_FLAGS}" )
  SET( CMAKE_CXX_FLAGS_CORE "${CMAKE_CXX_FLAGS}" )
  SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}" )
  SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}" )
ENDIF(OPENMP_FOUND)


FIND_PACKAGE( Cups )
IF(HAVE_CUPS)
  INCLUDE_DIRECTORIES( ${CUPS_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${CUPS_LIBRARYS} )
ENDIF(HAVE_CUPS)

FIND_PACKAGE( Sane )
IF(SANE_FOUND)
  INCLUDE_DIRECTORIES( ${SANE_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${SANE_LIBRARYS} )
ENDIF(SANE_FOUND)

FIND_PACKAGE( Cairo )
IF(HAVE_CAIRO)
  INCLUDE_DIRECTORIES( ${CAIRO_INCLUDE_DIR}/cairo )
  INCLUDE_DIRECTORIES( ${CAIRO_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${CAIRO_LIBRARY_DIRS} )
ENDIF(HAVE_CAIRO)

FIND_PACKAGE( Fltk )
IF(FLTK_FOUND)
  SET( HAVE_FLTK 1 )
  SET( HAVE_FLTK ${HAVE_FLTK} PARENT_SCOPE )
  INCLUDE_DIRECTORIES( ${FLTK_INCLUDE_DIRS} )
  LINK_DIRECTORIES( ${FLTK_LIBRARY_DIRS} )
ENDIF(FLTK_FOUND)

FIND_PACKAGE(LibXml2)
IF(LIBXML2_FOUND)
  INCLUDE_DIRECTORIES( ${LIBXML2_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${LIBXML2_LIBRARY_DIRS} )
  SET( HAVE_LIBXML2 1 )
  # we need xml2 for internal elektra
  SET( LIBXML2_LIBRARIES ${LIBXML2_LIBRARIES} PARENT_SCOPE)
ENDIF(LIBXML2_FOUND)

FIND_PACKAGE( LibRaw )
IF( HAVE_LIBRAW )
#  TRY_COMPILE(
#    HAVE_LIBRAW_REALLY
#    ${CMAKE_BINARY_DIR}/src/tests
#    ${CMAKE_SOURCE_DIR}/src/tests/libraw_test.cxx
#    CMAKE_FLAGS "-DCMAKE_CXX_FLAGS=-I${LIBRAW_INCLUDE_DIR} -DCMAKE_EXE_LINKER_FLAGS=${LIBRAW_LIBRARY_DIRS}"
#    COMPILE_DEFINITIONS -shared
#    OUTPUT_VARIABLE TRY_COMPILE_OUTPUT
#    )
  INCLUDE_DIRECTORIES( ${LIBRAW_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${LIBRAW_LIBRARY_DIRS} )
  TRY_COMPILE(
    HAVE_LIBRAW_PLUGIN
    ${CMAKE_BINARY_DIR}/src/tests/libraw_test
    ${CMAKE_SOURCE_DIR}/src/tests/libraw_test
    libraw_test
    CMAKE_FLAGS "-DCMAKE_CXX_FLAGS=-I${LIBRAW_INCLUDE_DIR} -DCMAKE_EXE_LINKER_FLAGS=${LIBRAW_LIBRARY_DIRS}"
    OUTPUT_VARIABLE TRY_COMPILE_OUTPUT
    )
  MESSAGE( "HAVE_LIBRAW_PLUGIN: ${HAVE_LIBRAW_PLUGIN}" )
ENDIF()

FIND_PACKAGE( Exif2 )
IF(HAVE_EXIV2)
  INCLUDE_DIRECTORIES( ${EXIV2_INCLUDE_DIRS} )
  LINK_DIRECTORIES( ${EXIV2_LIBRARY_DIRS} )
ENDIF(HAVE_EXIV2)

FIND_PACKAGE( LibPNG REQUIRED )
  INCLUDE_DIRECTORIES( ${LIBPNG_INCLUDE_DIRS} )
  IF( LIBPNG_INCLUDEDIR )
    SET( LIBPNG_INCLUDE "${LIBPNG_INCLUDEDIR}/png.h" )
  ELSE( LIBPNG_INCLUDEDIR )
    SET( LIBPNG_INCLUDE <png.h> )
  ENDIF( LIBPNG_INCLUDEDIR )
LINK_DIRECTORIES( ${LIBPNG_LIBRARY_DIRS} )

FIND_PACKAGE( JPEG )
IF( JPEG_FOUND )
  INCLUDE_DIRECTORIES( ${JPEG_INCLUDE_DIR} )
  SET( HAVE_JPEG 1 )
ENDIF( JPEG_FOUND )

FIND_PACKAGE( TIFF )
IF( TIFF_FOUND )
  INCLUDE_DIRECTORIES( ${TIFF_INCLUDE_DIR} )
  SET( HAVE_TIFF 1 )
ENDIF( TIFF_FOUND )

INCLUDE(FindThreads)
IF(CMAKE_HAVE_THREADS_LIBRARY)
   SET(THREADS_FOUND TRUE)
ELSE()
   SET(THREADS_FOUND FALSE)
ENDIF(CMAKE_HAVE_THREADS_LIBRARY)

IF(CMAKE_USE_PTHREADS_INIT)
   SET(HAVE_PTHREAD 1)
   SET(PTHREADS_FOUND TRUE)
   SET(THREAD_LIBRARY -lpthread)
ELSE()
   SET(HAVE_PTHREAD 0)
   SET(PTHREADS_FOUND FALSE)
ENDIF(CMAKE_USE_PTHREADS_INIT)


FIND_PACKAGE( GetText )
MESSAGE( "GetText: ${GETTEXT_FOUND}" )
IF( GETTEXT_FOUND )
  SET( USE_GETTEXT TRUE )
  SET( USE_GETTEXT ${USE_GETTEXT} PARENT_SCOPE )

  SET( LINGUAS ${CMAKE_SOURCE_DIR}/po/cs.po
               ${CMAKE_SOURCE_DIR}/po/de.po
               ${CMAKE_SOURCE_DIR}/po/eo.po
               ${CMAKE_SOURCE_DIR}/po/eu.po
               ${CMAKE_SOURCE_DIR}/po/fr.po
               ${CMAKE_SOURCE_DIR}/po/ru.po
     )
  FOREACH( oy_lang ${LINGUAS} )
    GET_FILENAME_COMPONENT(_absFile ${oy_lang} ABSOLUTE)
    GET_FILENAME_COMPONENT(_abs_PATH ${_absFile} PATH)
    GET_FILENAME_COMPONENT(_lang ${_absFile} NAME_WE)
    SET( OY_LINGUAS ${OY_LINGUAS} ${_lang} )
    SET( OY_LINGUAS_GMO_FILES ${OY_LINGUAS_GMO_FILES} " ${CMAKE_BINARY_DIR}/src/${_lang}.gmo" )
  ENDFOREACH( oy_lang )
  SET( OY_LINGUAS ${OY_LINGUAS} PARENT_SCOPE )
  SET( OY_LINGUAS_PO_FILES ${LINGUAS} PARENT_SCOPE )
  SET( OY_LINGUAS_GMO_FILES ${OY_LINGUAS_GMO_FILES} PARENT_SCOPE )
ENDIF()

# debug
GET_PROPERTY(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
#MESSAGE( "-- INCLUDE_DIRECTORIES: ${inc_dirs}" )

SET( FRAMEWORK_DIR "Library/Frameworks" )
SET( FRAMEWORK_DIR ${FRAMEWORK_DIR} PARENT_SCOPE )
IF(APPLE)
  SET( LIB_DIR ${FRAMEWORK_DIR} )
  SET( GCC_LINK_PATH_FLAG "-F " PARENT_SCOPE )
  SET( GCC_LINK_LIB_FLAG "-framework " PARENT_SCOPE )
  # each framework header directory is named here
  # otherwise user would need to include the framework name before the header name,
  # which would not be consistent with other platforms
  SET( OYRANOS_INCLUDE_FLAGS "-I${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_DIR}/Oyranos.framework/Headers -I${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_DIR}/OyranosConfig.framework/Headers -I${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_DIR}/OyranosModules.framework/Headers -I${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_DIR}/OyranosObject.framework/Headers -I${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_DIR}/OyranosCore.framework/Headers" PARENT_SCOPE )
ELSE(APPLE)
  SET( LIB_DIR "lib${LIB_SUFFIX}" )
  SET( GCC_LINK_PATH_FLAG "-L" PARENT_SCOPE )
  SET( GCC_LINK_LIB_FLAG "-l" PARENT_SCOPE )
  SET( OYRANOS_INCLUDE_FLAGS "-I${CMAKE_INSTALL_PREFIX}/include/${PACKAGE_NAME}" PARENT_SCOPE )
ENDIF(APPLE)
SET( LIB_DIR ${LIB_DIR} PARENT_SCOPE )

ADD_SUBDIRECTORY( include )

ADD_SUBDIRECTORY( include_private )

ADD_SUBDIRECTORY( liboyranos_core )

ADD_SUBDIRECTORY( liboyranos_object )

ADD_SUBDIRECTORY( liboyranos_modules )

ADD_SUBDIRECTORY( liboyranos_config )

ADD_SUBDIRECTORY( liboyranos )

ADD_SUBDIRECTORY( modules )

ADD_SUBDIRECTORY( tests )

ADD_SUBDIRECTORY( tools )

ADD_SUBDIRECTORY( examples )


# versions for libraries
SET_TARGET_PROPERTIES( ${PROJECT_NAME}Core ${PROJECT_NAME}Object ${PROJECT_NAME}Modules ${PROJECT_NAME}Config ${PROJECT_NAME}
    PROPERTIES VERSION   ${OYRANOS_VERSION_MAJOR}.${OYRANOS_VERSION_MINOR}.${OYRANOS_VERSION_MICRO}
               SOVERSION ${OYRANOS_VERSION_MAJOR}
                     )

SET( EXTRA_LIBS_CORE ${EXTRA_LIBS_CORE} PARENT_SCOPE)
SET( EXTRA_LIBS_CONFIG ${EXTRA_LIBS_CONFIG} PARENT_SCOPE)
SET( EXTRA_LIBS ${EXTRA_LIBS} PARENT_SCOPE)
SET( OY_MODULE_LIBRARIES ${OY_MODULE_LIBRARIES} PARENT_SCOPE )


SET( DOC_SOURCES
	doxymentation_common
        ${CMAKE_SOURCE_DIR}/src/include/*.h
        ${CMAKE_BINARY_DIR}/src/include/*.h
        oyranos_monitor_hooks.h
	${CFILES_CORE_PUBLIC}
	${CHEADERS_CORE_PUBLIC}
	${CFILES_OBJECT_PUBLIC}
	${CHEADERS_OBJECT_PUBLIC}
	${CFILES_OBJECT_MODULES_PUBLIC}
	${CHEADERS_OBJECT_MODULES_PUBLIC}
	${CFILES_OBJECT_CONFIG_PUBLIC}
	${CFILES_OBJECT_CONFIG_GEN_PUBLIC}
	${CHEADERS_OBJECT_CONFIG_PUBLIC}
	${CFILES_OBJECT_IMAGING_PUBLIC}
	${CHEADERS_OBJECT_IMAGING_PUBLIC}
        PARENT_SCOPE
   )


# i18n
IF( GETTEXT_FOUND )
  SET(_potFile ${CMAKE_SOURCE_DIR}/po/${PACKAGE_NAME}.pot)
  #SET( _xgettext_option_list --language=C --keyword=_ --keyword=N_ --keyword=C_:1c,2 --keyword=NC_:1c,2 -s --package-name=${PROJECT_NAME} --package-version=${OYRANOS_VERSION_MAJOR}.${OYRANOS_VERSION_MINOR} )
  SET( _xgettext_option_list --add-comments --keyword=gettext --flag=gettext:1:pass-c-format --keyword=_ --flag=_:1:pass-c-format --keyword=N_ --flag=N_:1:pass-c-format  --copyright-holder='Kai-Uwe Behrmann'  --msgid-bugs-address='ku.b@gmx.de' --from-code=utf-8 --package-name=${PROJECT_NAME} --package-version=${OYRANOS_VERSION_MAJOR}.${OYRANOS_VERSION_MINOR} )
  SET( XGETTEXT_CMD xgettext )
  FILE( GLOB SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
        include/*.h
        API_generated/*.c API_generated/*.h
        liboyranos/*.c
        liboyranos_core/*.c
        liboyranos_object/*.c
        liboyranos_modules/*.c
        liboyranos_config/*.c
        modules/oyranos/*.c
        modules/color/modules/*.c
        modules/color/modules/devices/*.c
        modules/color/modules/*.cpp
        modules/color/modules/devices/*.cpp
        tools/*.c tools/qcmsevents/*.cpp tools/qcmsevents/*.cxx
        examples/*.c examples/devices/*.c examples/libraw/*.cpp
        examples/image_display/*.c* examples/image_display/*.h
      )

  ADD_CUSTOM_COMMAND(OUTPUT ${_potFile}
    COMMAND ${XGETTEXT_CMD} ${_xgettext_option_list} -o ${_potFile} ${SRC_FILES}
    DEPENDS ${SRC_FILES}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Extract translatable messages to ${_potFile}"
  )

  # compile translations always to have them ready for installation
  GETTEXT_CREATE_TRANSLATIONS( ${_potFile} ALL ${LINGUAS} )
ENDIF()


# code generation
ADD_CUSTOM_COMMAND( OUTPUT generator/oyAPIGenerator
                COMMAND cd generator/ && CMAKE_PREFIX_PATH="${CMAKE_CURRENT_BINARY_DIR}/generator/grantlee/lib/cmake/grantlee/" cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-g -Wall ${CMAKE_CURRENT_SOURCE_DIR}/generator && make || echo "Did you run ${CMAKE_CURRENT_SOURCE_DIR}/generator/download_grantlee.sh in ${CMAKE_CURRENT_BINARY_DIR}/generator/ ?"
		)

ADD_CUSTOM_TARGET( generate
		COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/generator/ && ./oyAPIGenerator ${CMAKE_CURRENT_SOURCE_DIR}/templates:${CMAKE_CURRENT_SOURCE_DIR}/sources:${CMAKE_CURRENT_SOURCE_DIR}/API_generated
		COMMENT "Generate source code from templates"
		VERBATIM
                DEPENDS generator/oyAPIGenerator
		WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generator" )

