
####### 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(res_prep_OUTPUT_NAME_RELEASE "res_prep")
set(res_prep_OUTPUT_NAME_DEBUG "res_prep_d")


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

# See if the imported target already exists
if (TARGET res_prep_IMPORTED_${TARGET_POSTFIX})
    set(res_prep_EXECUTABLE res_prep_IMPORTED_${TARGET_POSTFIX})
    set(res_prep_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 res_prep_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(res_prep_EXE_${conf} ${res_prep_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 res_prep_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(res_prep_EXE_${conf})
				set(FIRST_FOUND_NAME ${res_prep_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 res_prep")
			set(res_prep_FOUND FALSE)
			return()
		endif()
		
		# Set all library names that were not found
		foreach(conf ${CONFIGS})
			if(NOT res_prep_EXE_${conf})
				set(res_prep_EXE_${conf} ${FIRST_FOUND_NAME})
			endif()
		endforeach()
	endif()

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

    set(res_prep_EXECUTABLE res_prep_IMPORTED_${TARGET_POSTFIX})
    set(res_prep_FOUND TRUE)
endif()


macro(cgv_prepare_resources base outfiles)

	# Add a custom build rule for every file
	foreach (infile ${ARGN})
		_res_prep_command_add("${base}" "${infile}" outfile)
		set(${outfiles} "${${outfiles}}" "${outfile}")
	endforeach()

	# Add the include directory 
	include_directories("${RP_BASE}")
endmacro()


macro(_res_prep_command_add base infile outfile)

	# Generate the output name
	set(RP_BASE "${CMAKE_CURRENT_BINARY_DIR}")

	get_filename_component(RP_ABS "${infile}" ABSOLUTE)
	file(RELATIVE_PATH RP_REL "${base}" "${RP_ABS}")

	if (IS_ABSOLUTE "${RP_REL}")
		message(FATAL_ERROR "The res_prep input file ${infile} was explicitely based to ${${PROJECT_NAME}_RP_BASE} which is not a file base!")
	endif()

	get_filename_component(RP_PATH "${RP_BASE}/${RP_REL}" PATH)		

	get_filename_component(RP_NAME "${infile}" NAME_WE)

	# Create the output directory if it does not exist
	if (NOT EXISTS "${RP_PATH}")
		file(MAKE_DIRECTORY "${RP_PATH}")
	endif()

	get_filename_component(${outfile} "${RP_PATH}/${RP_NAME}.cxx" ABSOLUTE)

	# FIXME: Can we really do this?
	include_directories(
		"${RP_BASE}"
		"${RP_PATH}")

	# Add the build rule
	add_custom_command(OUTPUT ${${outfile}}
		COMMAND ${res_prep_EXECUTABLE}
		ARGS "${CMAKE_CURRENT_SOURCE_DIR}/${infile}" "${${outfile}}"
		DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${${infile}}")
endmacro()



