################################################################################
#
# mosquitto
#
################################################################################

MOSQUITTO_VERSION = 2.1.2
MOSQUITTO_SITE = https://mosquitto.org/files/source
MOSQUITTO_LICENSE = EPL-2.0 or EDLv1.0
MOSQUITTO_LICENSE_FILES = LICENSE.txt epl-v20 edl-v10
MOSQUITTO_CPE_ID_VENDOR = eclipse
MOSQUITTO_INSTALL_STAGING = YES

MOSQUITTO_DEPENDENCIES = cjson

MOSQUITTO_CONF_OPTS = \
	-DUSE_LIBWRAP=OFF \
	-DWITH_DOCS=OFF \
	-DWITH_TESTS=OFF \
	-DWITH_STATIC_LIBRARIES=OFF \
	-DWITH_BUNDLED_DEPS=ON

# adns uses getaddrinfo_a
ifeq ($(BR2_TOOLCHAIN_USES_GLIBC),y)
MOSQUITTO_CONF_OPTS += -DWITH_ADNS=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_ADNS=OFF
endif

# threaded API uses pthread_setname_np
ifeq ($(BR2_TOOLCHAIN_HAS_THREADS_NPTL),y)
MOSQUITTO_CONF_OPTS += -DWITH_THREADING=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_THREADING=OFF
endif

ifeq ($(BR2_PACKAGE_OPENSSL),y)
MOSQUITTO_DEPENDENCIES += host-pkgconf openssl
MOSQUITTO_CONF_OPTS += \
	-DWITH_TLS=ON \
	-DWITH_TLS_PSK=ON

ifeq ($(BR2_PACKAGE_MOSQUITTO_WS_LIBWS),y)
MOSQUITTO_DEPENDENCIES += libwebsockets
MOSQUITTO_CONF_OPTS += -DWITH_WEBSOCKETS=ON -DWITH_WEBSOCKETS_BUILTIN=OFF
else ifeq ($(BR2_PACKAGE_MOSQUITTO_WS_BUILTIN),y)
MOSQUITTO_CONF_OPTS += -DWITH_WEBSOCKETS=ON -DWITH_WEBSOCKETS_BUILTIN=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_WEBSOCKETS=OFF -DWITH_WEBSOCKETS_BUILTIN=OFF
endif

else # !OPENSSL
MOSQUITTO_CONF_OPTS += \
	-DWITH_TLS=OFF \
	-DWITH_TLS_PSK=OFF \
	-DWITH_WEBSOCKETS=OFF
endif

ifeq ($(BR2_PACKAGE_C_ARES),y)
MOSQUITTO_DEPENDENCIES += c-ares
MOSQUITTO_CONF_OPTS += -DWITH_SRV=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_SRV=OFF
endif

ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
MOSQUITTO_CONF_OPTS += -DWITH_LIB_CPP=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_LIB_CPP=OFF
endif

ifeq ($(BR2_PACKAGE_MOSQUITTO_CLIENTS),y)
MOSQUITTO_CONF_OPTS += -DWITH_CLIENTS=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_CLIENTS=OFF
endif

ifeq ($(BR2_PACKAGE_MOSQUITTO_APPS),y)
MOSQUITTO_DEPENDENCIES += $(if $(BR2_PACKAGE_LIBEDIT),libedit,readline)
MOSQUITTO_CONF_OPTS += -DWITH_APPS=ON -DWITH_CTRL_SHELL=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_APPS=OFF -DWITH_CTRL_SHELL=OFF
endif

ifeq ($(BR2_PACKAGE_MOSQUITTO_BROKER),y)

MOSQUITTO_CONF_OPTS += \
	-DCMAKE_INSTALL_SYSCONFDIR=/etc \
	-DWITH_BROKER=ON \
	-DWITH_PLUGINS=ON \
	-DWITH_PLUGIN_EXAMPLES=OFF \
	-DWITH_PLUGIN_SPARKPLUG_AWARE=OFF \
	-DWITH_PLUGIN_PASSWORD_FILE=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_PLUGIN_STATIC_PASSWD),ON,OFF) \
	-DWITH_PLUGIN_ACL_FILE=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_PLUGIN_STATIC_ACL),ON,OFF) \
	-DWITH_PLUGIN_DYNAMIC_SECURITY=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_DYNAMIC_SECURITY_PLUGIN),ON,OFF) \
	-DINC_BRIDGE_SUPPORT=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_BRIDGE),ON,OFF) \
	-DWITH_SYS_TREE=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_SYS),ON,OFF) \
	-DWITH_CONTROL=$(if $(BR2_PACKAGE_MOSQUITTO_BROKER_CONTROL),ON,OFF)

ifeq ($(BR2_PACKAGE_MOSQUITTO_BROKER_HTTP_API),y)
MOSQUITTO_DEPENDENCIES += libmicrohttpd
MOSQUITTO_CONF_OPTS += -DWITH_HTTP_API=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_HTTP_API=OFF
endif

ifeq ($(BR2_PACKAGE_MOSQUITTO_BROKER_PLUGIN_PERSIST_SQLITE),y)
MOSQUITTO_DEPENDENCIES += sqlite
MOSQUITTO_CONF_OPTS += -DWITH_PLUGIN_PERSIST_SQLITE=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_PLUGIN_PERSIST_SQLITE=OFF
endif

ifeq ($(BR2_PACKAGE_SYSTEMD),y)
MOSQUITTO_DEPENDENCIES += systemd
MOSQUITTO_CONF_OPTS += -DWITH_SYSTEMD=ON
else
MOSQUITTO_CONF_OPTS += -DWITH_SYSTEMD=OFF
endif

define MOSQUITTO_INSTALL_INIT_SYSV
	$(INSTALL) -D -m 0755 package/mosquitto/S50mosquitto \
		$(TARGET_DIR)/etc/init.d/S50mosquitto
endef

define MOSQUITTO_INSTALL_INIT_SYSTEMD
	$(INSTALL) -D -m 644 $(@D)/service/systemd/mosquitto.service.notify \
		$(TARGET_DIR)/usr/lib/systemd/system/mosquitto.service
endef

define MOSQUITTO_USERS
	mosquitto -1 mosquitto -1 * - - - Mosquitto user
endef

define MOSQUITTO_INSTALL_TARGET_CONF
	rm -f $(TARGET_DIR)/etc/mosquitto/*.example
	$(INSTALL) -D -m 0644 $(@D)/mosquitto.conf \
		$(TARGET_DIR)/etc/mosquitto/mosquitto.conf
endef
MOSQUITTO_POST_INSTALL_TARGET_HOOKS += MOSQUITTO_INSTALL_TARGET_CONF

else # !BR2_PACKAGE_MOSQUITTO_BROKER
MOSQUITTO_CONF_OPTS += \
	-DWITH_BROKER=OFF \
	-DWITH_PLUGINS=OFF
endif

HOST_MOSQUITTO_DEPENDENCIES = \
	host-cjson \
	host-openssl \
	host-pkgconf

HOST_MOSQUITTO_CONF_OPTS = \
	-DWITH_DOCS=OFF \
	-DWITH_BUNDLED_DEPS=ON \
	-DWITH_CLIENTS=OFF \
	-DWITH_BROKER=OFF \
	-DWITH_PLUGINS=OFF \
	-DWITH_TESTS=OFF \
	-DWITH_APPS=ON \
	-DWITH_CTRL_SHELL=OFF \
	-DWITH_TLS=ON

# Manually install just the one tool we need
define HOST_MOSQUITTO_INSTALL_CMDS
	$(INSTALL) -D -m 0755 \
		$(@D)/apps/mosquitto_passwd/mosquitto_passwd \
		$(HOST_DIR)/bin/mosquitto_passwd
endef

$(eval $(cmake-package))
$(eval $(host-cmake-package))
