#
# Copyright (C) 2022 IOPSYS Software Solutions AB
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
export LIBVOICE_MAKE_OPTS:=$(TOPDIR)/tmp/.libvoice-opts.mk
include $(TOPDIR)/rules.mk
-include $(LIBVOICE_MAKE_OPTS)

PKG_NAME:=voicemngr
PKG_RELEASE:=1
PKG_VERSION:=1.2.5
PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

LOCAL_SRC_DIR:=~/git/voip/$(PKG_NAME)
LOCAL_DEV=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/iopsys/$(PKG_NAME).git
PKG_SOURCE_VERSION:=a8197fa92017cbf2a850b8f68ce967670525c03d
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.zst
PKG_MIRROR_HASH:=skip
endif

# All config variable that are passed to the make invocation, directly or
# indirectly. This ensures that the package is rebuilt on config-changes.
PKG_CONFIG_DEPENDS:=CONFIG_TARGET_BOARD

include $(INCLUDE_DIR)/package.mk
-include $(TOPDIR)/package/feeds/broadcom/bcmkernel/bcm-toolchain.mk

TARGET_CFLAGS += -Wall -Werror

export LIBVOICE_CFLAGS
export LIBVOICE_LIBS

define Package/$(PKG_NAME)
	CATEGORY:=Utilities
	TITLE:=IOPSYS Voice Manager
	URL:=

	DEPENDS:= +libubox +libblobmsg-json +ubus +libopenssl +!TARGET_brcmbca:libpicoevent +TARGET_brcmbca:libpicoevent-bcm \
		+!TARGET_brcmbca:uci +TARGET_brcmbca:uci-bcm \
		+TARGET_brcmbca:bcm963xx-bsp +TARGET_brcmbca:libvoice-broadcom \
		+TARGET_ipq95xx:libvoice-d2 +TARGET_ipq95xx:kmod-d2-voice \
		+TARGET_airoha:kmod-voip_driver +TARGET_airoha:voip_app +TARGET_airoha:libvoice-airoha
endef

define Package/$(PKG_NAME)/description
	Voice manager is a daemon that bridges Asterisk and platform specific DSP/SLIC APIs
endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
	@echo "Building from the local source. TARGET_PLATFORM=$(TARGET_PLATFORM)"
	rsync -av --exclude=.* $(LOCAL_SRC_DIR)/* $(PKG_BUILD_DIR)/
	$(LN) $(LIBVOICE_PKG_BUILD_DIR)/*.[ch] $(PKG_BUILD_DIR)/libvoice/
endef
else
define Build/Prepare
	$(call Build/Prepare/Default)
	$(LN) $(LIBVOICE_PKG_BUILD_DIR)/*.[ch] $(PKG_BUILD_DIR)/libvoice/
endef
endif

#installs files into ./staging_dir/. that shared with asterisk-chan-voicemngr
define Build/InstallDev
	$(INSTALL_DIR) $(STAGING_DIR)/usr/include
	$(CP) $(PKG_BUILD_DIR)/libvoice/voice-types.h $(STAGING_DIR)/usr/include
endef

define Package/$(PKG_NAME)/install
	$(CP) ./files/* $(1)/
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/usr/lib
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/sbin/
	$(CP) $(PKG_BUILD_DIR)/libvoice/libvoice.so* $(1)/usr/lib/
endef
$(eval $(call BuildPackage,$(PKG_NAME)))
