add_executable(bongo-config
	config.c
	store.c
	tls-names.c
)

target_compile_definitions(bongo-config PRIVATE
	BONGO_SETUP_PATH="${SBIN_INSTALL_DIR}/bongo-setup")

target_link_libraries(bongo-config
	bongoconnio
	bongomsgapi
	bongojson
	bongoutil
	bongoxpl
	bongocal
	LibGcrypt::LibGcrypt
	GnuTLS::GnuTLS
)

configure_file(bongo-setup.py.in bongo-setup @ONLY)
configure_file(bongo-sa-update.py.in bongo-sa-update @ONLY)

add_custom_target(default.set
	COMMAND tar chof - --exclude .svn --exclude config/aliases.d config
	        > ${CMAKE_CURRENT_BINARY_DIR}/default.set
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	VERBATIM
)

add_dependencies(bongo-config default.set)

install(TARGETS bongo-config DESTINATION ${SBIN_INSTALL_DIR})
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bongo-setup
	DESTINATION ${SBIN_INSTALL_DIR})
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bongo-sa-update
	DESTINATION ${LIBEXEC_INSTALL_DIR}/bongo)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/default.set DESTINATION ${DATA_INSTALL_DIR}/conf)

set(BONGO_CONFIG_TEMPLATES
	global queue smtp imap pop3 sieve collector manager antispam antivirus mailproxy
	acme worker web authentication)
foreach(template IN LISTS BONGO_CONFIG_TEMPLATES)
	install(FILES config/${template}
		DESTINATION ${DATA_INSTALL_DIR}/conf/templates)
endforeach()
install(FILES config/aliases.d/default_config
	DESTINATION ${DATA_INSTALL_DIR}/conf/templates/aliases.d)

if(BUILD_TESTING)
	add_executable(config-tls-names-test
		tests/tls-names-test.c
		tls-names.c)
	target_link_libraries(config-tls-names-test
		bongojson
		GLib2::GLib2
		GnuTLS::GnuTLS)
	add_test(NAME config-tls-names COMMAND config-tls-names-test)

	add_test(NAME configuration-model
		COMMAND ${CMAKE_COMMAND} -E env
			"PYTHONPATH=${CMAKE_SOURCE_DIR}/src/libs/python"
			"PYTHONPYCACHEPREFIX=${CMAKE_CURRENT_BINARY_DIR}/pycache"
			${Python3_EXECUTABLE} -m unittest discover
			-s ${CMAKE_CURRENT_SOURCE_DIR}/tests -v)
endif()
