include_guard(GLOBAL)

# include_macro_recurse [FILTER filter] | [dir1 [dir2 [...]]]
macro(include_macro_recurse)
  set(INCLUDE_MACRO_RECURSE_FILTER "*.macro.cmake")
  set(INCLUDE_MACRO_RECURSE_FILTER_FLAG FALSE)

  foreach(basedir ${ARGN})
    if("${basedir}" STREQUAL "FILTER")
      set(INCLUDE_MACRO_RECURSE_FILTER_FLAG TRUE)
    elseif(INCLUDE_MACRO_RECURSE_FILTER_FLAG)
      set(INCLUDE_MACRO_RECURSE_FILTER_FLAG FALSE)
      set(INCLUDE_MACRO_RECURSE_FILTER "${basedir}")
    else()
      file(GLOB_RECURSE ALL_MACRO_FILES "${basedir}/${INCLUDE_MACRO_RECURSE_FILTER}")
      foreach(macro_file ${ALL_MACRO_FILES})
        message(STATUS "Macro File Found: ${macro_file}")
        include("${macro_file}")
      endforeach()
    endif()
  endforeach()
  unset(INCLUDE_MACRO_RECURSE_FILTER)
  unset(INCLUDE_MACRO_RECURSE_FILTER_FLAG)
endmacro()

# source_group_by_dir [Source List Var1 [Source List Var2 [...]]]
function(source_group_by_dir)
  if(MSVC)
    foreach(SOURCE_FILE ${${ARGN}})
      if(IS_ABSOLUTE "${SOURCE_FILE}")
        file(RELATIVE_PATH sgbd_fpath ${CMAKE_CURRENT_SOURCE_DIR} ${SOURCE_FILE})
      else()
        set(sgbd_fpath "${SOURCE_FILE}")
      endif()
      string(REGEX REPLACE "\(.*\)/.*" \\1 sgbd_group_name ${sgbd_fpath})
      string(COMPARE EQUAL ${sgbd_fpath} ${sgbd_group_name} sgbd_nogroup)
      string(REPLACE "/" "\\" sgbd_group_name ${sgbd_group_name})
      if(sgbd_nogroup)
        set(sgbd_group_name "\\")
      endif(sgbd_nogroup)
      source_group(${sgbd_group_name} FILES ${SOURCE_FILE})
    endforeach()
  endif()
endfunction()
