# Add all subdirectories to the build
file(GLOB plugins RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/*")

set(baseline "plain")
if(WITH_AUTH)
    foreach(_base ${baseline})
        if(NOT ("${_base}" IN_LIST WITH_AUTH))
            message(WARNING "The ${_base} authenticator cannot be skipped, "
                    "it will be compiled anyway.")
        endif()
    endforeach()

    foreach(_plugin ${plugins})
        if(NOT ("${_plugin}" IN_LIST WITH_AUTH)
            AND NOT ("${_plugin}" IN_LIST baseline)
            AND IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_plugin}")
                list(REMOVE_ITEM plugins ${_plugin})
        endif()
    endforeach()
else()
    foreach(_plugin ${plugins})
        if(NOT ("${_plugin}" IN_LIST baseline))
            list(REMOVE_ITEM plugins ${_plugin})
        endif()
    endforeach()
endif()

# Set unique plugin directory variable for each plugin.
message(STATUS "Found the following authentication plugins:")
foreach(_plugin ${plugins})
    if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_plugin}")
        set(${_plugin}_PLUGIN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${_plugin}")
        set(${_plugin}_PLUGIN_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_plugin}")
        message(STATUS "  ${_plugin}")
    endif()
endforeach(_plugin)

# Add plugins.
foreach(_plugin ${plugins})
    if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_plugin}")
        set(PLUGIN_NAME ${_plugin})
        set(PLUGIN_DIR ${${_plugin}_PLUGIN_DIR})
        set(PLUGIN_BINARY_DIR ${${_plugin}_PLUGIN_BINARY_DIR})
        fancy_message("Loading authentication plugin ${_plugin}" "blue" TRUE)
        add_subdirectory(${_plugin})
    endif()
endforeach(_plugin)
