file( GLOB _dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} * )
list( REMOVE_ITEM _dirs html man )

string( REGEX REPLACE "[ \r\n\t]+" ";" _linguas "$ENV{LINGUAS}" )

foreach( _dir IN LISTS _dirs )
  if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${_dir}
      AND ( "${_dir}" STREQUAL "en" OR
            "${_linguas}" MATCHES "^;*$" OR
            ";${_linguas};" MATCHES ";${_dir};" ))
    file( GLOB _doc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/${_dir} ${_dir}/*.docbook )
    if( _doc_files )
      list( FIND _doc_files "index.docbook" _find_index )
      if( -1 EQUAL _find_index )
        set( _noindex "NOINDEX" )
      else()
        unset( _noindex )
      endif()
      tde_create_handbook(
        SOURCE_BASEDIR ${_dir}
        ${_noindex}
        LANG ${_dir}
        DESTINATION ${PROJECT_NAME}
      )
    endif()
  endif()
endforeach()

if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/html/CMakeLists.txt )
  add_subdirectory( html )
endif()
if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/man/CMakeLists.txt )
  add_subdirectory( man )
endif()
