#
# Copyright (C) 2022-2023 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk

PKG_NAME:=swmodd
PKG_VERSION:=2.5.28

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/lcm/swmodd.git
PKG_SOURCE_VERSION:=9b3b87f6a24a39f8917576e3cda8d9dc5f4a8f18
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

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

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

define Package/swmodd
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:= Software Modules Daemon
  MENU:=1
  DEPENDS:=+libuci +libubox +ubus +libuuid +opkg +libcurl \
	    +PACKAGE_lxc:lxc +PACKAGE_lxc:liblxc +@BUSYBOX_CONFIG_BUSYBOX \
	    +@BUSYBOX_CONFIG_FEATURE_SHOW_SCRIPT +@BUSYBOX_CONFIG_SCRIPT \
	    +jsonfilter +@CONFIG_KERNEL_CGROUPS +SWMODD_LEGACY_CGROUPS:swmodd-cgroup
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
endef

define Package/swmodd/description
	Software module daemon to manage software/deployment units using TR181 datamodel.
endef

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

define Package/swmodd-cgroup
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:= Cgroup mount handler for Software modules daemon
endef

TARGET_CFLAGS += \
	-D_GNU_SOURCE \
	-Wall -Werror

ifeq ($(CONFIG_SWMODD_OCI_SUPPORT),y)
MAKE_FLAGS += \
	SWMOD_CRUN="yes"
endif

ifeq ($(CONFIG_SWMODD_LXC_SUPPORT),y)
MAKE_FLAGS += \
	SWMOD_LXC="yes"
endif

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

define Package/swmodd/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/swmodd
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/usr/share/swmodd/
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/etc/init.d/swmodd $(1)/etc/init.d/swmodd
	$(INSTALL_BIN) ./files/etc/config/swmodd $(1)/etc/config/swmodd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/swmodd $(1)/usr/sbin/swmodd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/scripts/opkg_offline.sh $(1)/usr/share/swmodd/opkg_offline
	$(INSTALL_BIN) ./files/etc/uci-defaults/01-fix-bundle-path $(1)/etc/uci-defaults/01-fix-bundle-path
	$(INSTALL_BIN) ./files/etc/uci-defaults/03-execenv-ref $(1)/etc/uci-defaults/03-execenv-ref
	$(INSTALL_BIN) ./files/etc/uci-defaults/04-update-root-path $(1)/etc/uci-defaults/04-update-root-path

ifeq ($(CONFIG_PACKAGE_liblxc),y)
	$(INSTALL_DIR) $(1)/usr/share/lxc/templates/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/templates/lxc-iopsys $(1)/usr/share/lxc/templates/lxc-iopsys
	$(INSTALL_BIN) ./files/etc/uci-defaults/02-migrate-lxc $(1)/etc/uci-defaults/02-migrate-lxc
endif
ifeq ($(CONFIG_PACKAGE_crun),y)
	$(INSTALL_BIN) ./files/etc/swmodd/run.sh $(1)/etc/swmodd/run.sh
	$(INSTALL_BIN) ./files/etc/init.d/crun $(1)/etc/init.d/crun
	$(INSTALL_BIN) ./files/etc/config/crun $(1)/etc/config/crun
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/scripts/crun_create $(1)/usr/sbin/crun_create
endif
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/libswmodd.so $(1) $(PKG_NAME)
endef

define Package/swmodd-cgroup/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/swmodd_cgroup $(1)/etc/init.d/swmodd_cgroup
endef

$(eval $(call BuildPackage,swmodd-cgroup))
$(eval $(call BuildPackage,swmodd))
