cmake_minimum_required(VERSION 3.20)

project(prozilla VERSION 2.2.0 LANGUAGES C CXX)

include(GNUInstallDirs)
include(CheckIncludeFile)
include(CheckFunctionExists)

find_package(Threads REQUIRED)
find_package(Curses REQUIRED)
find_package(Intl)
find_package(libprozilla CONFIG REQUIRED)

option(PROZILLA_BUILD_CLI "Build the proz command-line client" ON)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

foreach(header
    assert.h arpa/inet.h ctype.h errno.h fcntl.h limits.h locale.h memory.h
    netdb.h netinet/in.h pthread.h pwd.h stdio.h stdlib.h string.h strings.h
    sys/socket.h sys/stat.h sys/time.h sys/types.h time.h unistd.h)
  string(TOUPPER "${header}" header_var)
  string(REGEX REPLACE "[^A-Z0-9]" "_" header_var "${header_var}")
  check_include_file("${header}" "HAVE_${header_var}")
endforeach()

foreach(function snprintf strdup strerror strncasecmp vsnprintf)
  string(TOUPPER "${function}" function_var)
  check_function_exists("${function}" "HAVE_${function_var}")
endforeach()

set(HAVE_CONFIG_H 1)
set(STDC_HEADERS 1)
set(HAVE_NCURSES_H ${CURSES_HAVE_NCURSES_H})
set(HAVE_CURSES_H ${CURSES_HAVE_CURSES_H})
set(HAVE_CURSES 1)
set(HAVE_LIBNCURSES 1)
set(PACKAGE "prozilla")
set(PACKAGE_NAME "prozilla")
set(PACKAGE_VERSION "${PROJECT_VERSION}")
set(VERSION "${PROJECT_VERSION}")
set(LOCALEDIR "${CMAKE_INSTALL_FULL_LOCALEDIR}")
set(GLOBAL_CONF_FILE "${CMAKE_INSTALL_FULL_SYSCONFDIR}/prozilla.conf")
configure_file(cmake/config.h.in generated/config.h)

add_subdirectory(src)
add_subdirectory(config)
add_subdirectory(man)
add_subdirectory(po)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Multi-connection command-line download accelerator")
set(CPACK_PACKAGE_CONTACT "prozilla-dev@disconnected-by-peer.at")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING")
set(CPACK_GENERATOR "TGZ;TXZ")
set(CPACK_SOURCE_GENERATOR "TGZ;TXZ")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}")
set(CPACK_SOURCE_IGNORE_FILES "/\\.git/;/build/;~$;${CPACK_SOURCE_IGNORE_FILES}")
include(CPack)
