#
# Copyright (C) 2023 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=timemngr
PKG_VERSION:=1.1.13

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/timemngr.git
PKG_SOURCE_VERSION:=9817c0ad15dea444258c9e0de6975f43fdc3e99a
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.zst
PKG_MIRROR_HASH:=skip
endif

PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/feeds/iopsys/bbfdm/bbfdm.mk

#RSTRIP:=:

define Package/$(PKG_NAME)
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=Time manager daemon based on ntpd
  DEPENDS:=+ntpd +libuci +libubox +ubus +@BUSYBOX_CONFIG_PGREP
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +bbfdmd
  MENU:=1
endef

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

MAKE_PATH:=src

define Package/$(PKG_NAME)/description
	Manage device time with ntpd and provides Device.Time. datamodel object based on TR181-2.16
endef

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

ifeq ($(CONFIG_TIMEMNGR_VENDOR_PREFIX),"")
VENDOR_PREFIX = $(CONFIG_BBF_VENDOR_PREFIX)
else
VENDOR_PREFIX = $(CONFIG_TIMEMNGR_VENDOR_PREFIX)
endif

TARGET_CFLAGS += -DBBF_VENDOR_PREFIX=\\\"$(VENDOR_PREFIX)\\\"

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/etc/timemngr/
	$(INSTALL_DIR) $(1)/etc/config/
	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
	$(INSTALL_DIR) $(1)/etc/hotplug.d/ntp
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/uci-defaults
ifeq ($(CONFIG_TIMEMNGR_LEGACY_DM),y)
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service_legacy.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/dm_time_legacy.so $(1) $(PKG_NAME)
else
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/timemngr $(1)/usr/sbin/timemngr
ifeq ($(CONFIG_TIMEMNGR_MIGRATE_SYSTEM),y)
	$(INSTALL_BIN) ./files/etc/uci-defaults/96-system-ntp-migrate $(1)/etc/uci-defaults/
endif
	$(INSTALL_BIN) ./files/etc/hotplug.d/iface/21-timemngr $(1)/etc/hotplug.d/iface/
	$(INSTALL_BIN) ./files/etc/hotplug.d/ntp/40-firstUseDate $(1)/etc/hotplug.d/ntp/
	$(INSTALL_BIN) ./files/etc/init.d/timemngr $(1)/etc/init.d/
	$(INSTALL_DATA) ./files/etc/config/time $(1)/etc/config/
ifeq ($(CONFIG_TIMEMNGR_NTPD),y)
	$(CP) ./files/etc/timemngr/ntpd_config.sh $(1)/etc/timemngr/time.sh
	$(CP) ./files/etc/timemngr/generate_supported_zones.sh $(1)/etc/timemngr/generate_supported_zones.sh
endif
endif

ifeq ($(CONFIG_TIMEMNGR_VENDOR_EXTENSIONS),y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/src/libtimeext.so $(1) $(PKG_NAME)
endif
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
