# try to find libxml2, once done following variables will be defined
#
# LIBXML2_FOUND        - system has libxml2
# LIBXML2_VERSION      - the version of libxml2

# LIBXML2_INCLUDE_DIR - the include dir of libxml2
# LIBXML2_LIBRARY     - the filepath of libxml2.a|so|dylib

# LIBXML2_INCLUDE_DIRS - the include dir of libxml2         and it's dependencies's include directory
# LIBXML2_LIBRARY_LIST - the filepath of libxml2.a|so|dylib and it's dependencies's library


if (LIBXML2_INCLUDE_DIR AND LIBXML2_LIBRARY)
    set(LIBXML2_FOUND TRUE)
else()
    pkg_check_modules(PKG_CONFIG_LIBXML2 QUIET libxml-2.0)

    #message("PKG_CONFIG_LIBXML2_FOUND=${PKG_CONFIG_LIBXML2_FOUND}")
    #message("PKG_CONFIG_LIBXML2_INCLUDE_DIRS=${PKG_CONFIG_LIBXML2_INCLUDE_DIRS}")
    #message("PKG_CONFIG_LIBXML2_LIBRARY_DIRS=${PKG_CONFIG_LIBXML2_LIBRARY_DIRS}")
    #message("PKG_CONFIG_LIBXML2_INCLUDEDIR=${PKG_CONFIG_LIBXML2_INCLUDEDIR}")
    #message("PKG_CONFIG_LIBXML2_LIBDIR=${PKG_CONFIG_LIBXML2_LIBDIR}")
    #message("PKG_CONFIG_LIBXML2_VERSION=${PKG_CONFIG_LIBXML2_VERSION}")
    #message("PKG_CONFIG_LIBXML2_LDFLAGS=${PKG_CONFIG_LIBXML2_LDFLAGS}")
    #message("PKG_CONFIG_LIBXML2_LDFLAGS_OTHER=${PKG_CONFIG_LIBXML2_LDFLAGS_OTHER}")
    #message("PKG_CONFIG_LIBXML2_STATIC_LIBRARIES=${PKG_CONFIG_LIBXML2_STATIC_LIBRARIES}")

    set(LIBXML2_INCLUDE_DIR)
    set(LIBXML2_LIBRARY)

    set(LIBXML2_INCLUDE_DIRS)
    set(LIBXML2_LIBRARY_LIST)

    if (ENABLE_STATIC)
        if (PKG_CONFIG_LIBXML2_FOUND)
            foreach (item ${PKG_CONFIG_LIBXML2_STATIC_LIBRARIES})
                if  (item STREQUAL "xml2")
                    find_path   (LIBXML2_INCLUDE_DIR libxml/SAX2.h  HINTS ${PKG_CONFIG_LIBXML2_INCLUDE_DIRS})
                    find_library(LIBXML2_LIBRARY     libxml2.a xml2 HINTS ${PKG_CONFIG_LIBXML2_LIBRARY_DIRS})
                elseif (item STREQUAL "m")
                    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
                        set(LIBM_FOUND 1)
                        set(HAVE_LIBM  1)
                    else()
                        find_package(LIBM)
                        if (LIBM_FOUND)
                            set(HAVE_LIBM 1)
                            list(APPEND LIBXML2_INCLUDE_DIRS ${LIBM_INCLUDE_DIR})
                            list(APPEND LIBXML2_LIBRARY_LIST ${LIBM_LIBRARY})
                        endif()
                    endif()
                elseif (item STREQUAL "z")
                    find_package(LIBZ)
                    if (LIBZ_FOUND)
                        set(HAVE_LIBZ 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${LIBZ_INCLUDE_DIR})
                        list(APPEND LIBXML2_LIBRARY_LIST ${LIBZ_LIBRARY})
                    endif()
                elseif (item STREQUAL "lzma")
                    find_package(LIBLZMA)
                    if (LIBLZMA_FOUND)
                        set(HAVE_LIBLZMA 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${LIBLZMA_INCLUDE_DIRS})
                        list(APPEND LIBXML2_LIBRARY_LIST ${LIBLZMA_LIBRARY_LIST})
                    endif()
                elseif (item STREQUAL "iconv")
                    find_package(ICONV)
                    if (ICONV_FOUND)
                        set(HAVE_ICONV 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${ICONV_INCLUDE_DIR})
                        list(APPEND LIBXML2_LIBRARY_LIST ${ICONV_LIBRARY})
                    endif()
                elseif (item STREQUAL "pthread")
                    if (CMAKE_SYSTEM_NAME STREQUAL "Android")
                        set(PTHREAD_FOUND 1)
                        set(HAVE_PTHREAD 1)
                    else()
                        find_package(PTHREAD)
                        if (PTHREAD_FOUND)
                            set(HAVE_PTHREAD 1)
                            list(APPEND LIBXML2_INCLUDE_DIRS ${PTHREAD_INCLUDE_DIR})
                            list(APPEND LIBXML2_LIBRARY_LIST ${PTHREAD_LIBRARY})
                        endif()
                    endif()
                elseif (item STREQUAL "icui18n")
                    find_package(ICU_I18N)
                    if (ICU_I18N_FOUND)
                        set(HAVE_ICU_I18N 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${ICU_I18N_INCLUDE_DIR})
                        list(APPEND LIBXML2_LIBRARY_LIST ${ICU_I18N_LIBRARY})
                    endif()
                elseif (item STREQUAL "icudata")
                    find_package(ICU_DATA)
                    if (ICU_DATA_FOUND)
                        set(HAVE_ICU_DATA 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${ICU_DATA_INCLUDE_DIR})
                        list(APPEND LIBXML2_LIBRARY_LIST ${ICU_DATA_LIBRARY})
                    endif()
                elseif (item STREQUAL "icuuc")
                    find_package(ICU_UC)
                    if (ICU_UC_FOUND)
                        set(HAVE_ICU_UC 1)
                        list(APPEND LIBXML2_INCLUDE_DIRS ${ICU_UC_INCLUDE_DIRS})
                        list(APPEND LIBXML2_LIBRARY_LIST ${ICU_UC_LIBRARY_LIST})
                    endif()
                endif()
            endforeach()
        else()
            find_path   (LIBXML2_INCLUDE_DIR libxml/SAX2.h)
            find_library(LIBXML2_LIBRARY     libxml2.a xml2)
        endif()
    else()
        if (PKG_CONFIG_LIBXML2_FOUND)
            find_path   (LIBXML2_INCLUDE_DIR libxml/SAX2.h HINTS ${PKG_CONFIG_LIBXML2_INCLUDE_DIRS})
            find_library(LIBXML2_LIBRARY     xml2          HINTS ${PKG_CONFIG_LIBXML2_LIBRARY_DIRS})
        else()
            find_path   (LIBXML2_INCLUDE_DIR libxml/SAX2.h)
            find_library(LIBXML2_LIBRARY     xml2)
        endif()
    endif()

    if (LIBXML2_INCLUDE_DIR AND LIBXML2_LIBRARY)
        list(INSERT LIBXML2_INCLUDE_DIRS 0 ${LIBXML2_INCLUDE_DIR})
        list(INSERT LIBXML2_LIBRARY_LIST 0 ${LIBXML2_LIBRARY})
        if (PKG_CONFIG_LIBXML2_VERSION)
            set(LIBXML2_VERSION ${PKG_CONFIG_LIBXML2_VERSION})
        endif()
    endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LIBXML2 REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR VERSION_VAR LIBXML2_VERSION)

mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY)
