cmake_minimum_required(VERSION 3.8)
project(librw
    VERSION 0.0.1
    LANGUAGES C CXX
)
set(librw_AUTHOR aap)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

if(WIN32)
    set(LIBRW_PLATFORMS "NULL" "GL3" "D3D9")
    set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL ON)
elseif(NINTENDO_SWITCH)
    set(LIBRW_PLATFORMS "NULL" "GL3")
    set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL OFF)
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/nx")
    include(NXFunctions)
elseif(PS2)
    set(LIBRW_PLATFORMS "NULL" "PS2")
    set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL OFF)
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/ps2")
    include(PS2Functions)
else()
    set(LIBRW_PLATFORMS "NULL" "GL3")
    set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL ON)
endif()
set(LIBRW_PLATFORM "NULL" CACHE STRING "Platform")
set_property(CACHE LIBRW_PLATFORM PROPERTY STRINGS ${LIBRW_PLATFORMS})
message(STATUS "LIBRW_PLATFORM = ${LIBRW_PLATFORM} (choices=${LIBRW_PLATFORMS})")
set("LIBRW_PLATFORM_${LIBRW_PLATFORM}" ON)
if(NOT LIBRW_PLATFORM IN_LIST LIBRW_PLATFORMS)
    message(FATAL_ERROR "Illegal LIBRW_PLATFORM=${LIBRW_PLATFORM}")
endif()

set(LIBRW_GL3_GFXLIBS "GLFW" "SDL2")
set(LIBRW_GL3_GFXLIB "GLFW" CACHE STRING "gfxlib for gl3")
set_property(CACHE LIBRW_GL3_GFXLIB PROPERTY STRINGS ${LIBRW_GL3_GFXLIBS})
if(LIBRW_PLATFORM_GL3)
    message(STATUS "LIBRW_GL3_GFXLIB = ${LIBRW_GL3_GFXLIB} (choices=${LIBRW_GL3_GFXLIBS})")
endif()
if(NOT LIBRW_GL3_GFXLIB IN_LIST LIBRW_GL3_GFXLIBS)
    message(FATAL_ERROR "Illegal LIBRW_GL3_GFXLIB=${LIBRW_GL3_GFXLIB}")
endif()

if(LIBRW_PLATFORM_PS2)
    enable_language(DSM)
endif()

if(NOT COMMAND librw_platform_target)
    function(librw_platform_target)
    endfunction()
endif()

option(LIBRW_TOOLS "Build librw tools" ON)
option(LIBRW_INSTALL "Install librw files" OFF)

if(LIBRW_INSTALL)
    include(GNUInstallDirs)
    set(LIBRW_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/librw")
endif()

add_subdirectory(src)

if(LIBRW_TOOLS)
    add_subdirectory(skeleton)

    add_subdirectory(tools)
endif()

if(LIBRW_INSTALL)
    include(CMakePackageConfigHelpers)
    configure_package_config_file(cmake/librw-config.cmake.in librw-config.cmake
        INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}"
    )
    install(
        FILES "${CMAKE_CURRENT_BINARY_DIR}/librw-config.cmake"
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
    )
    install(
        EXPORT librw-targets NAMESPACE librw::
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
    )

    if(LIBRW_GL3_GFXLIB STREQUAL "SDL2")
        install(
            FILES "${CMAKE_CURRENT_LIST_DIR}/cmake/FindSDL2.cmake"
            DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake"
        )
    endif()
    if(NINTENDO_SWITCH)
        install(
            FILES "${CMAKE_CURRENT_LIST_DIR}/cmake/nx/FindNXGL.cmake"
            DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake"
        )
    endif()

    string(REPLACE "." ";" cmake_c_compiler_version_list "${CMAKE_C_COMPILER_VERSION}")
    list(GET cmake_c_compiler_version_list 0 cmake_c_compiler_version_major)

    string(TOLOWER "${LIBRW_PLATFORM}" librw_platform)
    set(compiler)
    set(os)
    if(NOT LIBRW_PLATFORM STREQUAL "PS2")
        if(MSVC)
            set(compiler "-msvc${MSVC_VERSION}")
        elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
            set(compiler "-gcc${cmake_c_compiler_version_major}")
        elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
            set(compiler "-clang${cmake_c_compiler_version_major}")
        elseif(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
            set(compiler "-appleclang${cmake_c_compiler_version_major}")
        else()
            set(compiler "-UNK")
            message(WARNING "Unknown compiler. Created cpack package will be wrong. (override using cpack -P)")
        endif()
    endif()
    if(LIBRW_PLATFORM_NULL)
        set(platform "-null")
    elseif(LIBRW_PLATFORM_PS2)
        set(platform "-ps2")
    elseif(LIBRW_PLATFORM_GL3)
        if(LIBRW_GL3_GFXLIB STREQUAL "GLFW")
            set(platform "-gl3-glfw")
        else()
            set(platform "-gl3-sdl2")
        endif()
    elseif(LIBRW_PLATFORM_D3D9)
        set(platform "-d3d9")
    endif()
    if(NOT LIBRW_PLATFORM_PS2)
        if(WIN32)
            set(os "-win")
        elseif(APPLE)
            set(os "-apple")
        elseif(UNIX)
            set(os "-linux")
        elseif(NINTENDO_SWITCH)
            set(os "-switch")
        else()
            set(compiler "-UNK")
            message(WARNING "Unknown os. Created cpack package will be wrong. (override using cpack -P)")
        endif()
    endif()

    set(CPACK_PACKAGE_NAME "${PROJECT_NAME}${platform}${os}${compiler}")
    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A (partial) re-implementation of RenderWare Graphics")
    set(CPACK_PACKAGE_VENDOR "aap")
    set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/LICENSE")
    set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}")
    set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}")
    set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}")
    set(CPACK_GENERATOR "TXZ")
    include(CPack)
endif()
