
####### 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(cgv_viewer_OUTPUT_NAME_RELEASE "cgv_viewer")
set(cgv_viewer_OUTPUT_NAME_DEBUG "cgv_viewer_d")


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

# See if the imported target already exists
if (TARGET cgv_viewer_IMPORTED_${TARGET_POSTFIX})
    set(cgv_viewer_EXECUTABLE cgv_viewer_IMPORTED_${TARGET_POSTFIX})
    set(cgv_viewer_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 cgv_viewer_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(cgv_viewer_EXE_${conf} ${cgv_viewer_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 cgv_viewer_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(cgv_viewer_EXE_${conf})
				set(FIRST_FOUND_NAME ${cgv_viewer_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 cgv_viewer")
			set(cgv_viewer_FOUND FALSE)
			return()
		endif()
		
		# Set all library names that were not found
		foreach(conf ${CONFIGS})
			if(NOT cgv_viewer_EXE_${conf})
				set(cgv_viewer_EXE_${conf} ${FIRST_FOUND_NAME})
			endif()
		endforeach()
	endif()

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

    set(cgv_viewer_EXECUTABLE cgv_viewer_IMPORTED_${TARGET_POSTFIX})
    set(cgv_viewer_FOUND TRUE)
endif()


get_filename_component(BASE_PATH "${CMAKE_CURRENT_LIST_FILE}" PATH)

include("${BASE_PATH}/../CMakeMacroParseArguments.cmake")


function(cgv_add_viewer name)
	# We only support linux and shared library builds
	if (WIN32)
		message("Viewer starters can only be generated under Linux at the moment")
		return()
	endif()

	if (NOT BUILD_SHARED_LIBS)
		message("Can only create a viewer for shared libraries at the moment")
		return()
	endif()

	_cgv_add_shared_viewer(${name} ${ARGN}) 
endfunction()




function(_cgv_add_shared_viewer name)

	set(CONFIG ${CMAKE_BUILD_TYPE})
	if (NOT CONFIG)
		set(CONFIG "Release")
	endif()

	cmake_parse_arguments(VDEFS "" "" "PLUGINS;ARGS" ${ARGN})

	# If the command was not invoked with PLUGINS and CMDLINE sub lists
	# then treat all arguments as plugins
	if (NOT VDEFS_PLUGINS AND NOT VDEFS_ARGS)
		set(VDEFS_PLUGINS ${ARGN})
		set(VDEFS_ARGS "")
	endif()

	# Collect all library targets and definitions for all plugins
	set(LIBS "")
	foreach (mod ${VDEFS_PLUGINS})
		# Check the according member if the plugin is a target 
		if (TARGET ${mod})
			set(LIBS ${LIBS} ${mod})
			get_target_property(MOD_LIB_SHARED ${mod} _SHARED_DEFINITIONS)
		# Otherwise find the package
		else()
			find_package(${mod})
			if (${mod}_FOUND)
				set(LIBS ${LIBS} "${${mod}_LIBRARIES}")
			else()
				message(FATAL_ERROR "A project requested to create a viewer. The required module ${mod} could not be found!")
			endif()
		endif()
	endforeach()


	set(LIB_FILES "")
	set(LIB_PATHS "")

	# Find the file name of the library targets and construct a command line
	# Also find the paths which we need for LD_LIBRARY_PATH later
	foreach(lib ${LIBS})
		get_target_property(LOC ${lib} LOCATION_${CONFIG})
		set(LIB_FILES "${LIB_FILES} plugin:\"${LOC}\"")

		get_filename_component(ABS_LOC_PATH "${LOC}" PATH)
		list(APPEND LIB_PATHS ${ABS_LOC_PATH})
	endforeach()
	list(REMOVE_DUPLICATES LIB_PATHS)
	string(REPLACE ";" ":" LIB_PATHS "${LIB_PATHS}")

	# Find the name of the cgv viewer
	get_target_property(VIEWER_EXE ${cgv_viewer_EXECUTABLE} LOCATION_${CONFIG})

	configure_file(${BASE_PATH}/launcher.sh.in
				   ${CMAKE_BINARY_DIR}/${name}.sh
				   @ONLY)

endfunction()



