#
# Copyright (C) 2020-2023 IOPSYS Software Solutions AB
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=udpecho
PKG_VERSION:=2.2.5

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

PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
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

MAKE_PATH:=src

define Package/$(PKG_NAME)-server
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=BBF UDP Echo Server
  DEPENDS:=+libuci +libbbfdm-api +netmngr +libbbfdm-ubus +dm-service
endef

define Package/$(PKG_NAME)-client
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=BBF UDP Echo Client
endef

define Package/$(PKG_NAME)-server/description
	BBF UDP Echo Server
endef

define Package/$(PKG_NAME)-client/description
	BBF UDP Echo Client
endef

TARGET_CFLAGS += \
	-D_GNU_SOURCE \
	-Wall -Werror

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

define Package/$(PKG_NAME)-server/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/udpechoserverd $(1)/usr/sbin/
	$(INSTALL_DATA) ./files/etc/config/udpechoserver $(1)/etc/config/udpechoserver
	$(INSTALL_BIN) ./files/etc/init.d/udpechoserverd $(1)/etc/init.d/udpechoserverd
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/src/libudpechoserver.so $(1) netmngr 15
endef

define Package/$(PKG_NAME)-client/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/udpechoclientd $(1)/usr/sbin/
endef

$(eval $(call BuildPackage,$(PKG_NAME)-server))
$(eval $(call BuildPackage,$(PKG_NAME)-client))
