#
# Copyright (C) 2024 iopsys
#

include $(TOPDIR)/rules.mk

PKG_NAME:=logmngr
PKG_VERSION:=1.1.4

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/system/logmngr.git
PKG_SOURCE_VERSION:=62441fdfe14a39bff8fff7c62307bd7b54d7240f
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

PKG_LICENSE:=GPL-2.0-only
PKG_LICENSE_FILES:=LICENSE

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

MAKE_PATH:=bbf_plugin

define Package/logmngr
 SECTION:=utils
 CATEGORY:=Utilities
 TITLE:=Logging Manager
 DEPENDS:=+LOGMNGR_BACKEND_FLUENTBIT:fluent-bit
 DEPENDS+=+LOGMNGR_BACKEND_SYSLOG_NG:syslog-ng
 DEPENDS+=+LOGMNGR_LOGROTATE:logrotate
 DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
 CONFLICTS:=logd
endef

define Package/logmngr/description
	Configure log management. This package has the datamodel as well as the
	the backend implementation for handling syslog.
endef

define Package/$(PKG_NAME)/config
        source "$(SOURCE)/Config.in"
endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
	$(CP) -rf ~/git/logmngr/* $(PKG_BUILD_DIR)/
endef
endif

define Package/logmngr/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/uci-defaults

	$(INSTALL_BIN) ./files/etc/init.d/logmngr $(1)/etc/init.d/
	$(INSTALL_DATA) ./files/etc/config/logmngr $(1)/etc/config/
	$(INSTALL_DATA) ./files/etc/uci-defaults/10-logmngr_config_migrate $(1)/etc/uci-defaults/

	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/bbf_plugin/libbbfsyslog.so $(1) core 10

	# Install logmngr service backend
	$(INSTALL_DIR) $(1)/lib/logmngr
ifeq ($(CONFIG_LOGMNGR_BACKEND_FLUENTBIT),y)
	$(INSTALL_DIR) $(1)/sbin
	$(INSTALL_BIN) ./files/logread $(1)/sbin/
	$(INSTALL_DATA) ./files/lib/logmngr/fluent-bit.sh $(1)/lib/logmngr/
else ifeq ($(CONFIG_LOGMNGR_BACKEND_SYSLOG_NG),y)
	$(INSTALL_DATA) ./files/lib/logmngr/syslog-ng.sh $(1)/lib/logmngr/
endif

ifeq ($(CONFIG_LOGMNGR_LOGROTATE),y)
	$(INSTALL_DATA) ./files/lib/logmngr/logrotate.sh $(1)/lib/logmngr/
	$(INSTALL_DATA) ./files/etc/uci-defaults/11-logmngr_logrotate_syslog $(1)/etc/uci-defaults/
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/bbf_plugin/libbbflogrotate.so $(1) sysmngr 11
endif

ifeq ($(CONFIG_LOGMNGR_VENDOR_LOG_FILE),y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/bbf_plugin/libbbfvendorlog.so $(1) sysmngr 12
endif

endef

$(eval $(call BuildPackage,logmngr))
