# - Find Qt5
#
#  Below is a detailed list of variables that FindQt5.cmake sets.
#  QT_FOUND                     If false, don't try to use Qt.
#  QT5_FOUND                    If false, don't try to use Qt5.
#
#  QT_VERSION_MAJOR             The major version of Qt found.
#  QT_VERSION_MINOR             The minor version of Qt found.
#  QT_VERSION_PATCH             The patch version of Qt found.
#
#  QT_BINARY_DIR                Path to "bin" of Qt5
#  QT_DOC_DIR                   Path to "doc" of Qt5
#
# also defined, but NOT for general use are
#  QT_MOC_EXECUTABLE            Where to find the moc tool
#  QT_CONFIG_FLAGS              Flags used when building Qt

# Copyright (C) 2001-2009 Kitware, Inc.
# Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
# Copyright (C) 2011 Nokia Corporation
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

IF(QT_INCLUDES AND QT_LIBRARIES AND QT_MAJOR_VERSION MATCHES 5)
  # Already in cache, be silent
  SET(QT_FOUND TRUE)
  SET(QT5_FOUND TRUE)
  RETURN()
ENDIF(QT_INCLUDES AND QT_LIBRARIES AND QT_MAJOR_VERSION MATCHES 5)

IF(NOT Qt5Core_DIR )
    IF(NOT QT_QMAKE_EXECUTABLE)
        FIND_PROGRAM(QT_QMAKE_EXECUTABLE_FINDQT NAMES qmake qmake5 qmake-qt5
            PATHS "${QT_SEARCH_PATH}/bin" "$ENV{QTDIR}/bin")
        SET(QT_QMAKE_EXECUTABLE ${QT_QMAKE_EXECUTABLE_FINDQT} CACHE PATH "Qt qmake program.")
    ENDIF(NOT QT_QMAKE_EXECUTABLE)

    EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_VERSION" OUTPUT_VARIABLE QTVERSION)
    IF(NOT QTVERSION MATCHES "5.*")
        SET(QT_FOUND FALSE)
        SET(QT5_FOUND FALSE)
        IF(Qt5_FIND_REQUIRED)
            MESSAGE(FATAL_ERROR "CMake was unable to find Qt5, put qmake in your path or set QTDIR/QT_QMAKE_EXECUTABLE.")
        ENDIF(Qt5_FIND_REQUIRED)
        RETURN()
    ENDIF(NOT QTVERSION MATCHES "5.*")
ENDIF(NOT Qt5Core_DIR )

find_package(Qt5Core ${REQUIRED_QT_VERSION} REQUIRED)

SET(QT_VERSION_MAJOR ${Qt5Core_VERSION_MAJOR})
SET(QT_VERSION_MINOR ${Qt5Core_VERSION_MINOR})
SET(QT_VERSION_PATCH ${Qt5Core_VERSION_PATCH})
SET(QT_VERSION ${Qt5Core_VERSION})

GET_PROPERTY(QT_QMAKE_EXECUTABLE TARGET ${Qt5Core_QMAKE_EXECUTABLE} PROPERTY IMPORTED_LOCATION)

IF(NOT QT_INCLUDE_DIR)
  EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_INSTALL_HEADERS" OUTPUT_VARIABLE QTHEADERS)
  SET(QT_INCLUDE_DIR ${QTHEADERS} CACHE INTERNAL "" FORCE)
ENDIF(NOT QT_INCLUDE_DIR)

IF(NOT QT_LIBRARY_DIR)
  EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_INSTALL_LIBS" OUTPUT_VARIABLE QTLIBS)
  SET(QT_LIBRARY_DIR ${QTLIBS} CACHE INTERNAL "" FORCE)
ENDIF(NOT QT_LIBRARY_DIR)

IF(NOT QT_BINARY_DIR)
  EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_INSTALL_BINS" OUTPUT_VARIABLE QTBINS)
  SET(QT_BINARY_DIR ${QTBINS} CACHE INTERNAL "" FORCE)
ENDIF(NOT QT_BINARY_DIR)

IF(NOT QT_DOC_DIR)
  EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_INSTALL_DOCS" OUTPUT_VARIABLE QTDOCS)
  SET(QT_DOC_DIR ${QTDOCS} CACHE INTERNAL "" FORCE)
ENDIF(NOT QT_DOC_DIR)

IF(NOT QT_PLUGINS_DIR)
  EXEC_PROGRAM(${QT_QMAKE_EXECUTABLE} ARGS "-query QT_INSTALL_PLUGINS" OUTPUT_VARIABLE QTPLUGINS)
  SET(QT_PLUGINS_DIR ${QTPLUGINS} CACHE INTERNAL "" FORCE)
ENDIF(NOT QT_PLUGINS_DIR)

IF(NOT QT_MOC_EXECUTABLE)
  FIND_PROGRAM(QT_MOC_EXECUTABLE NAMES moc moc5 moc-qt5 PATHS ${QT_BINARY_DIR}
               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
ENDIF(NOT QT_MOC_EXECUTABLE)

MARK_AS_ADVANCED(QT_INCLUDE_DIR
                 QT_LIBRARY_DIR
                 QT_BINARY_DIR
                 QT_DOC_DIR
				 QT_PLUGINS_DIR
                 QT_QMAKE_EXECUTABLE_FINDQT QT_QMAKE_EXECUTABLE QT_MOC_EXECUTABLE)

SET(QT_FOUND TRUE)
SET(QT5_FOUND TRUE)