#
# Copyright (c) [2020], MediaTek Inc. All rights reserved.
#
# This software/firmware and related documentation ("MediaTek Software") are
# protected under relevant copyright laws.
# The information contained herein is confidential and proprietary to
# MediaTek Inc. and/or its licensors.
# Except as otherwise provided in the applicable licensing terms with
# MediaTek Inc. and/or its licensors, any reproduction, modification, use or
# disclosure of MediaTek Software, and information contained herein, in whole
# or in part, shall be strictly prohibited.
#

include $(TOPDIR)/rules.mk

SOURCE_DIR:=$(TOPDIR)/../mtk/package/network/utils/mipc-wan

PKG_NAME:=mipc-wan
PKG_VERSION:=1.0
PKG_RELEASE:=1
PKG_FILE_DEPENDS := $(SOURCE_DIR)
PKG_LICENSE:=GPL-2.0
PKG_LICENSE_FILES:=

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

include $(INCLUDE_DIR)/package.mk

define Package/mipc-wan
  SECTION:=MTK Properties
  CATEGORY:=MTK Properties
  SUBMENU:=Network
  DEPENDS:=+mipc +libubox +libjson-c +libdtmf_detect +libuci
  TITLE:=Control utility for Mediatek mobile broadband modems
endef

define Package/mipc-wan/description
  mipc-wan is a command line tool for controlling Mediatek mobile broadband modems using
  the MICP APIs.
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	cp $(SOURCE_DIR)/src/* $(PKG_BUILD_DIR)
	$(Build/Patch)
endef

TARGET_CFLAGS += \
	-I$(STAGING_DIR)/usr/include/mipc/api \
	-I$(STAGING_DIR)/usr/include/mipc/common \
	-I$(STAGING_DIR)/usr/include/mipc/msg \
	-I$(STAGING_DIR)/usr/include

TARGET_CFLAGS += -Wall -Werror -D_LINUX -fPIC
TARGET_LIBS = -Bdynamic -lubox -lmipc_msg -ljson-c -lpthread -ldtmf_detect -lasound -luci

define Build/Compile
	CC="$(TARGET_CC)" \
	CFLAGS="$(TARGET_CFLAGS)" \
	LDFLAGS="$(TARGET_LDFLAGS)" \
	LIBS="$(TARGET_LIBS)" \
	$(MAKE) -C $(PKG_BUILD_DIR) all
endef

define Package/mipc-wan/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/mipc_wan_cli $(1)/usr/bin

	$(INSTALL_DIR) $(1)/lib/netifd/proto
	$(INSTALL_BIN) $(SOURCE_DIR)/files/mipc.proto $(1)/lib/netifd/proto/mipc.sh

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) $(SOURCE_DIR)/files/mipc_wan.init $(1)/etc/init.d
	$(INSTALL_BIN) $(SOURCE_DIR)/files/telnetd $(1)/etc/init.d

	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) $(SOURCE_DIR)/files/default_apn.json $(1)/etc

	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(SOURCE_DIR)/files/mtk_netdiag.sh $(1)/usr/bin
	$(INSTALL_BIN) $(SOURCE_DIR)/files/test/mtk_usb_tether_on.sh $(1)/usr/bin
	$(INSTALL_BIN) $(SOURCE_DIR)/files/test/mtk_usb_tether_off.sh $(1)/usr/bin
	$(INSTALL_BIN) $(SOURCE_DIR)/files/test/mtk_wan_eth1.sh $(1)/usr/bin
	$(INSTALL_BIN) $(SOURCE_DIR)/files/test/mtk_wan_mipc.sh $(1)/usr/bin
endef

$(eval $(call BuildPackage,mipc-wan))
