#
#	Makefile -- Build instructions for user level apps
#

.EXPORT_ALL_VARIABLES:
.PHONY: config all romfs clean prune

#
# Include architecture specific build rules.
#
ifndef ROOTDIR
ROOTDIR=..
endif

UCLINUX_BUILD_USER=1
-include $(LINUX_CONFIG)
-include $(CONFIG_CONFIG)
-include $(BUSYBOX_CONFIG)
-include $(ARCH_CONFIG)
-include $(MODULES_CONFIG)

VEND=$(ROOTDIR)/vendors

#CONFIG_USER_GOAHEAD_GDB_DEBUG=y
#export CONFIG_USER_GDB_GOAHEAD_DEBUG

ifeq ("$(CONFIG_PRODUCT)","MT7621")
CONFIG_MTK_AP_DRV_FOLDER_NAME=mt_wifi_4421
else
ifeq ("$(CONFIG_MODEL)" , "AP802")
CONFIG_MTK_AP_DRV_FOLDER_NAME=rlt_wifi_new
else
CONFIG_MTK_AP_DRV_FOLDER_NAME=rt2860v2
endif
endif

ifeq ("$(CONFIG_USER_FREERADIUS2)","y")
FREERADIUS_FOLDER_NAME=freeradius-2.2.9
else
FREERADIUS_FOLDER_NAME=freeradius-1.1.6
endif

#
# must run the vendor build first
#
dir_v = $(VEND)/$(CONFIG_VENDOR)/$(CONFIG_PRODUCT)/.
dir_p = $(ROOTDIR)/prop

dir_y = 
dir_n =
dir_  =

dir_$(CONFIG_USER_MTKIAPPD)	    	    += mtkiappd
dir_$(CONFIG_USER_NVRAMD)           += nvramd
dir_$(CONFIG_LIB_LIBJSON)           += json-c-0.12
dir_$(CONFIG_USER_802_1X)		    += 802.1x
dir_$(CONFIG_USER_RT2561_802_1X)	    += rt61apd
dir_$(CONFIG_USER_BIGPOND) 		    += bigpond
dir_$(CONFIG_USER_BONNIE)		    += bonnie++
dir_$(CONFIG_USER_BRCTL_BRCTL)              += bridge-utils
dir_$(CONFIG_USER_BT_UTILS)                 += bt-utils
dir_$(CONFIG_USER_BUSYBOX_BUSYBOX)          += busybox
dir_$(CONFIG_USER_CTORRENT)                 += ctorrent-dnh3.2
dir_$(CONFIG_USER_DHCP6)                    += dhcp6
dir_$(CONFIG_DHRYSTONE)		    	    += dhrystone
dir_$(CONFIG_USER_DNSMASQ)                  += dnsmasq-2.40
dir_$(CONFIG_USER_DISKTYPE)		    += disktype-9
dir_$(CONFIG_USER_ETHTOOL)		    += ethtool
dir_$(CONFIG_USER_GDBSERVER_GDBREPLAY)      += gdbserver
dir_$(CONFIG_USER_GDBSERVER_GDBSERVER)      += gdbserver
dir_$(CONFIG_USER_MATRIXSSL)                += matrixssl-1.8.3
dir_$(CONFIG_USER_OPENSSL)	   	    += $(CONFIG_USER_OPENSSL_FOLDER_NAME)
dir_$(CONFIG_USER_GOAHEAD_HTTPD)            += goahead
dir_$(CONFIG_USER_TR069_CLIENT)             += tr069

ifeq ($(CONFIG_LINUXDIR), linux-2.6.21.x)
dir_$(CONFIG_USER_IPTABLES_IPTABLES)        += iptables-1.4.0rc1
dir_$(CONFIG_USER_IPTABLES_IP6TABLES)       += iptables-1.4.0rc1
else #linux 2.4
dir_$(CONFIG_USER_IPTABLES_IPTABLES)        += iptables
dir_$(CONFIG_USER_IPTABLES_IP6TABLES)       += iptables
endif

dir_$(CONFIG_USER_IXIA_ENDPOINT)	    += ixia_endpoint
dir_$(CONFIG_USER_INADYN)                   += inadyn
dir_$(CONFIG_USER_IGMP_PROXY)  		    += igmpproxy
dir_$(CONFIG_USER_IPROUTE2)  		    += iproute2-2.6.24-rc7
dir_$(CONFIG_USER_LMBENCH3)		    += lmbench3
dir_$(CONFIG_USER_LLTD)			    += lldt
dir_$(CONFIG_USER_LSUSB)		    += lsusb
dir_$(CONFIG_USER_MINIUPNPD)		    += miniupnpd-1.1
dir_$(CONFIG_USER_MTDUTILS)                 += mtd-utils
dir_$(CONFIG_USER_MTD_WRITE)         	    += mtd_write
dir_$(CONFIG_USER_WRITE_FW_TO_FLASH)         	    += wfw2flash
dir_$(CONFIG_USER_UP_AUTOTEST_CONF)			+= upautotestconf
dir_$(CONFIG_USER_MKDOSFS)                  += mkdosfs-2.11
dir_$(CONFIG_USER_NETCAT)		    += netcat
dir_$(CONFIG_USER_NETSTAT_NAT)		    += netstat-nat
dir_$(CONFIG_USER_NTPCLIENT_NTPCLIENT)      += ntpclient
dir_$(CONFIG_USER_NTPCLIENT_ADJTIMEX)       += ntpclient
dir_$(CONFIG_USER_NETSNMP)                  += net-snmp-5.1.2
dir_$(CONFIG_USER_NTFS3G)		    += ntfs-3g
dir_$(CONFIG_USER_NTFSPROGS)                += ntfsprogs
dir_$(CONFIG_USER_OPENSWAN)                 += openswan-2.4.9
dir_$(CONFIG_USER_PORTMAP)                  += portmap
dir_$(CONFIG_USER_PPPOECD)                  += ppp-2.4.1/pppoecd
dir_$(CONFIG_USER_PPPD)                     += ppp-2.4.2
dir_$(CONFIG_USER_PPPD_PPPSTATS)            += ppp-2.4.2
dir_$(CONFIG_USER_PPPD_PPPDUMP)             += ppp-2.4.2
dir_$(CONFIG_USER_PPTP_CLIENT)              += pptp-client
dir_$(CONFIG_USER_PCIUTIL_LSPCI)            += pciutils-3.0.0
dir_$(CONFIG_USER_PCIUTIL_SETPCI)           += pciutils-3.0.0
dir_$(CONFIG_USER_P910ND)                   += p910nd-0.91
dir_$(CONFIG_USER_PRINTER)                  += draytek-lpd
dir_$(CONFIG_USER_RADVD)                    += radvd-1.0
dir_$(CONFIG_RALINKAPP)		    	    += rt2880_app
dir_$(CONFIG_USER_RP_L2TP)		    += rp-l2tp-0.4
dir_$(CONFIG_USER_RPPPPOE_RELAY)  	    += rp-pppoe-3.8
dir_$(CONFIG_USER_RPPPPOE_SNIFF)  	    += rp-pppoe-3.8
dir_$(CONFIG_USER_STRACE_STRACE)            += strace
dir_$(CONFIG_USER_STUPID_FTPD)              += stupid-ftpd
dir_$(CONFIG_USER_SAMBA)		    += samba-3.0.2
dir_$(CONFIG_USER_TOTD)                     += totd-1.5
dir_$(CONFIG_USER_TCPDUMP)                  += tcpdump
dir_$(CONFIG_USER_UVC_STREAM)		    += uvc_stream
dir_$(CONFIG_USER_UPNP_IGD)                 += linux-igd
dir_$(CONFIG_USER_WIRELESS_TOOLS)           += wireless_tools
dir_$(CONFIG_USER_WATCHDOG)           	    += watchdog-5.6
dir_$(CONFIG_USER_WPA_SUPPLICANT)	    += wpa_supplicant-0.5.7
dir_$(CONFIG_USER_WSC)			    += wsc_upnp

ifeq ($(CONFIG_CHIP_MT7615E),y)
dir_$(CONFIG_USER_BNDSTRG)                  += bndstrg_sdk5030
else
dir_$(CONFIG_USER_BNDSTRG)                  += bndstrg
endif

dir_$(CONFIG_USER_ETHTOOL)		    += ethtool
dir_$(CONFIG_USER_ZEBRA)		    += zebra-0.95a_ripd
dir_$(CONFIG_USER_USBMODESWITCH)	    += usb_modeswitch-0.9.5
dir_$(CONFIG_USER_COMGT)		    += comgt-0.32
dir_$(CONFIG_USER_SDPARM) 		    += sdparm-1.02
dir_$(CONFIG_USER_FLOCK) 		    += flock
dir_$(CONFIG_USER_3G)	 		    += hso-1.6
dir_$(CONFIG_USER_USB_APP)	 		    += usb_app
dir_$(CONFIG_USER_3G_BACKUP)	 		    += 3g_backup
dir_$(CONFIG_USER_FREERADIUS)	 	+= $(FREERADIUS_FOLDER_NAME)
dir_$(CONFIG_USER_MADWIMAX)	 	+= madwimax-0.1.1
dir_y							+= dray_fwup
dir_y							+= dray_ap_detect
dir_y							+= dray_ap_discovery
dir_$(CONFIG_USER_DRAY_STA_INFO)	+= dray_sta_info
dir_$(CONFIG_USER_DRAY_RS)		+= dray_rs
dir_y							+= dray_loop
#dir_y							+= dray_stanley
dir_y							+= dray_traffic
dir_y							+= dray_keepalive
dir_$(CONFIG_PARPROUTED)	 	+= parprouted-0.7
dir_$(CONFIG_DHCP_FORWARD)	 	+= dhcp-forwarder-0.8
dir_$(CONFIG_UDP_BROADCAST_RELAY)	 	+= udp-broadcast-relay-0.3
dir_$(CONFIG_USER_USB_TEMPER)			+= temper
dir_$(CONFIG_USER_ATH_TOOL)		+= ath_tools

ifndef CONFIG_USER_DRAYTEK_WL_CENTER
# needn't mdm application cause mdm feature is control by draytek_wireless_center -- Tess
dir_$(CONFIG_USER_MDM)			+= dray_mdm
endif
dir_$(CONFIG_USER_DRAY_INTERFERENCE_MONITOR)	+= dray_interference_daemon
ifeq ($(CONFIG_USER_DRAYTEK_MESH_NETWORK), y)
dir_y							+= vigor_mesh
ifneq ($(CONFIG_MODEL), AP802)
dir_y							+= tinyxml-2.6.2
dir_y							+= dray_acs
endif
endif
ifeq ($(CONFIG_MODEL), AP802)
dir_$(CONFIG_USER_BATMAN_CONTROL)		+= batctl-2010.1.0
else
dir_$(CONFIG_USER_BATMAN_CONTROL)		+= batctl-2014.4.0
endif
dir_$(CONFIG_USER_DRAY_APM)		+= dray_apm
dir_$(CONFIG_USER_QCA11RAPM)	+= qca11rapm
dir_$(CONFIG_USER_RF_SCAN)		+= dray_rf_scan
dir_$(CONFIG_USER_DRAY_LIC_CHK)		+= dray_lic_chk
ifeq ($(CONFIG_USER_RWD), INTEGRATE_LIGHTTPD_TO_GOAHEAD)
dir_y                           += lighttpd-1.4.20
ifeq ("$(CONFIG_PRODUCT)","MT7621")
dir_y					+= dray_radio_info iperf-3.0.11 iperf-2.0.5
else ifeq ("$(CONFIG_MODEL)","AP912C")
dir_y					+= dray_radio_info iperf-3.0.11 iperf-2.0.5
else ifeq ("$(CONFIG_PRODUCT)", "IPQ8074")
dir_y					+= dray_radio_info iperf-3.0.11 iperf-2.0.5
endif

dir_$(CONFIG_USER_DWC_DRV_CTL) += dray_dwc_drv_ctl

dir_$(CONFIG_USER_AP_RADIO_INFO) += dray_radio_info
dir_$(CONFIG_USER_IPERF) += iperf-3.0.11 iperf-2.0.5

ifeq ($(CONFIG_MODEL), AP802)
dir_y					+= dray_radio_info
endif

dir_$(CONFIG_USER_EXTERNAL_WEB_PORTAL)		+= dray_radiusc
dir_$(CONFIG_USER_EXTERNAL_WEB_PORTAL)		+= dray_hotspot-web-portal
dir_$(CONFIG_USER_DRAY_FLASH_DATA)			+= flash_data

endif

dir_$(CONFIG_USER_XMPP_SERVICE)	+= libstrophe_master
dir_$(CONFIG_USER_XMPP_SERVICE)	+= xmpp_test

#TEST
dir_$(CONFIG_USER_HELLO_WORLD)		    += hello_world

dir_$(MTK_2G)					+= mtk_wds_test
#--
dir_$(CONFIG_USER_MDNS_REPEATER)        += mdns-repeater
dir_$(CONFIG_USER_GDB_DEBUG) += gdb-6.8

dir_$(INTEL_2G) += dray_genwificonf

all: config
	$(MAKE) -j$(HOST_NCPU) $(dir_y) $(dir_v) $(dir_p) || exit $$?

#
# add directory dependancies here
#
busybox: $(CONFIG_USER_OPENSSL_FOLDER_NAME)

goahead: $(CONFIG_USER_OPENSSL_FOLDER_NAME)

tr069: $(CONFIG_USER_OPENSSL_FOLDER_NAME)

dray_rs: $(CONFIG_USER_OPENSSL_FOLDER_NAME)

freeradius-1.1.6: $(CONFIG_USER_OPENSSL_FOLDER_NAME)

lighttpd-1.4.20: json-c-0.12

# add dependency for ath_tools to make sure openssl is built before ath_tools
ifeq ($(CONFIG_USER_ATH_TOOL), y)
ath_tools: $(CONFIG_USER_OPENSSL_FOLDER_NAME)
endif

.PHONY: $(sort $(dir_y) $(dir_p))

$(sort $(dir_y) $(dir_p)):  config
	[ ! -d "$@" ] || ( touch $@/.sgbuilt_user && $(MAKE) -j1 -C $@ ) || exit $$?

%_only:
	touch $(@:_only=)/.sgbuilt_user && $(MAKE) -j1 -C $(@:_only=)

%_clean:
	$(MAKE) -j1 -C $(@:_clean=) clean; rm -f $(@:_clean=)/.sgbuilt_user; true

romfs:
	for i in $(sort $(dir_y)) $(dir_p) ; do \
		[ ! -d $$i ] || $(MAKE) -C $$i romfs || exit $$? ; \
	done
	ln -sf ../bin/busybox $(ROMFSDIR)/sbin/udhcpc_apcli

ifeq ("$(CONFIG_USER_GOAHEAD_GDB_DEBUG)", "y")
	cp $(ROOTDIR)/user/goahead/src/goahead $(ROMFSDIR)/bin/
endif

clean:
	-for i in $(dir_v) $(sort $(dir_y) $(dir_n) $(dir_)) $(dir_p) ; do \
		if [ -f $$i/.sgbuilt_user ]; then \
			$(MAKE) -C $$i clean ; \
			rm -f $$i/.sgbuilt_user; \
		fi; \
	done

prune:
	-for i in $(sort $(dir_n) $(dir_)) ; do \
		found=0; \
		for j in $(sort $(dir_y)) ; do \
			if [ $$i == $$j ]; then \
				found=1; \
			fi; \
		done; \
		if [ $$found == 0 ]; then \
			[ "$$i" = "openswan" ] && make prune_openswan; \
			rm -fr $$i; \
		fi; \
	done

prune_openswan:
	@mkdir $(ROOTDIR)/$@
	@mv $(ROOTDIR)/openswan/linux/net/ipsec/{Config.in*,Kconfig} $(ROOTDIR)/$@/
	@rm -fr $(ROOTDIR)/openswan
	@mkdir -p $(ROOTDIR)/openswan/linux/net/ipsec
	@mv $(ROOTDIR)/$@/* $(ROOTDIR)/openswan/linux/net/ipsec/
	@rm -fr $(ROOTDIR)/$@
