# - Find TinyXML2
# Find the native TinyXML2 includes and library
#
#   TINYXML2_FOUND       - True if TinyXML found.
#   TINYXML2_INCLUDE_DIR - where to find tinyxml.h, etc.
#   TINYXML2_LIBRARIES   - List of libraries when using TinyXML.
#

IF( TINYXML2_INCLUDE_DIR )
    # Already in cache, be silent
    SET( TinyXML2_FIND_QUIETLY TRUE )
ENDIF( TINYXML2_INCLUDE_DIR )

FIND_PATH( TINYXML2_INCLUDE_DIR "tinyxml2.h"
  PATHS /usr/include
           PATH_SUFFIXES "tinyxml2" )
set (TINYXML2_INCLUDE_DIRS ${TINYXML2_INCLUDE_DIR})

file(STRINGS "${TINYXML2_INCLUDE_DIR}/tinyxml2.h" TINYXML2_MAJOR_VERSION
     LENGTH_MINIMUM 39 LIMIT_COUNT 1
     REGEX "static const int TIXML2_MAJOR_VERSION = [0-9]+;")
string(REGEX REPLACE "static const int TIXML2_MAJOR_VERSION = ([0-9]+)\\\\;"
       "\\1" TINYXML2_MAJOR_VERSION "${TINYXML2_MAJOR_VERSION}")
file(STRINGS "${TINYXML2_INCLUDE_DIR}/tinyxml2.h" TINYXML2_MINOR_VERSION
     LENGTH_MINIMUM 39 LIMIT_COUNT 1
     REGEX "static const int TIXML2_MINOR_VERSION = [0-9]+;")
string(REGEX REPLACE "static const int TIXML2_MINOR_VERSION = ([0-9]+)\\\\;"
       "\\1" TINYXML2_MINOR_VERSION "${TINYXML2_MINOR_VERSION}")
file(STRINGS "${TINYXML2_INCLUDE_DIR}/tinyxml2.h" TINYXML2_PATCH_VERSION
     LENGTH_MINIMUM 39 LIMIT_COUNT 1
     REGEX "static const int TIXML2_PATCH_VERSION = [0-9]+;")
string(REGEX REPLACE "static const int TIXML2_PATCH_VERSION = ([0-9]+)\\\\;"
       "\\1" TINYXML2_PATCH_VERSION "${TINYXML2_PATCH_VERSION}")
set(TINYXML2_VERSION
    "${TINYXML2_MAJOR_VERSION}.${TINYXML2_MINOR_VERSION}.${TINYXML2_PATCH_VERSION}")
math(EXPR TINYXML2_VERSION_CODE
    "${TINYXML2_MAJOR_VERSION}*100000+${TINYXML2_MINOR_VERSION}*100+${TINYXML2_PATCH_VERSION}")

if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
  set(PACKAGE_VERSION_COMPATIBLE TRUE)
  if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()
endif()

FIND_LIBRARY( TINYXML2_LIBRARY
              NAMES "tinyxml2"
              PATH_SUFFIXES "tinyxml2" )
set (TINYXML2_LIBRARIES ${TINYXML2_LIBRARY})

# handle the QUIETLY and REQUIRED arguments and set TINYXML_FOUND to TRUE if
# all listed variables are TRUE
include("FindPackageHandleStandardArgs")
find_package_handle_standard_args("TinyXML2"
  REQUIRED_VARS TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR TINYXML2_VERSION
  VERSION_VAR TINYXML2_VERSION)

MARK_AS_ADVANCED(TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR TINYXML2_INCLUDE_DIRS TINYXML2_LIBRARIES
  TINYXML2_VERSION TINYXML2_MAJOR_VERSION TINYXML2_MINOR_VERSION TINYXML2_PATCH_VERSION)
