#
# Copyright (C) 2024-2025 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=fluent-bit
PKG_VERSION:=4.0.4
PKG_RELEASE:=$(AUTORELEASE)

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/fluent/fluent-bit.git
PKG_SOURCE_VERSION=v$(PKG_VERSION)
PKG_SOURCE:=$(PKG_NAME)-v$(PKG_VERSION).tar.zst
PKG_MIRROR_HASH:=skip
endif

PKG_LICENSE:=Apache-2.0
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/fluent-bit
	CATEGORY:=Utilities
	DEPENDS:= +libyaml +libopenssl +libcurl +libatomic +musl-fts +flex +bison +libstdcpp +@OPENSSL_WITH_NPN
	TITLE:=Fluent-Bit
	URL:=https://fluentbit.io/
endef

define Package/fluent-bit/description
	Fluent Bit is a super fast, lightweight, and highly scalable logging and metrics processor and forwarder.
endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
        $(CP) -rf ./fluent-bit/* $(PKG_BUILD_DIR)/
endef
endif

# General options
TARGET_LDFLAGS +=-lfts -latomic

CMAKE_OPTIONS+= \
	-DFLB_RELEASE=Yes \
	-DFLB_SMALL=No \
	-DEXCLUDE_FROM_ALL=true \
	-DFLB_SHARED_LIB=Yes \
	-DFLB_DEBUG=Yes \
	-DFLB_ALL=No \
	-DFLB_JEMALLOC=No \
	-DFLB_EXAMPLES=No \
	-DFLB_CHUNK_TRACE=No \
	-DFLB_BACKTRACE=No \
	-DFLB_KAFKA=No \
	-DFLB_WASM=No \
	-DFLB_LUAJIT=No

# In plugins
CMAKE_OPTIONS += \
	-DFLB_IN_SYSLOG=Yes \
	-DFLB_IN_CPU=Yes \
	-DFLB_IN_MEM=Yes \
	-DFLB_IN_DISK=Yes \
	-DFLB_IN_EXEC=Yes \
	-DFLB_IN_HEAD=Yes \
	-DFLB_IN_KMSG=Yes \
	-DFLB_IN_TAIL=Yes \
	-DFLB_IN_FORWARD=No \
	-DFLB_IN_PROC=No \
	-DFLB_IN_RANDOM=No \
	-DFLB_IN_SERIAL=No \
	-DFLB_IN_MQTT=No \
	-DFLB_IN_STDIN=No \
	-DFLB_IN_SYSTEMD=No \
	-DFLB_IN_TCP=No \
	-DFLB_IN_THERMAL=No \
	-DFLB_IN_UDP=No \
	-DFLB_IN_DOCKER=No \
	-DFLB_IN_EXEC_WASI=No \
	-DFLB_IN_EVENT_TYPE=No \
	-DFLB_IN_FLUENTBIT_METRICS=No \
	-DFLB_IN_KUBERNETES_EVENTS=No \
	-DFLB_IN_KAFKA=No \
	-DFLB_IN_LIB=No \
	-DFLB_IN_SYSTEMD=No \
	-DFLB_IN_DUMMY=No \
	-DFLB_IN_NETIF=No \
	-DFLB_IN_COLLECTD=No \
	-DFLB_IN_PROMETHEUS_SCRAPE=No \
	-DFLB_IN_STATSD=No \
	-DFLB_IN_STORAGE_BACKLOG=No \
	-DFLB_IN_PODMAN_METRICS=No \
	-DFLB_IN_OPENTELEMETRY=No \
	-DFLB_IN_ELASTICSEARCH=No \
	-DFLB_IN_CALYPTIA_FLEET=No \
	-DFLB_IN_SPLUNK=No
	-DFLB_IN_HEALTH=No \
	-DFLB_IN_WINLOG=No \
	-DFLB_IN_WINEVTLOG=No


# Filter options
CMAKE_OPTIONS += \
	-DFLB_FILTER_AWS=No \
	-DFLB_FILTER_ECS=No \
	-DFLB_FILTER_KUBERNETES=No \
	-DFLB_FILTER_LUA=No \
	-DFLB_FILTER_NEST=No \
	-DFLB_FILTER_RECORD_MODIFIER=No \
	-DFLB_FILTER_THROTTLE=No \
	-DFLB_FILTER_TYPE_CONVERTER=No \
	-DFLB_FILTER_WASM=No \
	-DFLB_FILTER_TENSORFLOW=No \
	-DFLB_FILTER_GEOIP2=No \
	-DFLB_FILTER_NIGHTFALL=No

# out plugins
CMAKE_OPTIONS += \
	-DFLB_OUT_EXIT=Yes \
	-DFLB_OUT_FORWARD=Yes \
	-DFLB_OUT_HTTP=Yes \
	-DFLB_OUT_NATS=Yes \
	-DFLB_OUT_TCP=Yes \
	-DFLB_OUT_UDP=Yes \
	-DFLB_OUT_FILE=Yes \
	-DFLB_OUT_STDOUT=Yes \
	-DFLB_OUT_SYSLOG=Yes \
	-DFLB_OUT_NULL=Yes \
	-DFLB_OUT_PLOT=No \
	-DFLB_OUT_AZURE=No \
	-DFLB_OUT_AZURE_BLOB=No \
	-DFLB_OUT_AZURE_LOGS_INGESTION=No \
	-DFLB_OUT_AZURE_KUSTO=No \
	-DFLB_OUT_BIGQUERY=No \
	-DFLB_OUT_CALYPTIA=No \
	-DFLB_OUT_COUNTER=No \
	-DFLB_OUT_DATADOG=No \
	-DFLB_OUT_ES=No \
	-DFLB_OUT_GELF=No \
	-DFLB_OUT_INFLUXDB=No \
	-DFLB_OUT_KAFKA=No \
	-DFLB_OUT_NRLOGS=No \
	-DFLB_OUT_OPENSEARCH=No \
	-DFLB_OUT_TD=No \
	-DFLB_OUT_SKYWALKING=No \
	-DFLB_OUT_SLACK=No \
	-DFLB_OUT_SPLUNK=No \
	-DFLB_OUT_STACKDRIVER=No \
	-DFLB_OUT_LIB=No \
	-DFLB_OUT_FLOWCOUNTER=No \
	-DFLB_OUT_LOGDNA=No \
	-DFLB_OUT_LOKI=No \
	-DFLB_OUT_KAFKA=No \
	-DFLB_OUT_KAFKA_REST=No \
	-DFLB_OUT_CLOUDWATCH_LOGS=No \
	-DFLB_OUT_KINESIS_FIREHOSE=No \
	-DFLB_OUT_KINESIS_STREAMS=No \
	-DFLB_OUT_OPENTELEMETRY=No \
	-DFLB_OUT_PROMETHEUS_EXPORTER=No \
	-DFLB_OUT_PROMETHEUS_REMOTE_WRITE=No \
	-DFLB_OUT_S3=No \
	-DFLB_OUT_VIVO_EXPORTER=No \
	-DFLB_OUT_WEBSOCKET=No \
	-DFLB_OUT_ORACLE_LOG_ANALYTICS=No \
	-DFLB_OUT_CHRONICLE=No \
	-DFLB_OUT_PGSQL=No

define Package/fluent-bit/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/fluent-bit
	$(INSTALL_DIR) $(1)/etc/fluent-bit/conf.d
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/bin/fluent-bit $(1)/usr/sbin/
	$(INSTALL_DATA) ./files/fluent-bit.conf $(1)/etc/fluent-bit/fluent-bit.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/conf/parsers.conf $(1)/etc/fluent-bit/parsers.conf
endef

$(eval $(call BuildPackage,fluent-bit))
