#***********************************************************************
#
#  Copyright (c) 2006  Broadcom Corporation
#  All Rights Reserved
#
# 
# 
# Unless you and Broadcom execute a separate written software license 
# agreement governing use of this software, this software is licensed 
# to you under the terms of the GNU General Public License version 2 
# (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, 
# with the following added to such license:
# 
#    As a special exception, the copyright holders of this software give 
#    you permission to link this software with independent modules, and 
#    to copy and distribute the resulting executable under terms of your 
#    choice, provided that you also meet, for each linked independent 
#    module, the terms and conditions of the license of that module. 
#    An independent module is a module which is not derived from this
#    software.  The special exception does not apply to any modifications 
#    of the software.  
# 
# Not withstanding the above, under no circumstances may you combine 
# this software in any way with any other Broadcom software provided 
# under a license other than the GPL, without Broadcom's express prior 
# written consent. 
#
#
#***********************************************************************/

#
# To add an app, add it to APP_DIRS, and write the target rule in the
# last section of this Makefile
#
APP_DIRS = lighttpd-1.4.20 smartlife ppp sshd ipsec-tools-0.8.1 ledctl dhrystone tcpdump flash_write

#
# 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) pppd


#
# Cleaning rules
# See example in GNU Make, 4.6
# (ppp does not have a top level Makefile, so we can't just cd into
# and do a make clean.  Remove ppp from APP_DIRS and write out
# an explicit rule.)
#
clean: 
	for dir in $(subst ppp,,$(APP_DIRS)); do \
		$(MAKE) -C $$dir clean; \
	done
	$(MAKE) -C ppp/pppoe clean


#
# 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

ifneq ($(strip $(BUILD_VODSL)),)
include $(BUILD_DIR)/make.voice
endif

export BUILD_DIR
export CFLAGS


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

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

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


ifneq ($(strip $(BUILD_PPPD)),)
pppd: sanity_check
	$(MAKE) -C ppp/pppoe $(BUILD_PPPD)
else
pppd: sanity_check
	@echo "skipping pppd (not configured)"
endif

ifneq ($(strip $(BUILD_TCPDUMP)),)
tcpdump: sanity_check
# tcpdump is dependent on libpcap
	$(MAKE) -C libpcap
	$(MAKE) -C tcpdump install
else
tcpdump: sanity_check
	@echo "skipping tcpdump (not configured)"
endif
ifneq ($(strip $(BUILD_IPSEC_TOOLS)),)
ipsec-tools-0.8.1: sanity_check
#	(tar xkjf ipsec-tools.tar.bz2 2> /dev/null || true)
	cp -f ./ipsec-tools-0.8.1/libfl.a $(TOOLCHAIN)/lib
	$(MAKE) -C ipsec-tools-0.8.1 $(BUILD_IPSEC_TOOLS)
else
ipsec-tools-0.8.1: sanity_check
	@echo "skipping ipsec-tools (not configured)"
endif

ifneq ($(strip $(BUILD_LEDCTL)),)
ledctl: sanity_check
	$(MAKE) -C $@
else
ledctl: sanity_check
	@echo "skipping $@ (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 --enable-static=no --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 --enable-static=no --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_SMARTLIFE)),)
smartlife:
	cp -f $(PROFILE_DIR)/hf_zigbee/sbin/lighttpd $(INSTALL_DIR)/sbin/
	cp -f $(PROFILE_DIR)/hf_zigbee/sbin/lighttpd-angel $(INSTALL_DIR)/sbin/
	cp -f $(PROFILE_DIR)/hf_zigbee/lib/*.so $(INSTALL_DIR)/lib/
	cp -f $(PROFILE_DIR)/hf_zigbee/lib/*.la $(INSTALL_DIR)/lib/
else
smartlife:
	@echo "Skipping SmartLife APP "
endif

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 $(BENCHMARKS_SMP)),)
dhrystone: sanity_check
	$(MAKE) -C $@
else
dhrystone: sanity_check
	@echo "skipping dhrystone (not configured)"
endif

#ifneq ($(strip $(ANSWER_MACHINE_SUPPORT)),)
#flash_write: sanity_check
#	$(MAKE) -C $@
#else
flash_write: sanity_check
	@echo "skipping flash_write (not configured)"
#endif


