function(normalize_elise_files source_files)
	foreach(source_file ${source_files})
		string(FIND ${source_file} "${CMAKE_SOURCE_DIR}" pos)
		if (NOT ${pos} EQUAL -1)
			set(absolute_source_file "${source_file}")
			string(REPLACE "${CMAKE_SOURCE_DIR}/" "" source_file ${absolute_source_file})
		else()
			set(absolute_source_file "${CMAKE_SOURCE_DIR}/src/${source_file}")
		endif()
		#~ message(${absolute_source_file})
		list(APPEND normalized_files ${absolute_source_file}) 
	endforeach()
	set(Elise_Src_Files ${normalized_files} PARENT_SCOPE)
endfunction()

function(append_source_file source_output_file source_file compile_flags)
	get_filename_component(source_name ${source_file} NAME)
	#~ get_filename_component(absolute_source_file ${source_file} ABSOLUTE)
	string(REPLACE "${CMAKE_SOURCE_DIR}/" "" source_name "${source_file}")
	string(REPLACE "/" "_" source_name "${source_name}")
	set(object_file "obj/${source_name}.o")

	if (source_file MATCHES ".*.c$")
		set(compiler "$(CC)")
	else()
		set(compiler "$(CXX)")
	endif()

	file(APPEND ${source_output_file} "${object_file}: ${source_file} $(micmac_dir)/include/StdAfx.h.gch\n")
	file(APPEND ${source_output_file} "\t${compiler} -c ${compile_flags} -o ${object_file} ${source_file}\n")

	set(OBJECT_FILES ${OBJECT_FILES} ${object_file} PARENT_SCOPE)
endfunction()

function(write_source_files output_filename group_name source_files compile_flags)
	unset(OBJECT_FILES)
	foreach (src ${source_files})
		append_source_file("${output_filename}" "${src}" "${compile_flags}")
	endforeach()
	file(APPEND ${output_filename} "\n")

	file(APPEND ${output_filename} "${group_name}_object_files =")
	foreach(object_file ${OBJECT_FILES})
		file(APPEND ${output_filename} " ${object_file}")
	endforeach()
	file(APPEND ${output_filename} "\n\n")
endfunction()

set(elise_files_filename ${CMAKE_BINARY_DIR}/elise_files)
set(elise_objects_filename ${CMAKE_BINARY_DIR}/elise_object_files)

file(WRITE ${elise_files_filename} "micmac_dir = ${CMAKE_SOURCE_DIR}\n")
file(APPEND ${elise_files_filename} "build_dir = ${CMAKE_BINARY_DIR}\n")
file(APPEND ${elise_files_filename} "kakadu_dir = ${KAKADU_DIR}\n")
file(APPEND ${elise_files_filename} "precompiled_header = ${precompiled_header}\n")

if (WITH_QT5)
	set(QT_INCLUDE_DIR ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Core_INCLUDE_DIRS} ${Qt5Concurrent_INCLUDE_DIRS} ${Qt5OpenGL_INCLUDE_DIRS} ${Qt5Xml_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS})
	list(REMOVE_DUPLICATES QT_INCLUDE_DIR)

	list(APPEND QT_DEFINITIONS ${Qt5Widgets_DEFINITIONS} ${Qt5Xml_DEFINITIONS} ${Qt5Concurrent_DEFINITIONS} ${Qt5OpenGL_DEFINITIONS})
	list(REMOVE_DUPLICATES QT_DEFINITIONS)
endif()

file(APPEND ${elise_files_filename} "qt_includes =")
foreach(f ${QT_INCLUDE_DIR})
	file(APPEND ${elise_files_filename} " -I${f}")
endforeach()
file(APPEND ${elise_files_filename} " -Iui")
file(APPEND ${elise_files_filename} "\n")

file(APPEND ${elise_files_filename} "qt_qmake = ${QT_QMAKE_EXECUTABLE}\n")
file(APPEND ${elise_files_filename} "qt_moc = ${QT_MOC_EXECUTABLE}\n")
file(APPEND ${elise_files_filename} "qt_uic = ${QT_UIC_EXECUTABLE}\n")
file(APPEND ${elise_files_filename} "qt_rcc = ${QT_RCC_EXECUTABLE}\n")
file(APPEND ${elise_files_filename} "qt_compile_flags = ${qt_compile_flags}\n")

file(APPEND ${elise_files_filename} "qt_generated_files =")
foreach(f ${saisie_ui})
	get_filename_component(name ${f} NAME)
	file(APPEND ${elise_files_filename} " ui/${name}")
endforeach()
file(APPEND ${elise_files_filename} "\n")

set(qt_generated_files )

file(APPEND ${elise_files_filename} "qt_definitions =")
foreach(definition ${QT_DEFINITIONS})
	file(APPEND ${elise_files_filename} " ${definition}")
endforeach()
file(APPEND ${elise_files_filename} "\n")

file(APPEND ${elise_files_filename} "core_definitions =")
foreach(flag ${compile_flags})
	string(FIND ${flag} "QT_" pos)
	if (NOT ${pos} EQUAL 0)
		file(APPEND ${elise_files_filename} " -D${flag}")
	endif()
endforeach()
file(APPEND ${elise_files_filename} "\n\n")

normalize_elise_files("${Elise_Src_Files}")
list(REMOVE_DUPLICATES Elise_Src_Files)
write_source_files(${elise_files_filename} "elise" "${Elise_Src_Files}" "$(ALL_FLAGS)")
write_source_files(${elise_files_filename} "ANN" "${ann_source_files}" "$(ANN_FLAGS)")

# Qt moc files
foreach (infile ${HEADERS_tomoc})
	get_filename_component(inbase ${infile} NAME)
	set(outfile "moc/${inbase}.cpp")
	file(APPEND ${elise_files_filename} "${outfile}: ${infile}\n")
	file(APPEND ${elise_files_filename} "\t$(qt_moc) -I${CMAKE_SOURCE_DIR}/include -o ${outfile} ${infile}\n")
	list(APPEND elise_qt_files ${outfile})
endforeach()
file(APPEND ${elise_files_filename} "\n")

# Qt UI files
foreach (infile ${ui_toWrap})
	get_filename_component(inbase ${infile} NAME_WE)
	set(outfile "ui/ui_${inbase}.h")
	file(APPEND ${elise_files_filename} "${outfile}: ${infile}\n")
	file(APPEND ${elise_files_filename} "\t$(qt_uic) -o ${outfile} ${infile}\n")
endforeach()
file(APPEND ${elise_files_filename} "\n")

# Qt ressource files
foreach (rc_file ${qt_ressource_files})
	get_filename_component(name ${rc_file} NAME_WE)
	get_filename_component(basename ${rc_file} NAME)
	set(cpp_name "rc/${basename}.cpp")
	file(APPEND ${elise_files_filename} "${cpp_name}: ${rc_file}\n")
	file(APPEND ${elise_files_filename} "\t$(qt_rcc) -name ${name} -o ${cpp_name} ${rc_file}\n")
	list(APPEND elise_qt_files ${cpp_name})
endforeach()
file(APPEND ${elise_files_filename} "\n")

write_source_files(${elise_files_filename} "elise_qt" "${elise_qt_files}" "$(ALL_FLAGS)")

# building SaisieQT
if(${WITH_QT5} OR ${WITH_QT4})
	write_source_files(${elise_files_filename} "saisieqt" "${saisieqt_files}" "$(ALL_FLAGS)")
	file(APPEND ${elise_files_filename} "bin/SaisieQT: $(saisieqt_object_files)\n")
	file(APPEND ${elise_files_filename} "\t$(CXX) $(saisieqt_object_files) -o bin/SaisieQT $(ALL_LIB_FLAGS)\n")
endif()
