#INCLUDE( WGExternalProject )
INCLUDE( FindPackageHandleStandardArgs )
INCLUDE( ExternalModuleUrls )


IF( NOT QT5_FIND_VERSION )
    SET( QT5_FIND_VERSION "5.6.1-1" )
ENDIF()

#Need to use 5.6.1.1 because dashes aren't allowed in version
#numbers passed into find_package
IF ( "${QT5_FIND_VERSION}" STREQUAL "5.6.1.1")
    SET( QT5_FIND_VERSION "5.6.1-1" )
IF( EXISTS ${Qt5_URL} )
	SET(QT5_URL ${QT511_URL})
ENDIF()
ENDIF()

SET (QT5_DIRECTORY "${WG_TOOLS_SOURCE_DIR}/core/third_party/Qt/${QT5_FIND_VERSION}" )

SET (Qt5_DOWNLOAD_DIR ${QT5_DIRECTORY})
IF ( BW_PLATFORM_WINDOWS )
	# CMAKE_GENERATOR_TOOLSET check is for generating Visual Studio solutions
	# CMAKE_LINKER check is for generating QtCreator projects
	IF ( CMAKE_GENERATOR_TOOLSET STREQUAL "v110_xp"
		OR CMAKE_GENERATOR_TOOLSET STREQUAL "v110"
		OR CMAKE_LINKER MATCHES "Visual Studio 11" )
		SET( QT5_DIRECTORY "${QT5_DIRECTORY}/msvc2012" )
	ELSEIF( CMAKE_GENERATOR_TOOLSET STREQUAL "v120_xp"
		OR CMAKE_GENERATOR_TOOLSET STREQUAL "v120"
		OR CMAKE_LINKER MATCHES "Visual Studio 12")
		SET( QT5_DIRECTORY "${QT5_DIRECTORY}/msvc2013" )
	ELSEIF( CMAKE_GENERATOR_TOOLSET STREQUAL "v140_xp"
		OR CMAKE_GENERATOR_TOOLSET STREQUAL "v140"
		OR CMAKE_GENERATOR_TOOLSET STREQUAL "v141"
		OR CMAKE_LINKER MATCHES "Visual Studio 14")
		SET( QT5_DIRECTORY "${QT5_DIRECTORY}/msvc2015" )
	ELSE()
		MESSAGE( FATAL_ERROR "Cannot find Qt binaries built with the ${CMAKE_GENERATOR_TOOLSET} toolset." )
	ENDIF()
ELSEIF( BW_PLATFORM_MAC )
	SET( QT5_DIRECTORY "${QT5_DIRECTORY}/clang" )
ELSE()
	SET( QT5_DIRECTORY "${QT5_DIRECTORY}/___unsupported___" )
ENDIF()

IF (BW_ARCH_64)
	SET( QT5_DIRECTORY "${QT5_DIRECTORY}_64" )
ENDIF()

IF( NOT EXISTS "${QT5_DIRECTORY}/" AND EXISTS "${QT5_DIRECTORY}_opengl/" )
	SET( QT5_DIRECTORY "${QT5_DIRECTORY}_opengl" )
ENDIF()

IF (NOT EXISTS "${QT5_DIRECTORY}")
IF( EXISTS ${Qt5_URL} )
	FILE( REMOVE_RECURSE "${WG_EXTERNAL_PROJECTS_ROOT}/Qt5" )
	IF( ${CMAKE_VERSION} VERSION_GREATER "3.5")
		WG_FETCH_EXTERNAL_PACKAGE( Qt5 ${QT5_FIND_VERSION}
			GIT_REPOSITORY ${Qt5_URL}
			CONFIGURE_COMMAND ""
			BUILD_COMMAND ""
			INSTALL_COMMAND ""
			GIT_SHALLOW 1
			SOURCE_DIR ${Qt5_DOWNLOAD_DIR}
			)
	ELSE()
		WG_FETCH_EXTERNAL_PACKAGE( Qt5 ${QT5_FIND_VERSION}
			GIT_REPOSITORY ${Qt5_URL}
			CONFIGURE_COMMAND ""
			BUILD_COMMAND ""
			INSTALL_COMMAND ""
			SOURCE_DIR ${Qt5_DOWNLOAD_DIR}
			)
	ENDIF()
ENDIF()
ENDIF()

INCLUDE( WGQtLibrary )