if (WIN32)
  cmake_minimum_required(VERSION 2.8.4)
  cmake_policy(SET CMP0015 OLD)
else (WIN32)
  cmake_minimum_required(VERSION 2.6)
endif (WIN32)

# the default target is 'Debug'
if (CMAKE_BUILD_TYPE STREQUAL "")
  set (CMAKE_BUILD_TYPE Debug CACHE STRING "One of: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif ()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

set (CACHE_NAME_SUFFIX "" CACHE STRING "RawTherapee's cache folder suffix (leave empty to use the default suffix, i.e. latesttag)")

# By default, we don't use specific processor target, so  PROC_TARGET_NUMBER is set to 0. If can specify other values to select specific
# processor targets, which list can be found in ProcessorTargets.cmake.
set (PROC_TARGET_NUMBER 0 CACHE STRING "Selected target processor from the list above (taken from ProcessorTargets.cmake)")

# The following line set special compilation flags for RTEngine, and will be added to CMAKE_CXX_FLAGS
# It were moved away from rtengine/CMakefiles.txt, because some users may want to remove -ffast_math :
# this flag speeds up the floating-point operations, but with a little bite less precisions. This default value
# gives the same result/behaviour as before.
set (RTENGINE_CXX_FLAGS "-ffast-math -funroll-loops" CACHE STRING "Special compilation flags for RTEngine")

#loading the processor targets list
include (ProcessorTargets.cmake)
set (PROC_LABEL "undefined" CACHE STRING "Target processor label, unused if PROC_TARGET_NUMBER = 0 or 2")
set (PROC_FLAGS "" CACHE STRING "Target processor related build/link flags")
if ((NOT(PROC_TARGET_NUMBER NOT EQUAL 0)) AND (NOT(PROC_TARGET_NUMBER EQUAL 2)))
	set (PROC_LABEL ${PROC_TARGET_${PROC_TARGET_NUMBER}_LABEL})
	set (PROC_FLAGS ${PROC_TARGET_${PROC_TARGET_NUMBER}_FLAGS})
endif ()

# if it exists, the PROC_FORCED_LABEL value is loaded in PROC_LABEL to override the one of ProcessorTargets
if (DEFINED PROC_FORCED_LABEL)
  set (PROC_LABEL ${PROC_FORCED_LABEL})
endif (DEFINED PROC_FORCED_LABEL)

# adding the proc flags to the build flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PROC_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PROC_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PROC_FLAGS}")

if (APPLE)
#    SET (CMAKE_OSX_ARCHITECTURES "i386;x86_64;" )
#    SET (CMAKE_TRY_COMPILE_OSX_ARCHITECTURES "i386;x86_64;" )
    SET (CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX10.5.sdk")
    SET (CMAKE_OSX_DEPLOYMENT_TARGET "10.5")
endif (APPLE)

option (AUTOMATED_BUILD_SYSTEM "TRUE if built by an automate" OFF)

option (BUILD_SHARED "Build rawtherapee with shared libraries" OFF)
option (WITH_RAWZOR "Build with Rawzor support" OFF)
option (WITH_MYFILE_MMAP "Build using memory mapped file" ON)
option (OPTION_OMP "Build with OpenMP support" ON)

# set install directories
if (NOT DEFINED DATADIR)
    if (WIN32 OR APPLE)
        set (DATADIR ${CMAKE_CURRENT_SOURCE_DIR}/release)
    else (WIN32 OR APPLE)
        set (DATADIR ${CMAKE_INSTALL_PREFIX}/share/rawtherapee)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED DATADIR)

if (NOT DEFINED BINDIR)
    if (WIN32 OR APPLE)
        set (BINDIR  ${CMAKE_CURRENT_SOURCE_DIR}/release)
    else (WIN32 OR APPLE)
        set (BINDIR  ${CMAKE_INSTALL_PREFIX}/bin)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED BINDIR)

if (NOT DEFINED LIBDIR)
    if (WIN32 OR APPLE)
        set (LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/release)
    else (WIN32 OR APPLE)
        # Respect CMAKE_INSTALL_LIBDIR if set
        if (DEFINED CMAKE_INSTALL_LIBDIR)
            set (LIBDIR ${CMAKE_INSTALL_LIBDIR})
        else (DEFINED CMAKE_INSTALL_LIBDIR)
            set (LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
        endif (DEFINED CMAKE_INSTALL_LIBDIR)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED LIBDIR)

if (NOT DEFINED DOCDIR)
    if (WIN32 OR APPLE)
        set (DOCDIR ${CMAKE_CURRENT_SOURCE_DIR}/release/doc)
    else (WIN32 OR APPLE)
        set (DOCDIR ${CMAKE_INSTALL_PREFIX}/share/doc)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED DOCDIR)

if (NOT DEFINED CREDITSDIR)
    if (WIN32 OR APPLE)
        set (CREDITSDIR ${CMAKE_CURRENT_SOURCE_DIR}/release)
    else (WIN32 OR APPLE)
        set (CREDITSDIR ${CMAKE_INSTALL_PREFIX}/share/doc)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED CREDITSDIR)

if (NOT DEFINED LICENCEDIR)
    if (WIN32 OR APPLE)
        set (LICENCEDIR ${CMAKE_CURRENT_SOURCE_DIR}/release)
    else (WIN32 OR APPLE)
        set (LICENCEDIR ${CMAKE_INSTALL_PREFIX}/share/doc)
    endif (WIN32 OR APPLE)
endif (NOT DEFINED LICENCEDIR)

if (NOT DEFINED DESKTOPDIR)
    if (UNIX)
        set (DESKTOPDIR ${CMAKE_INSTALL_PREFIX}/share/applications)
    endif (UNIX)
endif (NOT DEFINED DESKTOPDIR)

if (NOT DEFINED ICONSDIR)
    if (UNIX)
        set (ICONSDIR ${CMAKE_INSTALL_PREFIX}/share/icons)
    endif (UNIX)
endif (NOT DEFINED ICONSDIR)

# check for libraries
find_package(PkgConfig)
pkg_check_modules (GTK     REQUIRED gtk+-2.0>=2.12)
pkg_check_modules (GLIB2   REQUIRED glib-2.0>=2.16)
pkg_check_modules (GLIBMM  REQUIRED glibmm-2.4>=2.16)
pkg_check_modules (GTKMM   REQUIRED gtkmm-2.4>=2.12)
pkg_check_modules (GIO     REQUIRED gio-2.0>=2.16)
pkg_check_modules (GIOMM   REQUIRED giomm-2.4>=2.12)
pkg_check_modules (GTHREAD REQUIRED gthread-2.0>=2.16)
pkg_check_modules (GOBJECT REQUIRED gobject-2.0>=2.16)
pkg_check_modules (SIGC    REQUIRED sigc++-2.0)
# NOTE: dependencies should be handled by pkg_check_modules and FIND_PACKAGE
# on windows too but I don't want to break current build chain
if (WIN32)
    set (EXTRA_LIBDIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")
    set (EXTRA_INCDIR "${CMAKE_CURRENT_SOURCE_DIR}/winclude")
    set (EXTRA_LIB "-lws2_32")
    set (IPTCDATA_LIBRARIES iptcdata)
    set (LCMS_LIBRARIES liblcms.a)
    set (JPEG_LIBRARIES libjpeg.a)
    set (PNG_LIBRARIES libpng.a)
    set (TIFF_LIBRARIES libtiff.a)
    set (ZLIB_LIBRARIES libz.a)

    add_definitions (-DWIN32)
    add_definitions (-D_WIN32)
    if (MINGW)
        add_definitions (-D__MINGW32__)
    endif (MINGW)
else (WIN32)
    pkg_check_modules (IPTCDATA REQUIRED libiptcdata)
    pkg_check_modules (LCMS REQUIRED lcms<=1.99)
    find_package (JPEG REQUIRED)
    find_package (PNG REQUIRED)
    find_package (TIFF REQUIRED)
    find_package (ZLIB REQUIRED)
endif (WIN32)

if (AUTOMATED_BUILD_SYSTEM)

	message(WARNING "Automated build system mode! You have to make sure that AboutThisBuild.txt and rtgui/version.h are up-to-date by building RT at least one time with AUTOMATED_BUILD_SYSTEM=OFF.")

  # Checking for the presence of AboutThisBuild.txt
  find_file(ABOUT_FILE AboutThisBuild.txt PATHS "${CMAKE_CURRENT_SOURCE_DIR}" NO_DEFAULT_PATH)
  if (ABOUT_FILE STREQUAL ABOUT_FILE-NOTFOUND)
	  message(FATAL_ERROR "AboutThisBuild.txt not found! You have to build RT at least one time with AUTOMATED_BUILD_SYSTEM=OFF to generate this file.")
  endif (ABOUT_FILE STREQUAL ABOUT_FILE-NOTFOUND)

  # Checking for the presence of version.h
  find_file(VERSION_FILE version.h PATHS "${CMAKE_CURRENT_SOURCE_DIR}/rtgui" NO_DEFAULT_PATH)
  if (VERSION_FILE STREQUAL VERSION_FILE-NOTFOUND)
	  message(FATAL_ERROR "rtgui/version.h not found! You have to build RT at least one time with AUTOMATED_BUILD_SYSTEM=OFF to generate this file.")
  endif (VERSION_FILE STREQUAL VERSION_FILE-NOTFOUND)

else (AUTOMATED_BUILD_SYSTEM)

  # set the bit number information of the platform
  if (CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(PROC_BIT_DEPTH 32 bits)
  elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(PROC_BIT_DEPTH 64 bits)
  endif (CMAKE_SIZEOF_VOID_P EQUAL 4)

  string (TOUPPER ${CMAKE_BUILD_TYPE} UPPER_CMAKE_BUILD_TYPE)

  #generating AboutThisBuild.txt
  if (WIN32)

    add_custom_target (AboutFile ALL
      COMMAND ${CMAKE_COMMAND}
              -DSRC_DIR:STRING=${CMAKE_CURRENT_SOURCE_DIR}
              -DCACHE_NAME_SUFFIX:STRING=${CACHE_NAME_SUFFIX}
              -DSYSTEM:STRING=Windows
              -DPROC_LABEL:STRING=\"${PROC_LABEL}\"
              -DPROC_BIT_DEPTH:STRING=\"${PROC_BIT_DEPTH}\"
              -DBUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
              -DGTKMM_VERSION:STRING=${GTKMM_VERSION}
              -DCXX_FLAGS:STRING=\"${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}\"
              -DLFLAGS:STRING=\"${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}\"
              -DOPTION_OMP:STRING=${OPTION_OMP}
              -DWITH_MYFILE_MMAP:STRING=${WITH_MYFILE_MMAP}
              -DWITH_RAWZOR:STRING=${WITH_RAWZOR}
              -P ${CMAKE_CURRENT_SOURCE_DIR}/About-Windows.cmake
      COMMENT "Creating the about file"
    )
    find_file(HG_CMD hg.exe HINTS ENV Path PATH_SUFFIXES ../)

  elseif (APPLE)
 
    add_custom_target (AboutFile ALL
      COMMAND ${CMAKE_COMMAND}
              -DSRC_DIR:STRING=${CMAKE_CURRENT_SOURCE_DIR}
              -DCACHE_NAME_SUFFIX:STRING=${CACHE_NAME_SUFFIX}
              -DSYSTEM:STRING=Apple
              -DPROC_LABEL:STRING=\"${PROC_LABEL}\"
              -DPROC_BIT_DEPTH:STRING=\"${PROC_BIT_DEPTH}\"
              -DBUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
              -DGTKMM_VERSION:STRING=${GTKMM_VERSION}
              -DCXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}
              -DLFLAGS:STRING=${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}
              -DOPTION_OMP:STRING=${OPTION_OMP}
              -DWITH_MYFILE_MMAP:STRING=${WITH_MYFILE_MMAP}
              -DWITH_RAWZOR:STRING=${WITH_RAWZOR}
              -P ${CMAKE_CURRENT_SOURCE_DIR}/About-Apple.cmake
      COMMENT "Creating the about file"
    )
    find_file(HG_CMD hg PATHS "/opt/local/bin" "/usr/local/bin" "/usr/bin")
    find_file(HG_CMD hg)

  else (WIN32)
 
    add_custom_target (AboutFile ALL
      COMMAND ${CMAKE_COMMAND}
              -DSRC_DIR:STRING=${CMAKE_CURRENT_SOURCE_DIR}
              -DCACHE_NAME_SUFFIX:STRING=${CACHE_NAME_SUFFIX}
              -DSYSTEM:STRING=Linux
              -DPROC_LABEL:STRING=\"${PROC_LABEL}\"
              -DPROC_BIT_DEPTH:STRING=\"${PROC_BIT_DEPTH}\"
              -DBUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
              -DGTKMM_VERSION:STRING=${GTKMM_VERSION}
              -DCXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}
              -DLFLAGS:STRING=${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${UPPER_CMAKE_BUILD_TYPE}}
              -DOPTION_OMP:STRING=${OPTION_OMP}
              -DWITH_MYFILE_MMAP:STRING=${WITH_MYFILE_MMAP}
              -DWITH_RAWZOR:STRING=${WITH_RAWZOR}
              -P ${CMAKE_CURRENT_SOURCE_DIR}/About-Linux.cmake
      COMMENT "Creating the about file"
    )
    find_file(HG_CMD hg)

  endif (WIN32)
 
  add_dependencies(AboutFile Debug Release MinSizeRel RelWithDebInfo)

  if (HG_CMD STREQUAL HG_CMD-NOTFOUND)
    message(FATAL_ERROR "hg command not found!")
  else (HG_CMD STREQUAL HG_CMD-NOTFOUND)
    message(STATUS "hg command found: ${HG_CMD}")
  endif (HG_CMD STREQUAL HG_CMD-NOTFOUND)

endif (AUTOMATED_BUILD_SYSTEM)

# link rawzor
if (WITH_RAWZOR)
    if (WIN32)
        set (EXTRA_INCDIR ${EXTRA_INCDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_win")
        set (EXTRA_LIBDIR ${EXTRA_LIBDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_win")
        set (EXTRA_LIB ${EXTRA_LIB} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_win/rwz_sdk_s.a")
        add_definitions (-DRAWZOR_SUPPORT)
        install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_win/rwz_sdk_s.dll DESTINATION ${BINDIR}
            PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ)
    elseif (APPLE)
        set (EXTRA_INCDIR "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_mac")
        set (EXTRA_LIBDIR "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_mac")
        set (EXTRA_LIB "-lrwz_sdk_64")
        add_definitions (-DRAWZOR_SUPPORT)
        install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_mac/librwz_sdk_64.dylib DESTINATION ${BINDIR}
            PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk_64.dylib)
        if (BUILD_SHARED)
            install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_mac/librwz_sdk_64.dylib DESTINATION ${LIBDIR}
                PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk_64.dylib)
        endif (BUILD_SHARED)
    else (WIN32)
        if (CMAKE_SIZEOF_VOID_P EQUAL 4)
            set (EXTRA_INCDIR ${EXTRA_INCDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin32")
            set (EXTRA_LIBDIR ${EXTRA_LIBDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin32")
            set (EXTRA_LIB ${EXTRA_LIB} "-lrwz_sdk")
            add_definitions (-DRAWZOR_SUPPORT)
            install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin32/librwz_sdk.so DESTINATION ${BINDIR}
                PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk.so)
            if (BUILD_SHARED)
                install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin32/librwz_sdk.so DESTINATION ${LIBDIR}
                    PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk.so)
            endif (BUILD_SHARED)
        elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
            set (EXTRA_INCDIR ${EXTRA_INCDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin64")
            set (EXTRA_LIBDIR ${EXTRA_LIBDIR} "${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin64")
            set (EXTRA_LIB ${EXTRA_LIB} "-lrwz_sdk")
            add_definitions (-DRAWZOR_SUPPORT)
            install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin64/librwz_sdk.so DESTINATION ${BINDIR}
                PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk.so)
            if (BUILD_SHARED)
                install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/rawzor_lin64/librwz_sdk.so DESTINATION ${LIBDIR}
                    PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME rwz_sdk.so)
            endif (BUILD_SHARED)
        endif (CMAKE_SIZEOF_VOID_P EQUAL 4)
    endif (WIN32)
endif (WITH_RAWZOR)

if (WITH_MYFILE_MMAP)
	add_definitions (-DMYFILE_MMAP)
endif (WITH_MYFILE_MMAP)

if (OPTION_OMP)
    find_package(OpenMP)
    if (OPENMP_FOUND)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    endif (OPENMP_FOUND)
endif (OPTION_OMP)

if (UNIX)
    install (PROGRAMS rtstart DESTINATION ${BINDIR})
endif (UNIX)

install (FILES AUTHORS.txt DESTINATION ${CREDITSDIR})
install (FILES LICENSE.txt DESTINATION ${LICENCEDIR})
install (FILES AboutThisBuild.txt DESTINATION ${CREDITSDIR})

add_subdirectory (rtexif)
add_subdirectory (rtengine)
add_subdirectory (rtgui)
add_subdirectory (rtdata)
