#
# Copyright (C) 2023 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=userinterface
PKG_VERSION:=1.1.9

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/userinterface.git
PKG_SOURCE_VERSION:=52f8bff5941c5e91d7ed8eed33cf5adf364960c5
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

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

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

define Package/userinterface
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=Package to add Device.UserInterface. datamodel support using bbfdm
  DEPENDS:=+USERINTERFACE_HTTPACCESS_BACKEND_NGINX:nginx
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
  MENU:=1
endef

MAKE_PATH:=src

define Package/userinterface/description
  Package to add Device.UserInterface. datamodel support
endef

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

ifeq ($(CONFIG_USERINTERFACE_VENDOR_EXTENSIONS),y)
TARGET_CFLAGS += -DUSERINTERFACE_VENDOR_EXTENSIONS
ifeq ($(CONFIG_USERINTERFACE_VENDOR_PREFIX),"")
TARGET_CFLAGS += -DBBF_VENDOR_PREFIX=\\\"$(CONFIG_BBF_VENDOR_PREFIX)\\\"
else
TARGET_CFLAGS += -DBBF_VENDOR_PREFIX=\\\"$(CONFIG_USERINTERFACE_VENDOR_PREFIX)\\\"
endif
endif

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

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

# Install service
	$(INSTALL_DATA) ./files/etc/config/userinterface $(1)/etc/config/userinterface
	$(INSTALL_BIN) ./files/etc/init.d/userinterface $(1)/etc/init.d/userinterface
	$(INSTALL_BIN) ./files/etc/uci-defaults/93-userinterface-firewall $(1)/etc/uci-defaults/93-userinterface-firewall
	$(INSTALL_BIN) ./files/etc/firewall.userinterface $(1)/etc/firewall.userinterface

# Install datamodel microservice
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/libuserinterface.so $(1) $(PKG_NAME)

# Install backend
ifeq ($(CONFIG_USERINTERFACE_HTTPACCESS_BACKEND_NGINX),y)
	$(INSTALL_DIR) $(1)/usr/libexec/rpcd
	$(INSTALL_BIN) ./files/backend/nginx/http_access $(1)/etc/userinterface/http_access
	$(INSTALL_BIN) ./files/backend/nginx/userinterface_rpc $(1)/usr/libexec/rpcd/userinterface
ifeq ($(CONFIG_USERINTERFACE_HTTPACCESS_MIGRATE_NGINX_CONFIG),y)
	$(INSTALL_BIN) ./files/backend/nginx/95-migrate-nginx-userinterface $(1)/etc/uci-defaults/95-migrate-nginx-userinterface
endif
endif
endef

$(eval $(call BuildPackage,userinterface))
