####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was PkgBinConfig.cmake.in ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file)
set(${_var} "${_file}")
  if(NOT EXISTS "${_file}")
    message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
endif()
endmacro()

macro(check_required_components _NAME)
foreach(comp ${${_NAME}_FIND_COMPONENTS})
if(NOT ${_NAME}_${comp}_FOUND)
if(${_NAME}_FIND_REQUIRED_${comp})
set(${_NAME}_FOUND FALSE)
endif()
endif()
endforeach()
endmacro()

####################################################################################

# Make a list of all configurations 
# FIXME: Replace this with ${CMAKE_CONFIGURATION_TYPES}
set(CONFIGS "RELEASE" "DEBUG")

# Construct the executable names
set(shader_test_OUTPUT_NAME_RELEASE "shader_test")
set(shader_test_OUTPUT_NAME_DEBUG "shader_test_d")


# See if we can go the easy way when our dependency is also a target to be built
if (TARGET shader_test)
    set(shader_test_EXECUTABLE shader_test)
    set(shader_test_FOUND TRUE)
endif()

# See if the imported target already exists
if (TARGET shader_test_IMPORTED_${TARGET_POSTFIX})
    set(shader_test_EXECUTABLE shader_test_IMPORTED_${TARGET_POSTFIX})
    set(shader_test_FOUND TRUE)
endif()


# If the project is a target then it might not be built but we assume
# nevertheless that the files will be present. If it is not a target then
# we look for the libraries explicitly
if (NOT shader_test_FOUND)
	set(FIND_PATHS "${PACKAGE_PREFIX_DIR}/framework/lib/unix" 
				   "${PACKAGE_PREFIX_DIR}/framework/bin/unix" 
				   "${PACKAGE_PREFIX_DIR}/build/bin/unix"
				   "${PACKAGE_PREFIX_DIR}/build/lib/unix")
	
	# Try to find executables for all configurations
	foreach(conf ${CONFIGS})
		find_program(shader_test_EXE_${conf} ${shader_test_OUTPUT_NAME_${conf}} PATHS ${FIND_PATHS})
	endforeach()

	# Check if all program types could be found
	set(ALL_EXES_FOUND TRUE)
	foreach(conf ${CONFIGS})
		if (NOT shader_test_EXE_${conf})
			set(ALL_EXES_FOUND FALSE)
			break()
		endif()
	endforeach()
	
	# If some programs could not be found then substitute their name with the
	# first library name that could be found
	if (NOT ALL_EXES_FOUND)	
		set(FIRST_FOUND_NAME FALSE)
	
		foreach(conf ${CONFIGS})
			if(shader_test_EXE_${conf})
				set(FIRST_FOUND_NAME ${shader_test_EXE_${conf}})
				break()
			endif()
		endforeach()
		
		# If no program was found then we failed
		if (NOT FIRST_FOUND_NAME)
			# FIXME: add better error handling here
			message(FATAL_ERROR "Could not find program shader_test")
			set(shader_test_FOUND FALSE)
			return()
		endif()
		
		# Set all library names that were not found
		foreach(conf ${CONFIGS})
			if(NOT shader_test_EXE_${conf})
				set(shader_test_EXE_${conf} ${FIRST_FOUND_NAME})
			endif()
		endforeach()
	endif()

    # Generate an imported target which each location for different configurations set accordingly
    add_executable(shader_test_IMPORTED_${TARGET_POSTFIX} IMPORTED)
	foreach(conf ${CONFIGS})
		set_target_properties(shader_test_IMPORTED_${TARGET_POSTFIX} PROPERTIES
			IMPORTED_LOCATION_${conf} "${shader_test_EXE_${conf}}")
	endforeach()

    set(shader_test_EXECUTABLE shader_test_IMPORTED_${TARGET_POSTFIX})
    set(shader_test_FOUND TRUE)
endif()


file(RELATIVE_PATH SRC_NAME "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
set(LOG_BASE "${CMAKE_BINARY_DIR}/@BUILD_BASE@/log/shader/${SRC_NAME}")

macro(cgv_test_shaders target)
	if (NOT TEST_SHADERS_IGNORE)
		# Add a custom build rule for every file
		foreach (infile ${ARGN})
			shader_command_add(${target} "${infile}")
		endforeach()
	endif()
endmacro()



macro(shader_command_add target infile)

	get_filename_component(LOG_PATH "${LOG_BASE}/${infile}" PATH)
	get_filename_component(LOG_NAME "${infile}" NAME)

	get_filename_component(INFILE_ABS "${CMAKE_CURRENT_SOURCE_DIR}/${infile}" ABSOLUTE)

	set(outfile "${LOG_PATH}/${LOG_NAME}.log")

	if (NOT EXISTS "${LOG_PATH}")
		file(MAKE_DIRECTORY "${LOG_PATH}")
	endif()
	add_custom_target(shader_test_${LOG_NAME}
		DEPENDS "${INFILE_ABS}"
		SOURCES "${INFILE_ABS}"
		COMMAND ${shader_test_EXECUTABLE} "${INFILE_ABS}" "${outfile}"
		COMMENT "Testing shader code in ${LOG_NAME}")

	add_dependencies(${target} shader_test_${LOG_NAME})
endmacro()