#
#	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 $(ARCH_CONFIG)
-include $(MODULES_CONFIG)

VEND=$(ROOTDIR)/vendors

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

dir_y = 
dir_n =
dir_  =

#dir_y += mini_httpd-1.19
dir_y += ipc_agent
#dir_y += utelnetd-0.1.1
dir_y += wizard
dir_y += config_server
#dir_y += user_test
dir_y += soft_wd
dir_y += procps-3.2.7
dir_y += bw_measure
#dir_y += net-snmp-5.4.2.1
dir_y += mkimage

dir_$(CONFIG_USER_LZO) 				+= lzo-2.03
dir_$(CONFIG_USER_DNRD) 			+= dnrd-2.20.3
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_BUSYBOX_BUSYBOX)          += busybox
dir_$(CONFIG_USER_CTORRENT)                 += ctorrent-dnh3.2
dir_$(CONFIG_USER_DHCP6)                    += dhcp6
dir_$(CONFIG_USER_DNSMASQ)                  += dnsmasq-2.40
dir_$(CONFIG_USER_DISKTYPE)		    += disktype-9
dir_$(CONFIG_USER_ECHO_SERVER)		+= echo_server
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_GOAHEAD_HTTPD)            += goahead
ifeq ($(CONFIG_LINUXDIR), linux-2.6.21.x)
#ifeq ($(CONFIG_RALINKAPP_SWQOS),y)
dir_$(CONFIG_USER_IPTABLES_IPTABLES)		+= iptables-1.4.0rc1
dir_$(CONFIG_USER_IPTABLES_IP6TABLES)		+= iptables-1.4.0rc1
#else
#dir_$(CONFIG_USER_IPTABLES_IPTABLES)		+= iptables
#dir_$(CONFIG_USER_IPTABLES_IP6TABLES)		+= iptables
#endif
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_LMBENCH3)			    += lmbench3
dir_$(CONFIG_LLDT)			    += lldt
dir_$(CONFIG_USER_MTDUTILS)                 += mtd-utils
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-2007
dir_$(CONFIG_USER_NTPCLIENT_ADJTIMEX)       += ntpclient-2007
dir_$(CONFIG_USER_NTFS3G)		    += ntfs-3g
dir_$(CONFIG_USER_OPENSWAN)                 += openswan-2.4.9
dir_$(CONFIG_USER_OPENSSL)	   	    += openssl-0.9.8e
dir_$(CONFIG_USER_PORTMAP)                  += portmap
dir_$(CONFIG_USER_PPPD)                     += ppp-2.4.4
dir_$(CONFIG_USER_PPPD_PPPSTATS)            += ppp-2.4.4
dir_$(CONFIG_USER_PPPD_PPPDUMP)             += ppp-2.4.4
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_RADVD)                    += radvd-1.0
dir_$(CONFIG_RT2880_APP)		    += 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_OPENVPN) 	+= openvpn-2.1_rc12
dir_$(CONFIG_USER_OPENSSH) 	+= openssh-3.4p1

#
#	Because tcpdump-3.4 takes fewer space. we use 3.4
#
#dir_$(CONFIG_USER_TCPDUMP)                  += tcpdump
#dir_$(CONFIG_USER_TCPDUMP)                  += tcpdump-3.9.8
dir_$(CONFIG_USER_TCPDUMP)                  += tcpdump-3.4

dir_$(CONFIG_USER_USHARE)		    += ushare-1.1a
dir_$(CONFIG_USER_UPNP_IGD)                 += linux-igd
dir_$(CONFIG_USER_WIRELESS_TOOLS)           += wireless_tools
dir_$(CONFIG_USER_WPA_SUPPLICANT)	    += wpa_supplicant-0.5.7
dir_$(CONFIG_USER_WSC)			    += wsc_upnp
dir_$(CONFIG_USER_ETHTOOL)		    += ethtool
dir_$(CONFIG_USER_ZEBRA)		    += zebra-0.95a_ripd

#TEST
dir_$(CONFIG_USER_HELLO_WORLD)		    += hello_world
#--

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

#	$(MAKE) -j$(HOST_NCPU) $(sort $(dir_y) $(dir_v) $(dir_p)) || exit $$?


#
# add directory dependancies here
#
.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

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)/$@

%_all:
	@if [ -d $(patsubst %_all, %, $@) ]; then \
		touch $(patsubst %_all, %, $@)/.sgbuilt_user; \
		make -C $(patsubst %_all, %, $@) || exit $$? ; \
	fi

%_gpl:
	@if [ -d $(patsubst %_gpl, %, $@) ]; then \
		make -C $(patsubst %_gpl, %, $@) gpl || exit $$? ; \
	fi

gpl:
	@mkdir -p .GPL_RELEASE_DIR
	@for i in $(dir_y) $(dir_v) $(dir_p); \
		do \
		if [ -e $$i/GPL_RELEASE ]; then \
			$(MAKE) -C $$i gpl; \
		fi; \
		done
	@cd .GPL_RELEASE_DIR; for i in $(dir_y) $(dir_v) $(dir_p); \
		do \
		if [ -e ../$$i ]; then \
			mkdir -p $$i; \
			rm -rf $$i; \
			mv ../$$i $$i; \
		fi; \
		done
	@mv Makefile .GPL_RELEASE_DIR/
	@rm -rf *
	@mv .GPL_RELEASE_DIR/* .
	@rm -rf .GPL_RELEASE_DIR

#%_gpl:
#	for i in $(dir_y) $(dir_v) $(dir_p); do \
#		if [ -e $$i/.dist ]; then \
#			make -C $$i gpl || exit $$? ; \
#		fi; \
#	done

%_clean:
	@if [ -d $(patsubst %_clean, %, $@) ]; then \
		if [ -f $(patsubst %_clean, %, $@)/.sgbuilt_user ]; then \
			make -C $(patsubst %_clean, %, $@) clean ; \
			rm -f $(patsubst %_clean, %, $@)/.sgbuilt_user; \
		fi; \
	fi

%_romfs:
	@if [ -d $(patsubst %_romfs, %, $@) ]; then \
		make -C $(patsubst %_romfs, %, $@) romfs || exit $$? ; \
	fi

