@PACKAGE_INIT@

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

# Construct the executable names
set(@TARGET@_OUTPUT_NAME_RELEASE "@OUTPUT_NAME@")
set(@TARGET@_OUTPUT_NAME_DEBUG "@OUTPUT_NAME@@DEBUG_POSTFIX@")


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

# See if the imported target already exists
if (TARGET @TARGET@_IMPORTED_${TARGET_POSTFIX})
    set(@TARGET@_EXECUTABLE @TARGET@_IMPORTED_${TARGET_POSTFIX})
    set(@TARGET@_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 @TARGET@_FOUND)
	set(FIND_PATHS "@PACKAGE_LIB_PATH@" 
				   "@PACKAGE_BIN_PATH@" 
				   "@PACKAGE_BUILD_BIN_PATH@"
				   "@PACKAGE_BUILD_LIB_PATH@")
	
	# Try to find executables for all configurations
	foreach(conf ${CONFIGS})
		find_program(@TARGET@_EXE_${conf} ${@TARGET@_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 @TARGET@_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(@TARGET@_EXE_${conf})
				set(FIRST_FOUND_NAME ${@TARGET@_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 @OUTPUT_NAME@")
			set(@TARGET@_FOUND FALSE)
			return()
		endif()
		
		# Set all library names that were not found
		foreach(conf ${CONFIGS})
			if(NOT @TARGET@_EXE_${conf})
				set(@TARGET@_EXE_${conf} ${FIRST_FOUND_NAME})
			endif()
		endforeach()
	endif()

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

    set(@TARGET@_EXECUTABLE @TARGET@_IMPORTED_${TARGET_POSTFIX})
    set(@TARGET@_FOUND TRUE)
endif()


@MACRO_DEFINITIONS@

