# Get package location hint from environment variable (if any)
if(NOT TinyXML_ROOT_DIR AND DEFINED ENV{TinyXML_ROOT_DIR})
    set(TinyXML_ROOT_DIR "$ENV{TinyXML_ROOT_DIR}" CACHE PATH
            "TinyXML base directory location (optional, used for nonstandard installation paths)")
endif()

# Search path for nonstandard package locations
if(TinyXML_ROOT_DIR)
    set(TinyXML_INCLUDE_PATH PATHS "${TinyXML_ROOT_DIR}/include" NO_DEFAULT_PATH)
    set(TinyXML_LIBRARY_PATH PATHS "${TinyXML_ROOT_DIR}/lib"     NO_DEFAULT_PATH)
endif()

# Find headers and libraries
find_path(TinyXML_INCLUDE_DIR NAMES tinyxml.h PATH_SUFFIXES "tinyxml" ${TinyXML_INCLUDE_PATH})
find_library(TinyXML_LIBRARY  NAMES tinyxml   PATH_SUFFIXES "tinyxml" ${TinyXML_LIBRARY_PATH})

if (TinyXML_INCLUDE_DIR)
    if (EXISTS "${TinyXML_INCLUDE_DIR}/tinyxml.h")
        FILE(READ "${TinyXML_INCLUDE_DIR}/tinyxml.h" TinyXML_VERSION_CONTENT)

        STRING(REGEX MATCH "const int TIXML_MAJOR_VERSION = ([0-9]+)" _dummy "${TinyXML_VERSION_CONTENT}")
        SET(TinyXML_VERSION_MAJOR "${CMAKE_MATCH_1}")

        STRING(REGEX MATCH "const int TIXML_MINOR_VERSION = ([0-9]+)" _dummy "${TinyXML_VERSION_CONTENT}")
        SET(TinyXML_VERSION_MINOR "${CMAKE_MATCH_1}")

        STRING(REGEX MATCH "const int TIXML_PATCH_VERSION = ([0-9]+)" _dummy "${TinyXML_VERSION_CONTENT}")
        SET(TinyXML_VERSION_PATCH "${CMAKE_MATCH_1}")

        SET(TinyXML_VERSION "${TinyXML_VERSION_MAJOR}.${TinyXML_VERSION_MINOR}.${TinyXML_VERSION_PATCH}")
    endif ()
endif ()

mark_as_advanced(TinyXML_INCLUDE_DIR
        TinyXML_LIBRARY)

# Output variables generation
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(TinyXML REQUIRED_VARS TinyXML_LIBRARY TinyXML_INCLUDE_DIR
                                          VERSION_VAR TinyXML_VERSION)

set(TinyXML_FOUND ${TINYXML_FOUND}) # Enforce case-correctness: Set appropriately cased variable...
unset(TINYXML_FOUND) # ...and unset uppercase variable generated by find_package_handle_standard_args

if(TinyXML_FOUND)
    set(TinyXML_INCLUDE_DIRS ${TinyXML_INCLUDE_DIR})
    set(TinyXML_LIBRARIES ${TinyXML_LIBRARY})
endif()


if(TinyXML_FOUND)
    if(NOT TARGET TinyXML::TinyXML)
        add_library(TinyXML::TinyXML UNKNOWN IMPORTED)
        set_target_properties(TinyXML::TinyXML PROPERTIES
                INTERFACE_INCLUDE_DIRECTORIES "${TinyXML_INCLUDE_DIRS}")

        if(TinyXML_LIBRARY_RELEASE)
            set_property(TARGET TinyXML::TinyXML APPEND PROPERTY
                    IMPORTED_CONFIGURATIONS RELEASE)
            set_target_properties(TinyXML::TinyXML PROPERTIES
                    IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
                    IMPORTED_LOCATION_RELEASE "${TinyXML_LIBRARY_RELEASE}")
        endif()

        if(TinyXML_LIBRARY_DEBUG)
            set_property(TARGET TinyXML::TinyXML APPEND PROPERTY
                    IMPORTED_CONFIGURATIONS DEBUG)
            set_target_properties(TinyXML::TinyXML PROPERTIES
                    IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
                    IMPORTED_LOCATION_DEBUG "${TinyXML_LIBRARY_DEBUG}")
        endif()

        if(NOT TinyXML_LIBRARY_RELEASE AND NOT TinyXML_LIBRARY_DEBUG)
            set_target_properties(TinyXML::TinyXML PROPERTIES
                    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
                    IMPORTED_LOCATION "${TinyXML_LIBRARY}")
        endif()
    endif()
endif()
