cmake_minimum_required (VERSION 3.5)
project (sealcurses VERSION 2.0.18 LANGUAGES C)
include (GNUInstallDirs)

set (NCURSES_PACKAGE ncursesw CACHE STRING "Name of the ncurses pkg-config package")

find_package (the_Foundation REQUIRED)
find_package (PkgConfig REQUIRED)

set (CMAKE_C_STANDARD 11)

# Find Curses.
pkg_check_modules (CURSES ${NCURSES_PACKAGE}>=6)
if (NOT CURSES_FOUND)
    find_library (CURSES ncurses>=6)
    if (NOT CURSES)
        find_library (CURSES curses)
        if (NOT CURSES)
           message (FATAL_ERROR "Curses not found")
        endif () 
    endif () 
    message (STATUS "Using ${CURSES}")
    set (CURSES_LDFLAGS ${CURSES})
endif ()

foreach (opt ${CURSES_CFLAGS})
    set (CURSES_PC_CFLAGS "${CURSES_PC_CFLAGS} ${opt}")
endforeach ()
foreach (opt ${CURSES_LDFLAGS})
    set (CURSES_PC_LDFLAGS "${CURSES_PC_LDFLAGS} ${opt}")
endforeach ()
configure_file (sealcurses.pc.in ${CMAKE_BINARY_DIR}/sealcurses.pc @ONLY)

option (ENABLE_SHARED "Build a shared library" YES)
option (ENABLE_STATIC "Build a static library" YES)

set (HEADERS
    include/sealcurses/SDL.h
    include/sealcurses/SDL_clipboard.h
    include/sealcurses/SDL_events.h
    include/sealcurses/SDL_hints.h
    include/sealcurses/SDL_keyboard.h
    include/sealcurses/SDL_misc.h
    include/sealcurses/SDL_mouse.h
    include/sealcurses/SDL_render.h
    include/sealcurses/SDL_surface.h
    include/sealcurses/SDL_syswm.h
    include/sealcurses/SDL_timer.h
    include/sealcurses/SDL_types.h
    include/sealcurses/SDL_version.h
    include/sealcurses/SDL_video.h
)
set (SOURCES
    sealcurses.pc.in
    src/canvas.c
    src/canvas.h
    src/clipboard.c
    src/events.c
    src/events.h
    src/hints.c
    src/keyboard.c
    src/mouse.c
    src/render.c
    src/render.h
    src/seal.c
    src/surface.c
    src/timer.c
    src/timer.h
    src/video.c
    src/video.h
)

# This is useful when compiling as a CMake subdirectory.
if (ENABLE_STATIC)
    add_library (sealcurses-static STATIC ${SOURCES} ${HEADERS})
    set_target_properties (sealcurses-static PROPERTIES OUTPUT_NAME sealcurses)
    target_include_directories (sealcurses-static
        PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/sealcurses>
            $<INSTALL_INTERFACE:include/sealcurses>
        PRIVATE
            ${CURSES_INCLUDE_DIRS}
    )
    target_link_libraries (sealcurses-static
        PUBLIC
            the_Foundation::the_Foundation
            ${CURSES_LDFLAGS}
    )
    install (TARGETS sealcurses-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif ()

if (ENABLE_SHARED)
    add_library (sealcurses SHARED ${SOURCES} ${HEADERS})
    set_target_properties (sealcurses PROPERTIES
        VERSION   ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    )
    target_include_directories (sealcurses
        PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/sealcurses>
            $<INSTALL_INTERFACE:include/sealcurses>
        PRIVATE
            ${CURSES_INCLUDE_DIRS}
    )
    target_link_libraries (sealcurses
        PRIVATE
            the_Foundation::the_Foundation
            ${CURSES_LDFLAGS}
    )
    install (TARGETS sealcurses DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif ()

install (FILES ${HEADERS} DESTINATION include/sealcurses)
install (FILES ${CMAKE_BINARY_DIR}/sealcurses.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
