# - Try to find the Kdcraw library
#
# If you have put a local version of libkdcraw into your source tree,
# set KDCRAW_LOCAL_DIR to the relative path to the local directory.
#
# Once done this will define
#
#  KDCRAW_FOUND - system has libkdcraw
#  KDCRAW_INCLUDE_DIR - the libkdcraw include directory
#  KDCRAW_LIBRARIES - Link these to use libkdcraw
#  KDCRAW_DEFINITIONS - Compiler switches required for using libkdcraw
#

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


if (KDCRAW_INCLUDE_DIR AND KDCRAW_LIBRARIES AND KDCRAW_DEFINITIONS)

  message(STATUS "Found Kdcraw library in cache: ${KDCRAW_LIBRARIES}")

  # in cache already
  set(KDCRAW_FOUND TRUE)

else (KDCRAW_INCLUDE_DIR AND KDCRAW_LIBRARIES AND KDCRAW_DEFINITIONS)

  message(STATUS "Check Kdcraw library in local sub-folder...")

  # Check if library is not in local sub-folder

  if (KDCRAW_LOCAL_DIR)
    set (KDCRAW_LOCAL_FOUND TRUE)
  else (KDCRAW_LOCAL_DIR)
    find_file(KDCRAW_LOCAL_FOUND libkdcraw/kdcraw.h ${CMAKE_SOURCE_DIR}/libkdcraw ${CMAKE_SOURCE_DIR}/libs/libkdcraw NO_DEFAULT_PATH)

    if (KDCRAW_LOCAL_FOUND)
      # Was it found in libkdcraw/ or in libs/libkdcraw?
      find_file(KDCRAW_LOCAL_FOUND_IN_LIBS libkdcraw/kdcraw.h ${CMAKE_SOURCE_DIR}/libs/libkdcraw NO_DEFAULT_PATH)
      if (KDCRAW_LOCAL_FOUND_IN_LIBS)
        set(KDCRAW_LOCAL_DIR libs/libkdcraw)
      else (KDCRAW_LOCAL_FOUND_IN_LIBS)
        set(KDCRAW_LOCAL_DIR libkdcraw)
      endif (KDCRAW_LOCAL_FOUND_IN_LIBS)
    endif (KDCRAW_LOCAL_FOUND)
  endif (KDCRAW_LOCAL_DIR)

  if (KDCRAW_LOCAL_FOUND)

    # we need two include directories: because the version.h file is put into the build directory
    # TODO KDCRAW_INCLUDE_DIR sounds like it should contain only one directory...
    set(KDCRAW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${KDCRAW_LOCAL_DIR} ${CMAKE_BINARY_DIR}/${KDCRAW_LOCAL_DIR})
    set(KDCRAW_DEFINITIONS "-I${CMAKE_SOURCE_DIR}/${KDCRAW_LOCAL_DIR}" "-I${CMAKE_BINARY_DIR}/${KDCRAW_LOCAL_DIR}")
    set(KDCRAW_LIBRARIES kdcraw)
    message(STATUS "Found Kdcraw library in local sub-folder: ${CMAKE_SOURCE_DIR}/${KDCRAW_LOCAL_DIR}")
    set(KDCRAW_FOUND TRUE)
    mark_as_advanced(KDCRAW_INCLUDE_DIR KDCRAW_LIBRARIES KDCRAW_DEFINITIONS)

  else(KDCRAW_LOCAL_FOUND)

    if (NOT WIN32)
      message(STATUS "Check Kdcraw library using pkg-config...")
      # use pkg-config to get the directories and then use these values
      # in the FIND_PATH() and FIND_LIBRARY() calls
      include(FindPkgConfig)
      pkg_check_modules(PC_LIBKDCRAW libkdcraw-kde4>=0.2.0)
      set(KDCRAW_DEFINITIONS ${PC_LIBKDCRAW_CFLAGS_OTHER})
    endif(NOT WIN32)

    find_path(KDCRAW_INCLUDE_DIR NAMES libkdcraw/version.h
        HINTS
        ${PC_LIBKDCRAW_INCLUDEDIR}
        ${PC_LIBKDCRAW_INCLUDE_DIRS}
        )
    
    find_library(KDCRAW_LIBRARY NAMES kdcraw
        HINTS
        ${PC_LIBKDCRAW_LIBDIR}
        ${PC_LIBKDCRAW_LIBRARY_DIRS}
        )
    
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(Kdcraw DEFAULT_MSG KDCRAW_LIBRARY KDCRAW_INCLUDE_DIR)
    if(KDCRAW_FOUND)
       set(KDCRAW_LIBRARIES ${KDCRAW_LIBRARY})
    endif(KDCRAW_FOUND)
    
    mark_as_advanced(KDCRAW_INCLUDE_DIR KDCRAW_LIBRARIES KDCRAW_DEFINITIONS)

  endif(KDCRAW_LOCAL_FOUND)
  
endif (KDCRAW_INCLUDE_DIR AND KDCRAW_LIBRARIES AND KDCRAW_DEFINITIONS)
