file(GLOB _files "${CMAKE_CURRENT_SOURCE_DIR}/*")
foreach (_dir ${_files})
  if (IS_DIRECTORY "${_dir}" AND EXISTS "${_dir}/CMakeLists.txt")
    add_subdirectory("${_dir}")
    get_filename_component(_lang ${_dir} NAME)
    list(APPEND _langs ${_lang})
  endif ()
endforeach()


macro(UPDATE_POT_FILE input)
  set(_potFile "${input}.pot")
  foreach(_lang ${_langs})
    set(_poFile "${CMAKE_CURRENT_SOURCE_DIR}/${_lang}/${input}.po")
    list(APPEND _commands
      COMMAND ${GETTEXT_MSGMERGE_EXECUTABLE} --quiet --update --backup=none -F ${_poFile} "${CMAKE_CURRENT_SOURCE_DIR}/${_potFile}")
  endforeach()

  set(COPY_NEW_POTFILE ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${_potFile}.new ${CMAKE_CURRENT_SOURCE_DIR}/${_potFile})
  if (NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows"))
    # IF the new potfile carries changes other than an updated POT-creation-date (autogenerated) THEN proceed to use that new potfile
    set(CM diff --brief --ignore-matching-lines="POT-Creation-Date.*" ${CMAKE_CURRENT_SOURCE_DIR}/${_potFile} ${CMAKE_CURRENT_BINARY_DIR}/${_potFile}.new || ${COPY_NEW_POTFILE})
  else ()
    set(CM ${COPY_NEW_POTFILE})
  endif ()

  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_potFile}.new
                     COMMAND xgettext ${input}.cc --package-name=${input} --keyword=_ -F --output="${CMAKE_CURRENT_BINARY_DIR}/${_potFile}.new"
                     COMMAND ${CM}
                     ${_commands}
                     DEPENDS ../${input}.cc
                     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
  add_custom_target(update_pot_${input}
                    ALL
                    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_potFile}.new)
endmacro()

UPDATE_POT_FILE(pdfsig)
