cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
cmake_policy(SET CMP0020 NEW) # Automatically link Qt executables to qtmain target on Windows
if(NOT LIBPROJECT)
	include(QtWraps)
endif()
get_directory_property(hasParent PARENT_DIRECTORY)
set(_SEARCH_DIR)
if(WIN32)
	find_package(MinGW REQUIRED)
endif()
set(_QAD_INCDIR)
set(_QAD_LIBDIR)
if(MINGW_INCLUDE)
	list(APPEND _QAD_INCDIR "${MINGW_INCLUDE}/qad")
	list(APPEND _QAD_LIBDIR "${MINGW_LIB}")
endif()
if(CMAKE_CROSSCOMPILING)
	list(APPEND _QAD_INCDIR "${CMAKE_PREFIX_PATH}/include/qad")
	list(APPEND _QAD_LIBDIR "${CMAKE_PREFIX_PATH}/lib")
	#if (DEFINED ANDROID_PLATFORM)
	#	list(APPEND _PIP_INCDIR ${CMAKE_PREFIX_PATH}/include/qad)
	#	list(APPEND _PIP_LIBDIR ${CMAKE_PREFIX_PATH}/lib)
	#endif()
	include_directories(${CMAKE_PREFIX_PATH}/include)
	link_directories(${CMAKE_PREFIX_PATH}/lib)
else()
	if(NOT WIN32)
		list(APPEND _QAD_INCDIR "/usr/include/qad" "/usr/local/include/qad")
		list(APPEND _QAD_LIBDIR "/usr/lib" "/usr/local/lib")
	endif()
	if(APPLE)
		include_directories(/usr/local/include)
		link_directories(/usr/local/lib)
	endif()
endif()
find_file(QAD_H_INCLUDE "qad_types.h" HINTS ${PIP_DIR}/include/qad ${_QAD_INCDIR} $ENV{SMSDK_DIR}/include/qad)
#if (DEFINED ANDROID_PLATFORM)
#	set(QAD_INCLUDES ${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include/qad)
#else()
	get_filename_component(QAD_INCLUDES ${QAD_H_INCLUDE} PATH)
#endif()
set(_SEARCH_DIR ${_QAD_LIBDIR} $ENV{SMSDK_DIR}/lib)
set(_QAD_LIBS utils widgets application blockview graphic sql_table touch_widgets doc)
if(LIBPROJECT)
	set(QAD_INCLUDES ${QAD_DIR})
endif()
foreach(_Q ${_QAD_LIBS})
	string(TOUPPER ${_Q} _QU)
	if(LIBPROJECT)
		set(QAD_${_QU}_LIBRARY qad_${_Q})
		list(APPEND QAD_INCLUDES ${QAD_DIR}/${_Q})
	else()
		foreach(_v ${_QT_VERSIONS_})
			find_library(QAD${_v}_${_QU}_LIBRARY qad_${_Q}${_v} HINTS ${_SEARCH_DIR})
			set(MULTILIB_${_Q}_SUFFIX_Qt${_v} ${_v})
			if(hasParent)
				set(MULTILIB_${_Q}_SUFFIX_Qt${_v} ${_v} PARENT_SCOPE)
			endif()
		endforeach()
	endif()
	#message(STATUS "Library qad_${_Q}5 (${_QU}) -> ${QAD5_${_QU}_LIBRARY} found in ${_SEARCH_DIR}")
	list(APPEND QT_MULTILIB_LIST qad_${_Q})
endforeach()
list(APPEND QT_MULTILIB_LIST qglview qglengine_core qglengine_widgets)
foreach(_v ${_QT_VERSIONS_})
	find_library(QGLVIEW${_v}_LIBRARY qglview${_v} ${_SEARCH_DIR})
	set(MULTILIB_qglview_SUFFIX_Qt${_v} ${_v})
	if(hasParent)
		set(MULTILIB_qglview_SUFFIX_Qt${_v} ${_v} PARENT_SCOPE)
	endif()
endforeach()
if(hasParent)
	set(QT_MULTILIB_LIST ${QT_MULTILIB_LIST} PARENT_SCOPE)
endif()

if(NOT LIBPROJECT)
	if(NOT QAD_VERSION)
		include(CheckSymbolExists)
		check_symbol_exists(QAD_VERSION_NAME "${QAD_INCLUDES}/qad_version.h" HAVE_QAD_VERSION)
		if (HAVE_QAD_VERSION)
			file(STRINGS "${QAD_INCLUDES}/qad_version.h" _VERSION REGEX "^[ \t]*#define[ \t]+QAD_VERSION_NAME+[ \t]+.*$")
			string(REGEX MATCH "\".*\"" _VERSION ${_VERSION})
			string(LENGTH ${_VERSION} SL)
			math(EXPR SL ${SL}-2)
			string(SUBSTRING ${_VERSION} 1 ${SL} _VERSION)
			set(QAD_VERSION ${_VERSION} CACHE STRING "VERSION")
		else()
			message(FATAL_ERROR "Can`t find QAD version file!")
		endif()
	endif()
	if(QAD_FIND_VERSION VERSION_GREATER QAD_VERSION)
		message(FATAL_ERROR "QAD version ${QAD_VERSION} is available, but ${QAD_FIND_VERSION} requested!")
	endif()
	if (NOT _QAD_MSG)
		set(_QAD_MSG 1 CACHE BOOL "msg_qad" FORCE)
		message(STATUS "Found QAD (version \"${QAD_VERSION}\")")
	endif()
endif()
