#
# To add an app, add it to APP_DIRS, and write the target rule in the
# last section of this Makefile
#
APP_DIRS = atm2684 bridge-utils busybox dproxy-nexgen ebtables bftpd \
           iproute2 iptables radvd udhcp urlfilterd zebra avm_samba\
           dynahelper mtd-utils ntfs-3g lighttpd-1.4.20 conntrack openl2tpd rng-tools-2 \
           pptp usb_modeswitch-1.0.5

#
# We need to declare these directory names as phony targets,
# otherwise make will think the directories themselves are
# the targets and will never execute the cmds.
# See GNU Make, 4.6 
#
.PHONY : $(APP_DIRS)


# Some apps have names that are different from their directory
# name, so need to list them explictly here.
all : $(APP_DIRS) brctl dproxy


#
# Cleaning rules
# See example in GNU Make, 4.6
#
ifneq ($(strip $(BUILD_SAMBA)),)
clean:samba_clean
else
clean:
endif
	for dir in $(APP_DIRS); do \
		if [ -d $$dir ] ; then \
			$(MAKE) -C $$dir clean; \
		fi \
	done


#
# Set our CommEngine directory (by splitting the pwd into two words
# at /userspace and taking the first word only).
# Then include the common defines under CommEngine.
# You do not need to modify this part.
#
CURR_DIR := $(shell pwd)
BUILD_DIR:=$(subst /userspace, /userspace,$(CURR_DIR))
BUILD_DIR:=$(word 1, $(BUILD_DIR))

include $(BUILD_DIR)/make.common

export BUILD_DIR



##############################################################
# 
# Rules for inidividual apps
# apps can be compiled or not depending on the profile (which
# is modified via make menuconfig)
#
##############################################################


ifneq ($(strip $(BUILD_BRCTL)),)
brctl: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfj bridge-utils.tar.bz2 2> /dev/null || true)
	$(MAKE) -C bridge-utils $(BUILD_BRCTL)
else
brctl: sanity_check
endif


ifeq ($(strip $(BRCM_IKOS)),y)
BUSYBOX_CONFIG=ikos.config
else
BUSYBOX_CONFIG=brcm.config
endif

ifneq ($(strip $(BUILD_BUSYBOX)),)
busybox: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfj busybox.tar.bz2 2> /dev/null || true)
	cd busybox; cp -f $(BUSYBOX_CONFIG) .config
	$(MAKE) -C busybox install
else
busybox: sanity_check
	@echo "skipping busybox (not configured)"
endif


ifneq ($(strip $(BUILD_DPROXY)),)
dproxy: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfj dproxy-nexgen.tar.bz2 2> /dev/null || true)
	$(MAKE) -C dproxy-nexgen $(BUILD_DPROXY)
else
dproxy: sanity_check
	@echo "skipping dproxy (not configured)"
endif


# in userapps->Dynamic ALG loader
ifneq ($(strip $(BUILD_DYNAHELPER)),)
dynahelper: sanity_check
	$(MAKE) -C dynahelper $(BUILD_DYNAHELPER)
else
dynahelper: sanity_check
	@echo "skipping dynahelper (not configured)"
endif


ifneq ($(strip $(BUILD_EBTABLES)),)
ebtables: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfj ebtables.tar.bz2 2> /dev/null || true)
	$(MAKE) -C ebtables $(BUILD_EBTABLES)
else
ebtables: sanity_check
	@echo "skipping ebtables (not configured)"
endif


# ftpd does sanity check in its own Makefile
ifneq ($(strip $(BUILD_FTPD)),)
bftpd:
	
	$(MAKE) -C bftpd 
else
bftpd:
	@echo "skipping ftpd (not configured)"
endif

# Build the iproute2 package whenever IPROUTE2, IPV6, or BRCM_KERNEL_NETQOS
# is defined.  In make menuconfig, "traffic control" enables BRCM_KERNEL_NETQOS
ifneq ($(strip $(BUILD_IPROUTE2)),)
DO_BUILD_IPROUTE2=y
endif

ifneq ($(strip $(BUILD_IPV6)),)
DO_BUILD_IPROUTE2=y
endif

ifneq ($(strip $(BRCM_KERNEL_NETQOS)),)
DO_BUILD_IPROUTE2=y
endif

ifneq ($(strip $(DO_BUILD_IPROUTE2)),)
iproute2: sanity_check
	(tar xkfj iproute2.tar.bz2 2> /dev/null || true)
	$(MAKE) -C iproute2 dynamic
else
iproute2: sanity_check
	@echo "skipping iproute2 (not configured)"
endif


# iptables is dependent on kernel netfilter modules
ifneq ($(strip $(BRCM_KERNEL_NETFILTER)),)
ifneq ($(strip $(BUILD_IPTABLES)),)
iptables: sanity_check
	(tar xkfj iptables.tar.bz2 2> /dev/null || true)
	$(MAKE) -C iptables $(BUILD_IPTABLES)
else
iptables: sanity_check
	@echo "skipping iptables (not configured)"
endif
else
iptables: sanity_check
	@echo Warning: To use iptables, you need to enable netfilter in the kernel !!!!!
endif


ifneq ($(strip $(BUILD_IPTABLES)),)
ifneq ($(strip $(BUILD_URLFILTER)),)
urlfilterd: sanity_check
	$(MAKE) -C urlfilterd $(BUILD_URLFILTER)
else
urlfilterd: sanity_check
endif
else
urlfilterd: sanity_check
	@echo Warning: To use urlfilterd, you need to enable iptables!!!!!
endif



ifneq ($(strip $(BUILD_IPV6)),)
radvd: sanity_check
	(tar xkjf radvd.tar.bz2 2> /dev/null || true)
	$(MAKE) -C radvd
else
radvd: sanity_check
	@echo "skipping radvd (not configured)"
endif



ifneq ($(strip $(BUILD_UDHCP)),)
udhcp: sanity_check
	$(MAKE) -C udhcp $(BUILD_UDHCP)
else
udhcp: sanity_check
	@echo "skipping udhcp (not configured)"
endif


ifneq ($(strip $(BUILD_ZEBRA)),)
zebra: sanity_check
	(tar xkfj zebra.tar.bz2 2> /dev/null || true)
	$(MAKE) -C zebra $(BUILD_ZEBRA)
else
zebra: sanity_check
	@echo "skipping zebra (not configured)"
endif

ifneq ($(strip $(BUILD_MTDUTILS)),)
mtd-utils: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfz mtd-utils.tar.gz 2> /dev/null || true)
	(cd mtd-utils; $(MAKE) install)
else
mtd-utils: sanity_check
	@echo "skipping mtd-utils (not configured)"
endif

ifneq ($(strip $(BUILD_NTFS_3G)),)
ntfs-3g: sanity_check
	(tar xkfz ntfs-3g.tar.gz 2> /dev/null || true)
	$(MAKE) -C ./ntfs-3g 
	install -m 755 ./ntfs-3g/libntfs-3g/.libs/libntfs-3g.so.54 $(INSTALL_DIR)/lib/libntfs-3g.so.54
	install -m 755 ./ntfs-3g/src/.libs/ntfs-3g  $(INSTALL_DIR)/bin/ntfs-3g	
else
ntfs-3g: sanity_check
	@echo "skipping $@ (not configured)"
endif

ifneq ($(strip $(BUILD_SAMBA)),)
#samba: sanity_check                                                                                                                                      
#	$(MAKE) -C samba 
#	@echo Done with samba
#else
#samba: sanity_check
#	@echo "skipping Samba (not configured)"
#endif
samba_make:
	cd ./avm_samba/source; make all; make bin/CP850.so
        
samba_install:
	install -d $(INSTALL_DIR)/bin;
	cd ./avm_samba/source/bin; cp nmbd smbd smbpasswd CP850.so $(INSTALL_DIR)/bin;
 
samba_clean:
	cd ./avm_samba/source; make clean     
 
avm_samba:samba_make samba_install
	@echo Done with samba
 
else
avm_samba: sanity_check
	@echo "skipping Samba (not configured)"
endif

#added by saxon.tseng
ifneq ($(strip $(BUILD_WEBDAV)),)
lighttpd-1.4.20:
# complier pcre first
#	if [ ! -f pcre-7.8/config.log ]; then \
	  echo configure pcre-7.8 first...;\
          cd pcre-7.8; (./configure --host=mips-linux-uclibc --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR);); cd ../; \
        else \
          echo have already been configured, just make and install...; \
        fi
	echo configure pcre-7.8 first...;\
        cd pcre-7.8; (./configure --host=mips-linux-uclibc --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR);); cd ../;
	$(MAKE) -C pcre-7.8
	$(MAKE) -C pcre-7.8 install
# complier libxml2-2.6.32 first
#       cd $(OPENSOURCE_DIR)/libxml2-2.6.32; (./configure --host=mips-linux-uclibc --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR);)
#       $(MAKE) -C $(OPENSOURCE_DIR)/libxml2-2.6.32
#       $(MAKE) -C $(OPENSOURCE_DIR)/libxml2-2.6.32 install
# complier sqlite-3.6.4 first
#       cd $(OPENSOURCE_DIR)/sqlite-3.6.4; (./configure --host=mips-linux-uclibc --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR);)
#       $(MAKE) -C $(OPENSOURCE_DIR)/sqlite-3.6.4
#       $(MAKE) -C $(OPENSOURCE_DIR)/sqlite-3.6.4 install
#only when distclean need to do once
#	if [ ! -f lighttpd-1.4.20/config.log ]; then \
	  echo configure lighttpd-1.4.20 first...;\
          cd lighttpd-1.4.20; (./configure --with-webdav-locks --without-zlib --with-pcre --without-bzip2 --host=mips-linux-uclibc PCRECONFIG='`pcre-config`' PCRE_LIB='`pcre-config --libs`' CFLAGS="-DHAVE_PCRE_H=1 -DHAVE_LIBPCRE=1 -I$(INSTALL_DIR)/include -L$(INSTALL_DIR)/lib" --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR););cd ../;\
        else \
          echo have already been configured, just make and install... ;\
        fi
# complier lighttpd-1.4.20
	echo configure lighttpd-1.4.20 first...;\
        cd lighttpd-1.4.20; (./configure --with-webdav-locks --without-zlib --with-pcre --without-bzip2 --host=mips-linux-uclibc PCRECONFIG='`pcre-config`' PCRE_LIB='`pcre-config --libs`' CFLAGS="-DHAVE_PCRE_H=1 -DHAVE_LIBPCRE=1 -I$(INSTALL_DIR)/include -L$(INSTALL_DIR)/lib" --libdir=$(INSTALL_DIR)/lib/ --includedir=$(INSTALL_DIR)/include/ --prefix=$(INSTALL_DIR););cd ../;
	$(MAKE) -C lighttpd-1.4.20 -I$(INSTALL_DIR)/include/
	$(MAKE) -C lighttpd-1.4.20 install
	install -m 755 lighttpd-1.4.20/doc/lighttpd.conf $(INSTALL_DIR)/etc/lighttpd.conf.org
#remove pcre-config to prevent strip, add this script after buildimage run buildFS
	rm -rf $(INSTALL_DIR)/bin/pcre-config
else
lighttpd-1.4.20:
endif
#end

ifneq ($(strip $(BUILD_L2TPAC)),)
openl2tpd: sanity_check
	(tar xkjf openl2tpd.tar.bz2 2> /dev/null || true)
	$(MAKE) -C openl2tpd
else
openl2tpd: sanity_check
	@echo "skipping openl2tpd (not configured)"
endif

ifneq ($(strip $(BUILD_RNGD)),)
rng-tools-2: sanity_check
	mkdir -p $(INSTALL_DIR)/bin
	(tar xkfz rng-tools-2.tar.gz 2> /dev/null || true)
	($(MAKE) -C rng-tools-2 $(BUILD_RNGD))
else
rngd: sanity_check
	@echo "skipping rng-tools-2 (not configured)"
endif

ifneq ($(strip $(BUILD_PPTPAC)),)
pptp: sanity_check
	(tar xkjf pptp.tar.bz2 2> /dev/null || true)
	$(MAKE) -C pptp
else
pptp: sanity_check
	@echo "skipping pptp (not configured)"
endif

ifneq ($(strip $(BUILD_CONNTRACK_TOOLS)),)
conntrack: sanity_check
	(tar xkfz conntrack.tar.gz 2> /dev/null || true)
	$(MAKE) -C ./conntrack
else
conntrack: sanity_check
	@echo "skipping $@ (not configured)"
endif

ifneq ($(strip $(BUILD_3G)),)
usb_modeswitch-1.0.5:
	$(MAKE) -C usb_modeswitch-1.0.5
else
usb_modeswitch-1.0.5:
endif
