include $(TOPDIR)/rules.mk

PKG_NAME:=netifd
PKG_VERSION:=2013-05-13
PKG_RELEASE=$(PKG_SOURCE_VERSION)

PKG_SOURCE_PROTO:=git
#PKG_SOURCE_URL:=git://nbd.name/luci2/netifd.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION:=bc4a4bb127622c76085ecec7fd20448aad7bafaf
USE_SOURCE_DIR:=./src
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MAINTAINER:=Felix Fietkau <nbd@openwrt.org>
# PKG_MIRROR_MD5SUM:=
# CMAKE_INSTALL:=1

PKG_LICENSE:=GPLv2
PKG_LICENSE_FILES:=

PKG_BUILD_PARALLEL:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

# For mips arch case, extern libopenat is required.
ifneq ($(CONFIG_OPENAT),)
  USELIBOPENAT=-lopenat
  EXTERN_OPENAT=-DEXTERN_OPENAT
endif

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

# provide for non-arm arch support
ifeq ($(CONFIG_LINUX_KARCH),"arm")
  arch_options=-D__ARM__=1
endif

define Package/netifd
  SECTION:=base
  # CATEGORY:=Base system
  CATEGORY:=TP-LINK iplatform apps
  SUBMENU:=Network
  DEPENDS:=+libuci +libnl-tiny +libubus +ubus +ubusd +jshn +PACKAGE_kmod-bonding:kmod-bonding +OPENAT:libopenat
  TITLE:=OpenWrt Network Interface Configuration Daemon
endef

TARGET_CFLAGS += \
	-I$(STAGING_DIR)/usr/include/libnl-tiny \
	-I$(STAGING_DIR)/usr/include $(USELIBOPENAT) \
	$(EXTERN_OPENAT)

TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include/json

CMAKE_OPTIONS += \
	-DLIBNL_LIBS=-lnl-tiny \
	-DDEBUG=1 $(arch_options) -DKEEP_BRIDGE=1 \


ifeq ($(CONFIG_PRODUCT_IS_SGMAC), y)
CMAKE_OPTIONS += -DCONFIG_PRODUCT_IS_SGMAC=1
endif

ifeq ($(CONFIG_aarch64), y)
	TARGET_CFLAGS += -DCONFIG_SUPPORT_AARCH64=1
endif

ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),y)
	TARGET_CFLAGS += -DEXTERNAL_TOOLCHAIN=1
endif
ifneq ($(CONFIG_PACKAGE_v6plus),)
	TARGET_CFLAGS += -DNETIFD_WITH_V6PLUS
export NETIFD_WITH_V6PLUS := 1
	CMAKE_OPTIONS += -DNETIFD_WITH_V6PLUS=1
endif

ifeq ($(CONFIG_MOBILE_FEATURE_BASIC_SUPPORT),y)
	CMAKE_OPTIONS += -DCONFIG_MOBILE_FEATURE_BASIC_SUPPORT=1
endif

ifeq ($(CONFIG_TOOLCHAIN_GCC_VERSION),"10.3")
	# for gcc 10.3 option -fno-common default
	TARGET_CFLAGS += -fcommon
endif

ifeq ($(CONFIG_IPF_PLATFORM_QCA), y)
    CMAKE_OPTIONS += -DCONFIG_IPF_PLATFORM_QCA=1
endif

ifeq ($(CONFIG_IPF_PLATFORM_BCM), y)
    TARGET_CFLAGS += -DCONFIG_IPF_PLATFORM_BCM
endif


define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)
endef

define Package/netifd/install
	$(INSTALL_DIR) $(1)/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/netifd $(1)/sbin/
	$(CP) ./filesystem/* $(1)/

ifeq ($(CONFIG_IPF_PLATFORM_RTK), y)
	rm -f $(1)/etc/hotplug.d/bridge/01-vlanstate
	rm -f $(1)/etc/hotplug.d/bridge/01-vlanstate_rtk
	$(CP) ./filesystem/etc/hotplug.d/bridge/01-vlanstate_rtk $(1)/etc/hotplug.d/bridge/01-vlanstate
else
	rm -f $(1)/etc/hotplug.d/bridge/01-vlanstate_rtk
endif

ifeq ($(CONFIG_MOBILE_FEATURE_BASIC_SUPPORT)_$(CONFIG_PRODUCT_IS_SGMAC), y_)
	$(CP) ./filesystem/etc/init.d/network_lte $(1)/etc/init.d/network
endif

ifeq ($(CONFIG_PRODUCT_IS_SGMAC), y)
	rm -f $(1)/etc/hotplug.d/bridge/01-vlanstate
	rm -f $(1)/etc/hotplug.d/bridge/02-alter-wanif
	$(CP) ./filesystem/etc/hotplug.d/mode/00-network_sgmac $(1)/etc/hotplug.d/mode/00-network
	$(CP) ./filesystem/etc/hotplug.d/net/03-vlanset_sgmac $(1)/etc/hotplug.d/net/03-vlanset
	$(CP) ./filesystem/etc/init.d/network_sgmac $(1)/etc/init.d/network
endif
	rm -f $(1)/etc/init.d/network_sgmac
	rm -f $(1)/etc/init.d/network_lte
	rm -f $(1)/etc/hotplug.d/mode/00-network_sgmac 
	rm -f $(1)/etc/hotplug.d/net/03-vlanset_sgmac 
	rm -f $(1)/etc/vlan.d/vlan.rtl8367s
ifneq ($(CONFIG_NETIF_TYPE_MARK), y)
	rm -f $(1)/etc/hotplug.d/net/00-intftype
endif
ifeq ($(CONFIG_TP_EXT_SWITCH_RTL8367S), y)
ifneq ($(CONFIG_IPF_PLATFORM_BCM), y)
	$(CP) ./filesystem/etc/vlan.d/vlan.rtl8367s $(1)/etc/vlan.d/vlan
endif
endif
	$(CP) $(PKG_BUILD_DIR)/dummy/netifd-proto.sh $(1)/lib/netifd/
endef

$(eval $(call BuildPackage,netifd))
