# find or download Assimp

if (${FG_EXTERNALS_USE_PREBUILD} AND ${FG_ENABLE_ASSIMP})
	add_library( "Assimp-lib" INTERFACE )
	target_include_directories( "Assimp-lib" INTERFACE "${FG_EXTERNAL_PREBUILD_PATH}/Assimp/include" )
	target_compile_definitions( "Assimp-lib" INTERFACE "FG_ENABLE_ASSIMP" )
	set_property( TARGET "Assimp-lib" PROPERTY INTERFACE_LINK_LIBRARIES 
		"${FG_EXTERNAL_PREBUILD_PATH}/Assimp/lib/${CMAKE_STATIC_LIBRARY_PREFIX}assimp${CMAKE_STATIC_LIBRARY_SUFFIX}"
		"${FG_EXTERNAL_PREBUILD_PATH}/Assimp/lib/${CMAKE_STATIC_LIBRARY_PREFIX}IrrXML${CMAKE_STATIC_LIBRARY_SUFFIX}"
		"${FG_EXTERNAL_PREBUILD_PATH}/Assimp/lib/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX}" )


elseif (${FG_ENABLE_ASSIMP})
	set( FG_EXTERNAL_ASSIMP_PATH "" CACHE PATH "path to Assimp source" )
	mark_as_advanced( FG_EXTERNAL_ASSIMP_PATH )

	# reset to default
	if (NOT EXISTS "${FG_EXTERNAL_ASSIMP_PATH}/include/assimp")
		message( STATUS "Assimp is not found in \"${FG_EXTERNAL_ASSIMP_PATH}\"" )
		set( FG_EXTERNAL_ASSIMP_PATH "${FG_EXTERNALS_PATH}/assimp" CACHE PATH "" FORCE )
	else ()
		message( STATUS "Assimp found in \"${FG_EXTERNAL_ASSIMP_PATH}\"" )
	endif ()

	if (NOT EXISTS "${FG_EXTERNAL_ASSIMP_PATH}/include/assimp")
		set( FG_ASSIMP_REPOSITORY "https://github.com/assimp/assimp.git" )
	else ()
		set( FG_ASSIMP_REPOSITORY "" )
	endif ()
	
	# select version
	if (${FG_EXTERNALS_USE_STABLE_VERSIONS})
		set( ASSIMP_TAG "v5.0.0" )
	else ()
		set( ASSIMP_TAG "master" )
	endif ()

	set( FG_ASSIMP_INSTALL_DIR "${FG_EXTERNALS_INSTALL_PATH}/Assimp" )

	ExternalProject_Add( "External.Assimp"
		LIST_SEPARATOR		"${FG_LIST_SEPARATOR}"
		LOG_OUTPUT_ON_FAILURE 1
		# download
		GIT_REPOSITORY		${FG_ASSIMP_REPOSITORY}
		GIT_TAG				${ASSIMP_TAG}
		GIT_PROGRESS		1
		# update
		PATCH_COMMAND		${CMAKE_COMMAND} -E make_directory "${FG_EXTERNAL_ASSIMP_PATH}/cmake-ext"
					COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_FOLDER}/assimp_CMakeLists.txt" "${FG_EXTERNAL_ASSIMP_PATH}/cmake-ext/CMakeLists.txt"
		UPDATE_DISCONNECTED	1
		# configure
		SOURCE_DIR			"${FG_EXTERNAL_ASSIMP_PATH}"
		SOURCE_SUBDIR		"cmake-ext"
		CMAKE_GENERATOR		"${CMAKE_GENERATOR}"
		CMAKE_GENERATOR_PLATFORM "${CMAKE_GENERATOR_PLATFORM}"
		CMAKE_GENERATOR_TOOLSET	"${CMAKE_GENERATOR_TOOLSET}"
		CMAKE_ARGS			"-DCMAKE_CONFIGURATION_TYPES=${FG_EXTERNAL_CONFIGURATION_TYPES}"
							"-DCMAKE_SYSTEM_VERSION=${CMAKE_SYSTEM_VERSION}"
							"-DCMAKE_INSTALL_PREFIX=${FG_ASSIMP_INSTALL_DIR}"
							"-DASSIMP_BUILD_ASSIMP_TOOLS=OFF"
							"-DASSIMP_BUILD_ASSIMP_VIEW=OFF"
							"-DASSIMP_BUILD_SAMPLES=OFF"
							"-DASSIMP_BUILD_TESTS=OFF"
							"-DASSIMP_INSTALL_PDB=OFF"
							"-DBUILD_SHARED_LIBS=OFF"
							"-DCMAKE_DEBUG_POSTFIX="
							"-DCMAKE_RELEASE_POSTFIX="
							"-DINJECT_DEBUG_POSTFIX=OFF"
							${FG_BUILD_TARGET_FLAGS}
		LOG_CONFIGURE 		1
		# build
		BINARY_DIR			"${CMAKE_BINARY_DIR}/build-Assimp"
		BUILD_COMMAND		${CMAKE_COMMAND}
							--build .
							--target assimp
							--config $<CONFIG>
		LOG_BUILD 			1
		# install
		INSTALL_DIR 		"${FG_ASSIMP_INSTALL_DIR}"
		LOG_INSTALL 		1
		# test
		TEST_COMMAND		""
	)
	
	set_property( TARGET "External.Assimp" PROPERTY FOLDER "External" )
	
	if (MSVC)
		set( FG_ASSIMP_MAIN_LIB "assimp-vc${MSVC_TOOLSET_VERSION}-mt" )
	else()
		set( FG_ASSIMP_MAIN_LIB "assimp" ) # TODO
	endif()
		
	add_library( "Assimp-lib" INTERFACE )
	target_include_directories( "Assimp-lib" INTERFACE "${FG_ASSIMP_INSTALL_DIR}/include" )
	target_compile_definitions( "Assimp-lib" INTERFACE "FG_ENABLE_ASSIMP" )
	add_dependencies( "Assimp-lib" "External.Assimp" )
	set_property( TARGET "Assimp-lib" PROPERTY INTERFACE_LINK_LIBRARIES 
		# assimp
		"$<$<CONFIG:Debug>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${FG_ASSIMP_MAIN_LIB}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Profile>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${FG_ASSIMP_MAIN_LIB}${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Release>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${FG_ASSIMP_MAIN_LIB}${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
			
		# IrrXML$
		"$<$<CONFIG:Debug>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}IrrXML${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Profile>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}IrrXML${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Release>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}IrrXML${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
			
		# zlibstatic
		"$<$<CONFIG:Debug>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Profile>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>"
		"$<$<CONFIG:Release>:${FG_ASSIMP_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_RELEASE_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}>" )

endif ()
