set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${QT_PATH}")

# enable automatic linking of qtmain
if (POLICY CMP0020)
  cmake_policy(SET CMP0020 NEW)
endif (POLICY CMP0020)

find_package(Qt5Widgets QUIET)
if (Qt5Widgets_FOUND)
  find_package(Qt5Script REQUIRED)
  include_directories(${Qt5Widgets_INCLUDE_DIRS} ${Qt5Script_INCLUDE_DIRS})
  add_definitions(${Qt5Widgets_DEFINITONS})
  macro(qtX_wrap_ui)
    qt5_wrap_ui(${ARGN})
  endmacro()
  macro(qtX_wrap_cpp)
    qt5_wrap_cpp(${ARGN})
  endmacro()
  macro(qtX_add_resources)
    qt5_add_resources(${ARGN})
  endmacro()

  set(CMake_QT_LIBRARIES Qt5::Widgets Qt5::Script)

  if(WIN32 AND TARGET Qt5::Core)
    get_property(_Qt5_Core_LOCATION TARGET Qt5::Core PROPERTY LOCATION)
    get_filename_component(QT_BINARY_DIR "${_Qt5_Core_LOCATION}" PATH)
  endif()
else()
  set(QT_MIN_VERSION "4.4.3")
  find_package(Qt4)
  if(NOT QT4_FOUND)
    return()
  endif()

  macro(qtX_wrap_ui)
    qt4_wrap_ui(${ARGN})
  endmacro()
  macro(qtX_wrap_cpp)
    qt4_wrap_cpp(${ARGN})
  endmacro()
  macro(qtX_add_resources)
    qt4_add_resources(${ARGN})
  endmacro()

  set( QT_USE_QTSCRIPT TRUE )
  set( QT_USE_QTMAIN   TRUE )
  if ( QT_USE_FILE )
      include( ${QT_USE_FILE} )
  endif()
  add_definitions(${QT_DEFINITIONS})

  set(CMake_QT_LIBRARIES ${QT_LIBRARIES})

endif()
