#
# Find libxml2 (http://www.xmlsoft.org)
#
# Prioritizes LIBXML2_HOME and QMC_BUILD_STATIC
# before calling standard cmake find_package(LibXml2)
#
# Define the following values:
#
# LibXml2_INCLUDE_DIR  = where libxml/xpath.h can be found
# LibXml2_LIBRARIES    = the library to link against libxml2
# LibXml2_FOUND        = set to 1 if libxml2 is found
#
# Environment variables:
#
# LIBXML2_HOME = directory containing lib/ and include/libxml2
#

SET(Libxml2 xml2)
IF(QMC_BUILD_STATIC)
  SET(Libxml2 libxml2.a)
ENDIF(QMC_BUILD_STATIC)

IF(Libxml2_INCLUDE_DIRS)
  MESSAGE(STATUS "Trying libxml2 via Libxml2_INCLUDE_DIRS")
  FIND_PATH(LIBXML2_INCLUDE_DIR libxml/xpath.h ${Libxml2_INCLUDE_DIRS} NO_DEFAULT_PATH)
  FIND_LIBRARY(LIBXML2_LIBRARIES xml2 ${Libxml2_LIBRARY_DIRS} NO_DEFAULT_PATH)
ELSE(Libxml2_INCLUDE_DIRS)
  MESSAGE(STATUS "Trying libxml2 via LIBXML2_HOME")
  FIND_LIBRARY(LIBXML2_LIBRARIES ${Libxml2} PATHS $ENV{LIBXML2_HOME}/lib NO_DEFAULT_PATH)
  FIND_PATH(LIBXML_INCLUDE_DIR libxml2/libxml/xpath.h PATHS ${LIBXML2_HOME}/include $ENV{LIBXML2_HOME}/include NO_DEFAULT_PATH)
  if(LIBXML_INCLUDE_DIR)
    set(LIBXML2_INCLUDE_DIR ${LIBXML_INCLUDE_DIR}/libxml2)
  else(LIBXML_INCLUDE_DIR)
    FIND_PATH(LIBXML2_INCLUDE_DIR libxml/xpath.h $ENV{LIBXML2_HOME}/include)
  endif(LIBXML_INCLUDE_DIR)
ENDIF(Libxml2_INCLUDE_DIRS)

SET(LIBXML2_FOUND FALSE)
IF(LIBXML2_INCLUDE_DIR AND LIBXML2_LIBRARIES)
  MESSAGE(STATUS "LIBXML2_INCLUDE_DIR=${LIBXML2_INCLUDE_DIR}")
  MESSAGE(STATUS "LIBXML2_LIBRARIES=${LIBXML2_LIBRARIES}")
  SET(LIBXML2_FOUND TRUE)
ENDIF()

IF(LIBXML2_FOUND)
  MESSAGE(STATUS "Libxml2 found by FindLibxml2QMC")  
ELSE(LIBXML2_FOUND)
  MESSAGE(STATUS "Locating Libxml2 via FIND_PACKAGE")
  FIND_PACKAGE(LibXml2)
ENDIF(LIBXML2_FOUND)

MARK_AS_ADVANCED(
  LIBXML2_INCLUDE_DIR 
  LIBXML2_LIBRARIES 
  LIBXML2_FOUND
  )
  
