# - Try to find the Librdf library
#
# Once done this will define
#
#  LIBRDF_FOUND - System has 
#  LIBRDF_INCLUDE_DIR - The librdf include directory
#  LIBRDF_LIBRARIES - The libraries needed to use rdf

if (NOT DEFINED LIBRDF_ROOT)
  set(LIBRDF_ROOT_FOLDER $ENV{LIBRDF_ROOT})
endif()

IF (LIBRDF_INCLUDE_DIR AND LIBRDF_LIBRARY)
   # in cache already
   SET(LibRdf_FIND_QUIETLY TRUE)
ENDIF (LIBRDF_INCLUDE_DIR AND LIBRDF_LIBRARY)

IF (NOT WIN32)
   # try using pkg-config to get the directories and then use these values
   # in the FIND_PATH() and FIND_LIBRARY() calls
   # also fills in LIBRDF_DEFINITIONS, although that isn't normally useful
   FIND_PACKAGE(PkgConfig)
   PKG_SEARCH_MODULE(PC_LIBRDF librdf)
   SET(LIBRDF_DEFINITIONS ${PC_LIBRDF_CFLAGS})
ENDIF (NOT WIN32)


# attempt to find the botan include directory using the standard KETO build process
# fall back to the installed packages
FIND_PATH(LIBRDF_INCLUDE_DIR librdf.h
    PATHS "${LIBRDF_ROOT_FOLDER}/include/"
)
FIND_PATH(LIBRDF_RAPTOR_INCLUDE_DIR raptor2.h
    PATHS "${LIBRDF_ROOT_FOLDER}/include/raptor2/"
)
FIND_PATH(LIBRDF_RASQAL_INCLUDE_DIR rasqal.h
    PATHS "${LIBRDF_ROOT_FOLDER}/include/rasqal/"
)
IF (NOT LIBRDF_INCLUDE_DIR)
    FIND_PATH(LIBRDF_INCLUDE_DIR librdf.h
        HINTS
        ${PC_LIBRDF_INCLUDEDIR}
        ${PC_LIBRDF_INCLUDE_DIRS}
        )
ENDIF (NOT LIBRDF_INCLUDE_DIR)

# find the library, first be optimist and this is being built with a standard KETO build,
# if not found fall back to the possible package install
find_library(LIBRDF_RAPTOR_LIBRARY
    NAMES raptor2
    PATHS ${LIBRDF_ROOT_FOLDER}/lib)

find_library(LIBRDF_RASQAL_LIBRARY
    NAMES rasqal
    PATHS ${LIBRDF_ROOT_FOLDER}/lib)

find_library(LIBRDF_LIBRARY
    NAMES rdf
    PATHS ${LIBRDF_ROOT_FOLDER}/lib)

IF (NOT LIBRDF_LIBRARY)
    FIND_LIBRARY(LIBRDF_LIBRARY NAMES ${PC_LIBRDF_LIBRARIES}
        HINTS
        ${PC_LIBRDF_LIBDIR}
        ${PC_LIBRDF_LIBRARY_DIRS}
        )
ENDIF (NOT LIBRDF_LIBRARY)

FIND_LIBRARY(LIBRDF_EXTRA_LIBRARY 
        NAMES
        db
        )

FIND_LIBRARY(LIBRDF_XML_EXTRA_LIBRARY 
        NAMES
        xml2
        )

FIND_LIBRARY(LIBRDF_PCRE_EXTRA_LIBRARY 
        NAMES
        pcre
        )

FIND_LIBRARY(LIBRDF_LTDL_EXTRA_LIBRARY 
        NAMES
        ltdl
        )
FIND_LIBRARY(LIBRDF_GMP_EXTRA_LIBRARY 
        NAMES
        gmp
        )

message(STATUS "LIBRDF_INCLUDE_DIR " ${LIBRDF_INCLUDE_DIR})
message(STATUS "LIBRDF_RAPTOR_INCLUDE_DIR " ${LIBRDF_RAPTOR_INCLUDE_DIR})
message(STATUS "LIBRDF_RASQAL_INCLUDE_DIR " ${LIBRDF_RASQAL_INCLUDE_DIR})
message(STATUS "LIBRDF_LIBRARY " ${LIBRDF_LIBRARY})
message(STATUS "LIBRDF_RAPTOR_LIBRARY " ${LIBRDF_RAPTOR_LIBRARY})
message(STATUS "LIBRDF_RASQAL_LIBRARY " ${LIBRDF_RASQAL_LIBRARY})
message(STATUS "LIBRDF_EXTRA_LIBRARY " ${LIBRDF_EXTRA_LIBRARY})
message(STATUS "LIBRDF_PCRE_EXTRA_LIBRARY " ${LIBRDF_PCRE_EXTRA_LIBRARY})
message(STATUS "LIBRDF_LTDL_EXTRA_LIBRARY " ${LIBRDF_LTDL_EXTRA_LIBRARY})

MARK_AS_ADVANCED(LIBRDF_INCLUDE_DIR LIBRDF_RAPTOR_INCLUDE_DIR LIBRDF_RASQAL_INCLUDE_DIR LIBRDF_LIBRARY LIBRDF_RAPTOR_LIBRARY LIBRDF_RASQAL_LIBRARY LIBRDF_EXTRA_LIBRARY)

# handle the QUIETLY and REQUIRED arguments and set LIBRDF_FOUND to TRUE if 
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Librdf DEFAULT_MSG LIBRDF_LIBRARY LIBRDF_RAPTOR_LIBRARY LIBRDF_RASQAL_LIBRARY LIBRDF_INCLUDE_DIR LIBRDF_EXTRA_LIBRARY)

IF(LIBRDF_FOUND)
    SET(LIBRDF_LIBRARIES    ${LIBRDF_LIBRARY})
    SET(LIBRDF_RAPTOR_LIBRARY    ${LIBRDF_RAPTOR_LIBRARY})
    SET(LIBRDF_RASQAL_LIBRARY    ${LIBRDF_RASQAL_LIBRARY})
    SET(LIBRDF_INCLUDE_DIRS ${LIBRDF_INCLUDE_DIR})
    SET(LIBRDF_RAPTOR_INCLUDE_DIR ${LIBRDF_RAPTOR_INCLUDE_DIR})
    SET(LIBRDF_RASQAL_INCLUDE_DIR ${LIBRDF_RASQAL_INCLUDE_DIR})
    SET(LIBRDF_EXTRA_LIBRARY ${LIBRDF_EXTRA_LIBRARY})
    SET(LIBRDF_XML_EXTRA_LIBRARY ${LIBRDF_XML_EXTRA_LIBRARY})
    SET(LIBRDF_XML_EXTRA_LIBRARY ${LIBRDF_XML_EXTRA_LIBRARY})
    SET(LIBRDF_PCRE_EXTRA_LIBRARY ${LIBRDF_PCRE_EXTRA_LIBRARY})
    SET(LIBRDF_LTDL_EXTRA_LIBRARY ${LIBRDF_LTDL_EXTRA_LIBRARY})
ENDIF()
