# -- C library --------------

# define list of public headers
set(LIBFRAME_HEADERS FrameL.h FrFilter.h FrIO.h FrVect.h)
# and make this available to other parts of the build
set(LIBFRAME_HEADERS ${LIBFRAME_HEADERS} PARENT_SCOPE)

if (NOT DEFINED ENABLE_C OR ENABLE_C)

    set(LIBFRAME_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

    add_library(
        framel SHARED
        FrFilter.c FrIO.c FrameL.c
        zlib/Fradler32.c zlib/Frcompress.c zlib/Frcrc32.c
        zlib/Frdeflate.c zlib/Frinfblock.c zlib/Frinfcodes.c
        zlib/Frinffast.c zlib/Frinflate.c zlib/Frinftrees.c
        zlib/Frinfutil.c zlib/Frtrees.c zlib/Fruncompr.c
        zlib/Frzutil.c
    )
    target_include_directories(framel PRIVATE zlib)
    if (NOT WIN32)
        target_link_libraries(framel PRIVATE m)
    endif (NOT WIN32)
    set_target_properties(
        framel PROPERTIES
        VERSION ${LIBFRAME_VERSION_STRING}
        SOVERSION ${PROJECT_VERSION_MAJOR}
        PUBLIC_HEADER "${LIBFRAME_HEADERS}"
    )

    install(
        TARGETS framel
        RUNTIME DESTINATION ${RUNTIME_DESTINATION}
        LIBRARY DESTINATION ${LIBRARY_DESTINATION}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )

    foreach (app FrChannels FrCheck FrCopy FrDiff FrDump FrTrend FrameDataDump)
	    add_executable(${app} ${app}.c)
        target_link_libraries(${app} PRIVATE framel)
        if (NOT WIN32)
            target_link_libraries(${app} PRIVATE m)
        endif (NOT WIN32)
	    install(TARGETS ${app} DESTINATION ${CMAKE_INSTALL_BINDIR})
    endforeach(app)

    configure_file(
        "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in"
        "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
        @ONLY
    )
    install(
        FILES ${CMAKE_CURRENT_BINARY_DIR}/framel.pc
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
    )

    # -- testing

    set(FRAMEL_TESTS "")
    if (BUILD_TESTING)
        set(
            FRAMEL_TESTS
            Complex
            Compress
            CopyFile
            CopyFrame
            Event
            FileDump
            Filter
            Full
            IInMemory
            MultiR
            MultiTOC
            MultiW
            MultiWD
            Reshape
            SelectFrame
            Speed
            Stat
        )
        if (ENABLE_SLOW_TESTS)
            list(
                APPEND
                FRAMEL_TESTS
                Mark
                Online
            )
        endif (ENABLE_SLOW_TESTS)
    endif (BUILD_TESTING)

    foreach (tag ${FRAMEL_TESTS})
        add_executable(Fr${tag} example${tag}.c)
        target_link_libraries(Fr${tag} framel)
        target_include_directories(Fr${tag} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
        if (NOT WIN32)
            target_link_libraries(Fr${tag} m)
        endif (NOT WIN32)
        add_test(
          NAME ${tag}
          COMMAND $<TARGET_FILE:Fr${tag}>
        )
    endforeach(tag)

    # test dependencies
    if ("Mark" IN_LIST FRAMEL_TESTS)
        set_tests_properties(Mark PROPERTIES DEPENDS Full)
    endif()

endif()
