# For each library, fuses all sources to a single .cc file.
#----------------------------------------------------------

# Start with new content
string(CONCAT output_content "" "// This file is autogenerated. Any modification might be lost.\n\n")

# Patches arguments so that cmake take them as a list.
separate_arguments(ozz_target_source_files)

string(REPLACE "\\ " " " ozz_fuse_target_dir "${ozz_fuse_target_dir}")
string(REPLACE "\\ " " " ozz_fuse_src_dir "${ozz_fuse_src_dir}")
string(REPLACE "\\ " " " ozz_fuse_output_file "${ozz_fuse_output_file}")

# Concat all sources to the output.
foreach(src_file ${ozz_target_source_files})

  get_filename_component(absolute_src_file ${src_file} ABSOLUTE BASE_DIR ${ozz_fuse_target_dir})
  get_filename_component(src_file_ext ${absolute_src_file} EXT)

  # Handle source files.
  if(src_file_ext STREQUAL ".cc")
  
    file(READ "${absolute_src_file}" src_file_content)

    string(CONCAT output_content "${output_content}" "// Including ${src_file} file.\n\n")
    string(CONCAT output_content "${output_content}" "${src_file_content}\n")

  endif()
endforeach()

# Handle private include files (they are not prefixed by "ozz/").
string(REGEX MATCHALL "#include \"[\^\"ozz\"]([^\"]+)\"" internal_include_lines ${output_content})

foreach(internal_include_line ${internal_include_lines})

  STRING(REGEX REPLACE "#include \"([^\"]+)\"" "\\1" internal_include_file "${internal_include_line}" )

  get_filename_component(internal_src_file src/${internal_include_file} ABSOLUTE BASE_DIR ${ozz_fuse_src_dir})
  file(READ "${internal_src_file}" internal_src_file_content)

  string(REPLACE "${internal_include_line}" "\n// Includes internal include file ${internal_include_file}\n\n${internal_src_file_content}" output_content "${output_content}")

endforeach()

# Output new file
file(WRITE "${ozz_fuse_output_file}" "${output_content}")