#
# Copyright (C) 2021-2023 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=sulu-builder
PKG_VERSION:=5.3.10
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/websdk/sulu-builder.git
PKG_SOURCE_VERSION:=6a82c845c5203a7e33737d668659e60203f9f68f
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_SOURCE_VERSION)
PKG_SOURCE:=$(PKG_NAME)-$(PKG_SOURCE_VERSION).tar.zst
PKG_BUILD_DIR:=$(BUILD_DIR)/sulu-$(PKG_VERSION)/sulu-builder-$(PKG_SOURCE_VERSION)
PKG_MIRROR_HASH:=skip

PKG_LICENSE:=PROPRIETARY IOPSYS
PKG_LICENSE_FILES:=LICENSE

SULU_PLUGINS:=$(filter-out sulu-builder,$(filter-out Readme.md,$(notdir $(wildcard ../*))))
SULU_EXTRA:=$(subst $\",,$(CONFIG_SULU_BUILDER_EXTRA_PACKAGES))
include $(INCLUDE_DIR)/package.mk

define Package/sulu/default
	SECTION:=utils
	CATEGORY:=Utilities
	SUBMENU:=SULU
	TITLE:=SULU-CE
	DEPENDS:=+mosquitto-auth-plugin +usermngr +userinterface +obuspa
	DEPENDS+=+@OBUSPA_LOCAL_MQTT_LISTENER
	EXTRA_DEPENDS:=nginx
endef

define Package/sulu
	$(Package/sulu/default)
	CONFLICTS:=sulu-builder
	TITLE += (Binaries)
	VARIANT:=binary
	DEFAULT_VARIANT:=1
endef

define Package/sulu-builder
	$(Package/sulu/default)
	CONFLICTS:=sulu
	TITLE += (Builder)
	VARIANT:=builder
	DEPENDS+=+sulu-base $(foreach plugin,$(SULU_PLUGINS), +PACKAGE_$(plugin):$(plugin)) $(foreach plugin,$(SULU_EXTRA), +PACKAGE_$(plugin):$(plugin))
endef

define Package/sulu/description
	SULU-CE ReactJS based Web UI works on USP with websocket.
endef

define Package/sulu-builder/description
	SULU-CE ReactJS based Web UI builder.
endef

define Package/sulu/config
	config SULU_DEFAULT_UI
		depends on PACKAGE_sulu
		bool "Makes Sulu run on port 443 instead of 8443"
		default n

endef

define Package/sulu-builder/config
	config SULU_BUILDER_EXTRA_PACKAGES
		depends on PACKAGE_sulu-builder
		string "Space separated list of sulu packages"
		help
			You can specify the list of non core sulu package,
			so that sulu-builder include them before building the core.

	config SULU_BUILDER_DEFAULT_UI
		depends on PACKAGE_sulu-builder
		bool "Makes Sulu run on port 443 instead of 8443"
		default n

endef

ifeq ($(BUILD_VARIANT),builder)
define Build/Compile
	VERSION="v${PKG_VERSION}" CORE_DIR="../core/sulu-*" PLUGINS_PATH="../plugins" $(MAKE) -C $(PKG_BUILD_DIR)/
endef
else
define Build/Compile
	# Pass
endef
endif

define Build/Clean
	$(RM) -r $(BUILD_DIR)/sulu-$(PKG_VERSION)
endef

define Package/sulu/install/Default
	$(INSTALL_DIR) $(1)/sulu/
	$(INSTALL_DIR) $(1)/etc/sulu

	$(INSTALL_DATA) ./files/maintenance.html $(1)/sulu/
	$(LN) /tmp/sulu $(1)/sulu/connection

	$(INSTALL_BIN) ./files/etc/sulu/sulu.sh $(1)/etc/sulu/
	$(INSTALL_DATA) ./files/etc/sulu/nginx.locations $(1)/etc/sulu/
	$(INSTALL_BIN) ./files/etc/sulu/sulu_watcher.sh $(1)/etc/sulu/

	$(INSTALL_DIR) $(1)/etc/users/roles
	$(INSTALL_DATA) ./files/etc/users/roles/*.json $(1)/etc/users/roles/

	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DATA) ./files/etc/uci-defaults/40-add-sulu-config $(1)/etc/uci-defaults/
ifneq ($(CONFIG_SULU_DEFAULT_UI)$(CONFIG_SULU_BUILDER_DEFAULT_UI),)
	$(INSTALL_DATA) ./files/etc/uci-defaults/41-make-sulu-default-ui $(1)/etc/uci-defaults/
endif
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/sulu $(1)/etc/init.d/
endef

define Package/sulu/install/Post
	# Post install cleanup
ifneq ($(CONFIG_PACKAGE_skopeo)$(CONFIG_PACKAGE_umoci),yy)
	$(RM) $(1)/sulu/presets/lcm-store.json
endif
endef

define Package/sulu/install
	$(Package/sulu/install/Default)
	$(CP) $(PKG_BUILD_DIR)/dist/* $(1)/sulu
	$(Package/sulu/install/Post)
endef

define Package/sulu-builder/install
	$(Package/sulu/install/Default)
	$(INSTALL_DIR) $(1)/sulu/presets
	$(INSTALL_DIR) $(1)/sulu/settings
	$(INSTALL_DIR) $(1)/sulu/widget-presets
	$(CP) $(PKG_BUILD_DIR)/build/dist/* $(1)/sulu
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/build/device/presets/*.json $(1)/sulu/presets/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/build/device/settings/*.json $(1)/sulu/settings/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/build/device/widget-presets/*.json $(1)/sulu/widget-presets/
	$(Package/sulu/install/Post)
endef

$(eval $(call BuildPackage,sulu-builder))
$(eval $(call BuildPackage,sulu))
