#***********************************************************************
#
#  Copyright (c) 2006  Broadcom Corporation
#  All Rights Reserved
#
# 
# 
# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License, version 2, as published by  
# the Free Software Foundation (the "GPL"). 
# 
#
# 
# This program is distributed in the hope that it will be useful,  
# but WITHOUT ANY WARRANTY; without even the implied warranty of  
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
# GNU General Public License for more details. 
#  
# 
#  
#   
# 
# A copy of the GPL is available at http://www.broadcom.com/licenses/GPLv2.php, or by 
# writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
# Boston, MA 02111-1307, USA. 
#
#
#***********************************************************************/

#
# 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 ppp sshd ipsec-tools 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: sanity_check
	(tar xkjf ipsec-tools.tar.bz2 2> /dev/null || true)
	$(MAKE) -C ipsec-tools $(BUILD_IPSEC_TOOLS)
else
ipsec-tools: 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_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


