include_directories(
    ${CMAKE_SOURCE_DIR}/src
)

# --- YAML BACKEND (Corrected) ---
find_package(YAML)

# The conditional check needs a standard CMake variable check, not a dereference.
if(YAML_FOUND) 
    set( yamlconfsrc "yaml/yamlconf.cpp")
    add_library(yamlconf SHARED ${yamlconfsrc})

    # REMOVED: add_dependencies(yamlconf confplus) -> Linking handles this.

    set_target_properties(yamlconf PROPERTIES PREFIX "" OUTPUT_NAME "yaml.cfg")

    # REMOVED: target_include_directories(yamlconf PRIVATE ${YAML_INCLUDE_DIR}) 
    # The modern target 'yaml::yaml' handles the includes automatically.

    if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
        # Ensure 'confplus' is linked as PRIVATE unless its interface is exposed
        target_link_libraries(yamlconf PRIVATE confplus yaml::yaml kernel32.lib)
    else()
        target_link_libraries(yamlconf PRIVATE confplus yaml::yaml dl)
    endif()
    
    install(TARGETS yamlconf
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/confplus/backend NAMELINK_SKIP
    )
endif()

# --- INIPARSER BACKEND (Corrected) ---
find_package(INIPARSER)

if(INIPARSER_FOUND)
    set( inisrc ini/ini.cpp)
    add_library(iniconf SHARED ${inisrc})

    set_target_properties(iniconf PROPERTIES PREFIX "" OUTPUT_NAME "ini.cfg")

    # Using modern target (INIPARSER::INIPARSER) for linking automatically pulls in includes.
    # The classic variable ${INIPARSER_INCLUDE_DIRS} is deprecated/obsolete when using modern targets.
    # We remove target_include_directories and rely on INIPARSER::INIPARSER.
    
    if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
        target_link_libraries(iniconf PRIVATE confplus iniparser::iniparser kernel32.lib)
    else()
        target_link_libraries(iniconf PRIVATE confplus iniparser::iniparser dl)
    endif()

    install(TARGETS iniconf
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/confplus/backend NAMELINK_SKIP
    )
endif()

set(REGEDIT_FOUND FALSE CACHE BOOL "Windows Reg Support")

if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    set(REGEDIT_FOUND TRUE)
    set(regeditsrc regedit/regedit.cpp)
    
    add_library(regedit SHARED ${regeditsrc})

    set_target_properties(regedit PROPERTIES PREFIX "" OUTPUT_NAME "regedit.cfg")

    target_link_libraries(regedit PRIVATE confplus kernel32.lib Advapi32.lib)

    install(TARGETS regedit
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
endif()


if(NOT YAML_FOUND AND NOT INIPARSER_FOUND AND NOT REGEDIT_FOUND)
    message(ERROR "No backends are found Fatal!")
endif()
