#
# Broadcom Linux Router Makefile
#
# Copyright 2001-2003, Broadcom Corporation
# All Rights Reserved.
#
# THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
# KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
# SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
#
# $Id: Makefile,v 1.30 2003/12/31 00:43:12 sparq Exp $
#

include .config
ifneq ($(wildcard ../cy_conf.mak),)
  include ../cy_conf.mak
endif

#
# Paths
#

# Source bases
export PLATFORM LINUXDIR LIBDIR USRLIBDIR
export TOP := $(shell pwd)
export SRCBASE := $(shell (cd $(TOP)/.. && pwd -P))

#
# Cross-compile environment variables
#

# Build platform
export BUILD := i386-pc-linux-gnu
export HOSTCC := gcc

# uClibc wrapper
ifeq ($(CONFIG_UCLIBC),y)
export PLATFORM := $(PLATFORM)-uclibc
endif

ifeq ($(PLATFORM),mipsel)
export CROSS_COMPILE := mipsel-linux-
export CONFIGURE := ./configure mipsel-linux --build=$(BUILD)
export TOOLCHAIN := $(shell cd $(dir $(shell which $(CROSS_COMPILE)gcc))/../mipsel-linux && pwd -P)
endif

ifeq ($(PLATFORM),mipsel-uclibc)
export CROSS_COMPILE := mipsel-uclibc-
export CONFIGURE := ./configure mipsel-linux --build=$(BUILD)
export TOOLCHAIN := $(shell cd $(dir $(shell which $(CROSS_COMPILE)gcc))/.. && pwd -P)
endif


export CC := $(CROSS_COMPILE)gcc
export AR := $(CROSS_COMPILE)ar
export AS := $(CROSS_COMPILE)as
export LD := $(CROSS_COMPILE)ld
export NM := $(CROSS_COMPILE)nm
export RANLIB := $(CROSS_COMPILE)ranlib
export STRIP := $(CROSS_COMPILE)strip
export SIZE := $(CROSS_COMPILE)size

export CFLAGS := -O1
export LDFLAGS := -s

#
# Install and target directories
#

export PLATFORMDIR := $(TOP)/$(PLATFORM)
export INSTALLDIR := $(PLATFORMDIR)/install
export TARGETDIR := $(PLATFORMDIR)/target

#
# Configuration
#
CONFIG_IPTABLES=y
obj-$(CONFIG_OPENSSL) += openssl
obj-$(CONFIG_MATRIXSSL) += matrixssl
obj-$(CONFIG_HTTPD) += httpd
obj-$(CONFIG_RC) += rc
obj-$(CONFIG_SHARED) += shared
obj-$(CONFIG_WWW) += www
obj-$(CONFIG_GLIBC) += lib
obj-$(CONFIG_UCLIBC) += lib
obj-$(CONFIG_WLCONF) += wlconf
obj-$(CONFIG_BRIDGE) += bridge
obj-$(CONFIG_BUSYBOX) += busybox
obj-$(CONFIG_DNSMASQ) += dnsmasq
obj-$(CONFIG_IPTABLES) += iptables
obj-$(CONFIG_LIBIPT) += iptables
obj-$(CONFIG_IPSEC) += ipsec
obj-$(CONFIG_LIBPCAP) += libpcap
obj-$(CONFIG_LIBNET) += net
obj-$(CONFIG_TCPDUMP) += tcpdump
obj-$(CONFIG_KISMETDRONE) += kismet
obj-$(CONFIG_NETSTATNAT) += netstatnat
obj-$(CONFIG_NAS) += nas
obj-$(CONFIG_WPA_SUPPLICANT) += wpa_supplicant
obj-$(CONFIG_NETCONF) += netconf
obj-$(CONFIG_NTP) += ntpclient
obj-$(CONFIG_NVRAM) += nvram
obj-$(CONFIG_PPP) += ppp
obj-$(CONFIG_UDHCPD) += udhcpd
obj-$(CONFIG_UPNP) += upnp
obj-$(CONFIG_UTILS) += utils
obj-$(CONFIG_ETC) += etc
obj-$(CONFIG_VLAN) += vlan
obj-$(CONFIG_DROPBEAR_SSHD) += dropbear
obj-$(CONFIG_IPROUTE2) += iproute2
obj-$(CONFIG_EBTABLES) += ebtables
obj-$(CONFIG_PPTPD) += pptpd
obj-$(CONFIG_PIPSEC) += pipsec
obj-$(CONFIG_FROTTLE) += frottle
obj-$(CONFIG_WOL) += wol
obj-$(CONFIG_SNMP) += snmp
obj-$(CONFIG_RADVD) += radvd
obj-$(CONFIG_RFLOW) += rflow
obj-$(CONFIG_ADM6996) += adm6996
##################################################################
CONFIG_OTHERS=y
obj-$(CONFIG_ADM6996) += adm6996
CONFIG_L2TP=y
obj-$(CONFIG_L2TP) += rp-l2tp

obj-$(CONFIG_CHILLISPOT) += chillispot
obj-$(CONFIG_PARPROUTED) += parprouted
obj-$(CONFIG_HEARTBEAT) += bpalogin
obj-$(CONFIG_TFTPD) += tftpd
obj-$(CONFIG_CRON) += cron
obj-$(CONFIG_PPTP) += pptp-client
obj-$(CONFIG_PPPD) += pppd
obj-$(CONFIG_ZEBRA) += zebra
obj-$(CONFIG_BIRD) += bird
obj-$(CONFIG_DDNS) += ipupdate
obj-$(CONFIG_OTHERS) += others

obj-clean := $(foreach obj,$(obj-y) $(obj-n),$(obj)-clean)
obj-install := $(foreach obj,$(obj-y),$(obj)-install)
obj-distclean := $(foreach obj,$(obj-y) $(obj-n),$(obj)-distclean)

#
# Basic rules
#

all: clean_target $(obj-y) $(LINUXDIR)/.config
	if [ ! -e $(LINUXDIR)/drivers/net/mac/mac.o ] ; then \
	    (cd $(LINUXDIR)/drivers/net/mac ; ln -s ../../../../../wl/linux/mac.o .) ; \
	fi
        # Also build kernel
	( . $(LINUXDIR)/.config ; \
	if [ "$${CONFIG_EMBEDDED_RAMDISK}" = "" ] ; then \
	    $(MAKE) -C $(LINUXDIR) zImage ; \
	fi ; \
	if [ "$${CONFIG_MODULES}" = "y" ] ; then \
	    $(MAKE) -C $(LINUXDIR) modules ; \
	fi )

clean: $(obj-clean)
	rm -f .config.old .config.cmd
	rm -rf $(TARGETDIR)
	rm -f $(PLATFORMDIR)/linux.trx $(PLATFORMDIR)/vmlinuz $(PLATFORMDIR)/target.squashfs

clean_target:
	rm -rf $(TARGETDIR)
	rm -f $(PLATFORMDIR)/linux.trx $(PLATFORMDIR)/vmlinuz $(PLATFORMDIR)/target.squashfs

distclean mrproper: $(obj-distclean) clean_target
	rm -rf $(INSTALLDIR)
	$(MAKE) -C $(LINUXDIR) distclean
	$(MAKE) -C $(LINUXDIR)/arch/mips/brcm-boards/bcm947xx/compressed clean
	rm -f $(PLATFORMDIR)/zImage $(PLATFORMDIR)/linux.bin
	$(MAKE) -C config clean
	rm -f .config $(LINUXDIR)/.config
	rm -f .config.old .config.cmd

install package: $(filter-out lib-install,$(obj-install)) $(LINUXDIR)/.config
        # Install binaries into target directory
	install -d $(TARGETDIR)
	for dir in $(wildcard $(patsubst %,$(INSTALLDIR)/%,$(obj-y))) ; do \
	    (cd $${dir} && tar cpf - .) | (cd $(TARGETDIR) && tar xpf -) \
	done
        # Install (and possibly optimize) C library
	$(MAKE) lib-install
        # Install modules into filesystem
	( . $(LINUXDIR)/.config ; \
	if [ "$${CONFIG_MODULES}" = "y" ] ; then \
	    $(MAKE) -C $(LINUXDIR) modules_install DEPMOD=/bin/true INSTALL_MOD_PATH=$(TARGETDIR) ; \
	fi )
        # quick n dirty - stuff Nikki's module where it belongs
	mkdir $(TARGETDIR)/lib/modules/2.4.20/kernel/drivers/net/adm6996
	cp adm6996/adm6996.o $(TARGETDIR)/lib/modules/2.4.20/kernel/drivers/net/adm6996/adm6996.o
	rm -f $(TARGETDIR)/lib/modules/2.4.20/build
        # Prepare filesystem
	cd $(TARGETDIR) && $(TOP)/misc/rootprep.sh
        # Make sure mksquashfs-2.0 is used
	$(MAKE) -C $(LINUXDIR)/scripts/squashfs mksquashfs
	cd $(TARGETDIR)  &&  find . -iname "CVS" | xargs rm -rf
	$(LINUXDIR)/scripts/squashfs/mksquashfs $(TARGETDIR) $(PLATFORMDIR)/target.squashfs -noappend
        # Package kernel and filesystem
	( . $(LINUXDIR)/.config ; \
	if [ "$${CONFIG_EMBEDDED_RAMDISK}" = "y" ] ; then \
	    cp $(PLATFORMDIR)/target.squashfs $(LINUXDIR)/arch/mips/ramdisk/$${CONFIG_EMBEDDED_RAMDISK_IMAGE} ; \
	    $(MAKE) -C $(LINUXDIR) zImage ; \
	else \
	    cp $(LINUXDIR)/arch/mips/brcm-boards/bcm947xx/compressed/vmlinuz $(PLATFORMDIR)/ ; \
	    trx -o $(PLATFORMDIR)/linux.trx $(PLATFORMDIR)/vmlinuz $(PLATFORMDIR)/target.squashfs ; \
	fi )
#REMOVE
        # Pad self-booting Linux to a 64 KB boundary
	cp $(LINUXDIR)/arch/mips/brcm-boards/bcm947xx/compressed/zImage $(PLATFORMDIR)/
	dd conv=sync bs=64k < $(PLATFORMDIR)/zImage > $(PLATFORMDIR)/linux.bin
        # Append filesystem to self-booting Linux
	cat $(PLATFORMDIR)/target.squashfs >> $(PLATFORMDIR)/linux.bin
#endif
#
# Configuration rules
#
fixkc: $(LINUXDIR)/.config
	cp $(LINUXDIR)/.config /tmp/config.$$$$ ; \
  if [ "$(CONFIG_IPSEC_TUNNEL)" = "y" ] ; then \
    sed -e '/^CONFIG_IPSEC_TUNNEL=.*$$/d' \
				-e '/^CONFIG_CRYPTO=.*$$/d' \
        -e '/^CONFIG_CIPHERS=.*$$/d' \
        -e '/^CONFIG_DIGESTS=.*$$/d' \
        -e '/^CONFIG_CIPHER_DES=.*$$/d' \
        -e '/^CONFIG_CIPHER_3DES=.*$$/d' \
        -e '/^CONFIG_CIPHER_AES=.*$$/d' \
        -e '/^CONFIG_DIGEST_MD5=.*$$/d' \
        -e '/^CONFIG_DIGEST_SHA1=.*$$/d' \
        /tmp/config.$$$$ > /tmp/config.n.$$$$ ; \
    mv -f /tmp/config.n.$$$$ /tmp/config.$$$$ ; \
    echo 'CONFIG_IPSEC_TUNNEL=y' >> /tmp/config.$$$$ ; \
    echo 'CONFIG_CRYPTO=y' >> /tmp/config.$$$$ ; \
    echo 'CONFIG_DIGESTS=y' >> /tmp/config.$$$$ ; \
    echo 'CONFIG_CIPHERS=y' >> /tmp/config.$$$$ ; \
    if [ "$(CONFIG_IPSEC_TUNNEL_MD5)" = "y" ] ; then \
      echo 'CONFIG_DIGEST_MD5=y' >> /tmp/config.$$$$ ; \
    fi ; \
    if [ "$(CONFIG_IPSEC_TUNNEL_SHA1)" = "y" ] ; then \
      echo 'CONFIG_DIGEST_SHA1=y' >> /tmp/config.$$$$ ; \
    fi ; \
    if [ "$(CONFIG_IPSEC_TUNNEL_DES)" = "y" ] ; then \
      echo 'CONFIG_CIPHER_DES=y' >> /tmp/config.$$$$ ; \
    fi ; \
    if [ "$(CONFIG_IPSEC_TUNNEL_3DES)" = "y" ] ; then \
      echo 'CONFIG_CIPHER_3DES=y' >> /tmp/config.$$$$ ; \
    fi ; \
    if [ "$(CONFIG_IPSEC_TUNNEL_AES)" = "y" ] ; then \
      echo 'CONFIG_CIPHER_AES=y' >> /tmp/config.$$$$ ; \
    fi ; \
  fi && mv /tmp/config.$$$$ $(LINUXDIR)/.config


conf mconf:
	$(MAKE) -C config
	@./config/$@ ./config/Config
	$(MAKE) fixkc
        # Also configure kernel
	$(MAKE) k$@

oldconf: .config
	$(MAKE) -C config
	@./config/conf -o ./config/Config
	$(MAKE) fixkc
        # Also configure kernel
	$(MAKE) k$@

kconf:
	$(MAKE) -C $(LINUXDIR) config

kmconf:
	$(MAKE) -C $(LINUXDIR) menuconfig

koldconf: $(LINUXDIR)/.config
	$(MAKE) -C $(LINUXDIR) oldconfig

# Convenience
config: conf

menuconfig: mconf

oldconfig: oldconf

# Default configurations
.config:
	cp config/defconfig $@
	$(MAKE) oldconfig

$(LINUXDIR)/.config:
	cp $(LINUXDIR)/arch/mips/defconfig-bcm947xx $@

#
# Overrides
#


matrixssl:
	$(MAKE) -C matrixssl/src all

matrixssl-install:
	install -D matrixssl/src/libmatrixssl.so $(INSTALLDIR)/matrixssl/usr/lib/libmatrixssl.so
	$(STRIP) $(INSTALLDIR)/matrixssl/usr/lib/libmatrixssl.so

matrixssl-clean:
	$(MAKE) -C matrixssl/src clean


openssl:
	$(MAKE) -C openssl

openssl-install:
	install -D openssl/libcrypto.so $(INSTALLDIR)/openssl/usr/lib/libcrypto.so
	$(STRIP) $(INSTALLDIR)/openssl/usr/lib/libcrypto.so
	install -D openssl/libssl.so $(INSTALLDIR)/openssl/usr/lib/libssl.so
	$(STRIP) $(INSTALLDIR)/openssl/usr/lib/libssl.so

openssl-clean:
	$(MAKE) -C openssl clean

rp-l2tp-install:
	install -d $(INSTALLDIR)/rp-l2tp/usr/lib/l2tp
	install rp-l2tp/handlers/*.so $(INSTALLDIR)/rp-l2tp/usr/lib/l2tp
	$(STRIP) $(INSTALLDIR)/rp-l2tp/usr/lib/l2tp/*.so
	install -D rp-l2tp/handlers/l2tp-control $(INSTALLDIR)/rp-l2tp/usr/sbin/l2tp-control
	$(STRIP) $(INSTALLDIR)/rp-l2tp/usr/sbin/l2tp-control
	install -D rp-l2tp/l2tpd $(INSTALLDIR)/rp-l2tp/usr/sbin/l2tpd
	$(STRIP) $(INSTALLDIR)/rp-l2tp/usr/sbin/l2tpd

libpcap:
	$(MAKE) -C libpcap CC=$(CC) AR=$(AR) RANLIB=$(RANLIB)

libnet:
	-mkdir -p libnet/lib
	$(MAKE) -C libnet CC=$(CC) AR=$(AR) RANLIB=$(RANLIB)


httpd: nvram shared hsiab matrixssl
	$(MAKE) -C httpd

httpd-clean:
	$(MAKE) -C httpd clean

rc: netconf nvram shared hsiab

www www-%:
ifeq ($(CONFIG_BIRD),y)
	cp www/$(WEB_PAGE)/Routing_bird.asp www/$(WEB_PAGE)/Routing.asp
else
	@true
endif

ifeq ($(CONFIG_ZEBRA),y)
	cp www/$(WEB_PAGE)/Routing_zebra.asp www/$(WEB_PAGE)/Routing.asp
else
	@true
endif
	$(MAKE) -C www/$(WEB_PAGE) $* INSTALLDIR=$(INSTALLDIR)/www


www-distclean www-clean:
	$(MAKE) -C www/$(WEB_PAGE) clean

bridge:
	$(MAKE) -C bridge brctl/brctl

bridge-install:
	install -D bridge/brctl/brctl $(INSTALLDIR)/bridge/usr/sbin/brctl
	$(STRIP) $(INSTALLDIR)/bridge/usr/sbin/brctl

busybox-config:
	cd busybox && rm -f Config.h && ln -sf configs/$(CONFIG_BUSYBOX_CONFIG).h Config.h

busybox: busybox-config
	$(MAKE) -C busybox STRIPTOOL=$(STRIP)

busybox-install:
	$(MAKE) -C busybox STRIPTOOL=$(STRIP) PREFIX=$(INSTALLDIR)/busybox install

busybox-clean: busybox-config
	$(MAKE) -C busybox clean

busybox-distclean: busybox-config
	$(MAKE) -C busybox clean
	$(MAKE) -C busybox/scripts/config clean
	rm -f busybox/.depend busybox/include/config.h busybox/scripts/mkdep

dnsmasq-install:
	install -D dnsmasq/src/dnsmasq $(INSTALLDIR)/dnsmasq/usr/sbin/dnsmasq
	$(STRIP) $(INSTALLDIR)/dnsmasq/usr/sbin/dnsmasq

iptables:
	$(MAKE) -C iptables BINDIR=/usr/sbin LIBDIR=/usr/lib KERNEL_DIR=$(LINUXDIR)

iptables-devel:
	$(MAKE) -C iptables install-devel LIBDIR=/opt/brcm/hndtools-mipsel-uclibc-0.9.19/lib/ KERNEL_DIR=$(LINUXDIR)


iptables-install:
ifeq ($(CONFIG_IPTABLES),y)
	install -d $(INSTALLDIR)/iptables/usr/lib/iptables
	install iptables/extensions/*.so $(INSTALLDIR)/iptables/usr/lib/iptables
	$(STRIP) $(INSTALLDIR)/iptables/usr/lib/iptables/*.so
	install -D iptables/iptables $(INSTALLDIR)/iptables/usr/sbin/iptables
	$(STRIP) $(INSTALLDIR)/iptables/usr/sbin/iptables
	install -D iptables/iptables-restore $(INSTALLDIR)/iptables/usr/sbin/iptables-restore
	$(STRIP) $(INSTALLDIR)/iptables/usr/sbin/iptables-restore
        ifeq ($(CONFIG_L7),y)
	  install -d $(TARGETDIR)/etc/l7-protocols
	  cp -rp l7/* $(TARGETDIR)/etc/l7-protocols/
        endif
else
        # So that generic rule does not take precedence
	@true
endif

iptables-clean:
	-$(MAKE) -C iptables KERNEL_DIR=$(LINUXDIR) clean

iptables-distclean:
	-$(MAKE) -C iptables KERNEL_DIR=$(LINUXDIR) distclean

netconf: iptables


pptpd:
ifeq ($(CONFIG_PPTPD),y)
	$(MAKE) -C pptpd
else
	@true
endif

pptpd-clean:
	$(MAKE) -C pptpd clean

pptpd-install:
ifeq ($(CONFIG_PPTPD),y)
	install -D pptpd/pptpd $(INSTALLDIR)/pptpd/usr/sbin/pptpd
	install -D pptpd/pptpctrl $(INSTALLDIR)/pptpd/usr/sbin/pptpctrl
	install -D pptpd/bcrelay $(INSTALLDIR)/pptpd/usr/sbin/bcrelay
else
        # So that generic rule does not take precedence
	@true
endif

pipsec:
ifeq ($(CONFIG_PIPSEC),y)
	$(MAKE) -C pipsec
else
	@true
endif

pipsec-clean:
	$(MAKE) -C pipsec clean

pipsec-install:
ifeq ($(CONFIG_PIPSEC),y)
	install -D pipsec/pipsecd $(INSTALLDIR)/pipsec/usr/sbin/pipsecd
	$(STRIP) $(INSTALLDIR)/pipsec/usr/sbin/pipsecd
else
        # So that generic rule does not take precedence
	@true
endif

snmp:
ifeq ($(CONFIG_SNMP),y)
	$(MAKE) -C snmp LIBDIR=/opt/brcm/hndtools-mipsel-uclibc-0.9.19/lib/
else
	@true
endif

snmp-clean:
	$(MAKE) -C snmp clean
	rm -f $(INSTALLDIR)/etc/snmp

snmp-install:
ifeq ($(CONFIG_SNMP),y)
	install -D snmp/agent/snmpd $(INSTALLDIR)/snmp/usr/sbin/snmpd
	$(STRIP) $(INSTALLDIR)/snmp/usr/sbin/snmpd
	ln -sf /tmp/etc/snmp $(TARGETDIR)/etc/snmp
else
        # So that generic rule does not take precedence
	@true
endif

wol:
	$(MAKE) -C wol/src

wol-clean:
	$(MAKE) -C wol/src clean

wol-install:
	install -D wol/src/wol $(INSTALLDIR)/wol/usr/sbin/wol
	$(STRIP) $(INSTALLDIR)/wol/usr/sbin/wol

chillispot:
	$(MAKE) -C chillispot

chillispot-install:
	install -D chillispot/src/chilli $(INSTALLDIR)/chillispot/usr/sbin/chilli
	$(STRIP) $(INSTALLDIR)/chillispot/usr/sbin/chilli

chillispot-clean:
	$(MAKE) -C chillispot clean

frottle:
ifeq ($(CONFIG_FROTTLE),y)
	$(MAKE) -C frottle LIBDIR=/opt/brcm/hndtools-mipsel-uclibc-0.9.19/lib/
else
	@true
endif

frottle-clean:
	$(MAKE) -C frottle clean

frottle-install:
ifeq ($(CONFIG_FROTTLE),y)
	install -D frottle/frottle $(INSTALLDIR)/frottle/usr/sbin/frottle
	$(STRIP) $(INSTALLDIR)/frottle/usr/sbin/frottle
else
        # So that generic rule does not take precedence
	@true
endif

ebtables:
	$(MAKE) -C ebtables BINDIR=/usr/sbin LIBDIR=/usr/lib KERNEL_DIR=$(LINUXDIR)

ebtables-install:
ifeq ($(CONFIG_EBTABLES),y)
	install -D ebtables/ebtables $(INSTALLDIR)/ebtables/usr/sbin/ebtables
	$(STRIP) $(INSTALLDIR)/ebtables/usr/sbin/ebtables
else
        # So that generic rule does not take precedence
	@true
endif

ebtables-clean:
	-$(MAKE) -C ebtables KERNEL_DIR=$(LINUXDIR) clean


ntpclient-install:
	install -D ntpclient/ntpclient $(INSTALLDIR)/ntpclient/usr/sbin/ntpclient
	$(STRIP) $(INSTALLDIR)/ntpclient/usr/sbin/ntpclient

ppp:
	$(MAKE) -C rp-pppoe-3.5/src
ppp-clean:
	$(MAKE) -C rp-pppoe-3.5/src clean

ppp-install:
	install -D rp-pppoe-3.5/src/pppoe $(INSTALLDIR)/ppp/usr/sbin/pppoe
	$(STRIP) $(INSTALLDIR)/ppp/usr/sbin/pppoe
ifeq ($(CONFIG_PPPOERELAY),y)
	install -D rp-pppoe-3.5/src/pppoe-relay $(INSTALLDIR)/ppp/usr/sbin/pppoe-relay
	$(STRIP) $(INSTALLDIR)/ppp/usr/sbin/pppoe-relay
else
	rm -f $(INSTALLDIR)/ppp/usr/sbin/pppoe-relay
endif
ifeq ($(CONFIG_PPPOESERVER),y)
	install -D rp-pppoe-3.5/src/pppoe-server $(INSTALLDIR)/ppp/usr/sbin/pppoe-server
	$(STRIP) $(INSTALLDIR)/ppp/usr/sbin/pppoe-server
else
	rm -f $(INSTALLDIR)/ppp/usr/sbin/pppoe-server
endif
ifeq ($(CONFIG_PPPOESNIFF),y)
	install -D rp-pppoe-3.5/src/pppoe-sniff $(INSTALLDIR)/ppp/usr/sbin/pppoe-sniff
	$(STRIP) $(INSTALLDIR)/ppp/usr/sbin/pppoe-sniff
else
	rm -f $(INSTALLDIR)/ppp/usr/sbin/pppoe-sniff
endif

udhcpd: shared nvram

udhcpd-install:
	install -D udhcpd/udhcpd $(INSTALLDIR)/udhcpd/usr/sbin/udhcpd
	install -D udhcpd/dumpleases $(INSTALLDIR)/udhcpd/usr/sbin/dumpleases
	$(STRIP) $(INSTALLDIR)/udhcpd/usr/sbin/udhcpd
	$(STRIP) $(INSTALLDIR)/udhcpd/usr/sbin/dumpleases
	cd $(INSTALLDIR)/udhcpd/usr/sbin && ln -sf udhcpd udhcpc

upnp: netconf nvram shared

upnp-distclean: upnp-clean
	rm -f $(SRCBASE)/tools/upnp/upnp/linux/libupnp.a

wlconf: nvram shared

#vlan:
#	$(MAKE) -C vlan CROSS=$(CROSS_COMPILE) STRIPTOOL=$(STRIP)

#vlan-install:
#	$(MAKE) -C vlan CROSS=$(CROSS_COMPILE) STRIPTOOL=$(STRIP) INSTALLDIR=$(INSTALLDIR) install

vlan-clean:
	$(MAKE) -C vlan clean

vlan-distclean: vlan-clean
	rm -f vlan/vconfig

nas nas-clean nas-distclean:
        # So that generic rule does not take precedence
	@true

nas-install:
	install -D nas/nas $(INSTALLDIR)/nas/usr/sbin/nas
	$(STRIP) $(INSTALLDIR)/nas/usr/sbin/nas
	cd $(INSTALLDIR)/nas/usr/sbin && ln -sf nas nas4not && ln -sf nas nas4wds

########################################################################
# Below is added by CT

tftpd tftpd-clean tftpd-distclean:
        # So that generic rule does not take precedence
	@true

tftpd-install:
	install -D tftpd/tftpd $(INSTALLDIR)/tftpd/usr/sbin/tftpd
	$(STRIP) $(INSTALLDIR)/tftpd/usr/sbin/tftpd

cron-install:
	#install -d $(INSTALLDIR)/cron/etc/cron.d
	install -D cron/cron $(INSTALLDIR)/cron/usr/sbin/cron
	$(STRIP) $(INSTALLDIR)/cron/usr/sbin/cron

pptp-client:
	$(MAKE) -C pptp-client

pptp-client-clean:
	$(MAKE) -C pptp-client clean

pptp-client-install:
	install -D pptp-client/pptp $(INSTALLDIR)/pptp-client/usr/sbin/pptp
	$(STRIP) $(INSTALLDIR)/pptp-client/usr/sbin/pptp


netstatnat:
	$(MAKE) -C netstatnat

netstatnat-clean:
	$(MAKE) -C netstatnat clean

netstatnat-install:
	install -D netstatnat/netstat-nat $(INSTALLDIR)/netstatnat/usr/sbin/netstat-nat
	$(STRIP) $(INSTALLDIR)/netstatnat/usr/sbin/netstat-nat

pppd-symlinks:
	- (cd pppd ; \
	ln -s linux/Makefile.top Makefile ; \
	ln -s Makefile.linux pppd/Makefile ; \
	ln -s Makefile.linux chat/Makefile ; \
	ln -s Makefile.linux pppd/plugins/Makefile ; \
	ln -s Makefile.linux pppd/plugins/radius/Makefile ; \
	ln -s Makefile.linux pppd/plugins/rp-pppoe/Makefile ; \
	ln -s Makefile.linux pppdump/Makefile ; \
	ln -s Makefile.linux pppstats/Makefile ; \
	)

pppd: pppd-symlinks
	$(MAKE) -C pppd/pppd
ifeq ($(CONFIG_PPPSTATS),y)
	$(MAKE) -C pppd/pppstats
endif
ifeq ($(CONFIG_RADIUSPLUGIN),y)
	$(MAKE) -C pppd/pppd/plugins/radius
endif


pppd-clean pppd-distclean: pppd-symlinks
	$(MAKE) -C pppd/pppd clean

pppd-install:
	install -D pppd/pppd/pppd $(INSTALLDIR)/pppd/usr/sbin/pppd
ifeq ($(CONFIG_PPPSTATS),y)
	install -D pppd/pppstats/pppstats $(INSTALLDIR)/pppd/usr/sbin/pppstats
else
	rm -rf $(INSTALLDIR)/pppd/usr/sbin/pppstats
endif
ifeq ($(CONFIG_RADIUSPLUGIN),y)
	install -D pppd/pppd/plugins/radius/radius.so $(INSTALLDIR)/pppd/usr/lib/pppd/radius.so
	install -D pppd/pppd/plugins/radius/radattr.so $(INSTALLDIR)/pppd/usr/lib/pppd/radattr.so
	install -D pppd/pppd/plugins/radius/radrealms.so $(INSTALLDIR)/pppd/usr/lib/pppd/radrealms.so
	install -d $(INSTALLDIR)/pppd/etc/radiusclient
	cp pppd/pppd/plugins/radius/radiusclient/etc/issue $(INSTALLDIR)/pppd/etc/radiusclient/issue
	cp pppd/pppd/plugins/radius/radiusclient/etc/port-id-map $(INSTALLDIR)/pppd/etc/radiusclient/port-id-map
	cp pppd/pppd/plugins/radius/radiusclient/etc/dictionary $(INSTALLDIR)/pppd/etc/radiusclient/dictionary
	cp pppd/pppd/plugins/radius/radiusclient/etc/dictionary.microsoft $(INSTALLDIR)/pppd/etc/radiusclient/dictionary.microsoft
	chmod 0644 $(INSTALLDIR)/pppd/etc/radiusclient/*
else
	rm -rf $(INSTALLDIR)/pppd/usr/lib/pppd/rad*.so
	rm -rf $(INSTALLDIR)/pppd/etc/radiusclient
endif
	rm -rf $(INSTALLDIR)/pppd/usr/lib/pppd/rp-pppoe.so

	$(STRIP) $(INSTALLDIR)/pppd/usr/sbin/pppd
ifeq ($(CONFIG_PPPSTATS),y)
	$(STRIP) $(INSTALLDIR)/pppd/usr/sbin/pppstats
endif

ipupdate-install:
	install -D ipupdate/ez-ipupdate $(INSTALLDIR)/ipupdate/usr/sbin/ez-ipupdate
	$(STRIP) $(INSTALLDIR)/ipupdate/usr/sbin/ez-ipupdate

zebra:
ifeq ($(CONFIG_GNUZEBRA),y)
	$(MAKE) -C quagga CC=$(CC)
endif

zebra-install:
ifeq ($(CONFIG_GNUZEBRA),y)
	install -D quagga/zebra/zebra $(INSTALLDIR)/zebra/usr/sbin/zebra
	install -D quagga/ripd/ripd $(INSTALLDIR)/zebra/usr/sbin/ripd
	install -D quagga/ospfd/ospfd $(INSTALLDIR)/zebra/usr/sbin/ospfd
	$(STRIP) $(INSTALLDIR)/zebra/usr/sbin/zebra
	$(STRIP) $(INSTALLDIR)/zebra/usr/sbin/ripd
	$(STRIP) $(INSTALLDIR)/zebra/usr/sbin/ospfd
else
	rm -rf $(INSTALLDIR)/zebra/usr/sbin/zebra
	rm -rf $(INSTALLDIR)/zebra/usr/sbin/ripd
	rm -rf $(INSTALLDIR)/zebra/usr/sbin/ospfd
endif

bird-symlinks:
	$(MAKE) -C bird/obj .dir-stamp
	mkdir -p bird/obj/conf bird/obj/lib
	- (cd bird/obj/conf ; \
	ln -s ../../conf/cf-lex.l cf-lex.l ; \
	ln -s ../../conf/conf.c conf.c ; \
	ln -s ../../conf/conf.h conf.h ; \
	ln -s ../../sysdep/unix/config.Y config.Y ; \
	ln -s ../../sysdep/unix/krt.Y krt.Y ; \
	ln -s ../../sysdep/linux/netlink/netlink.Y netlink.Y ; \
	)
	- (cd bird/obj/lib ; \
	ln -s ../../lib/birdlib.h birdlib.h ; \
	ln -s ../../lib/bitops.c bitops.c ; \
	ln -s ../../lib/bitops.h bitops.h ; \
	ln -s ../../lib/checksum.c checksum.c ; \
	ln -s ../../lib/checksum.h checksum.h ; \
	ln -s ../../sysdep/unix/endian.h endian.h ; \
	ln -s ../../lib/event.c event.c ; \
	ln -s ../../lib/event.h event.h ; \
	ln -s ../../sysdep/unix/io.c io.c ; \
	ln -s ../../lib/ip.c ip.c ; \
	ln -s ../../lib/ip.h ip.h ; \
	ln -s ../../lib/ipv4.c ipv4.c ; \
	ln -s ../../lib/ipv4.h ipv4.h ; \
	ln -s ../../sysdep/unix/krt.c krt.c ; \
	ln -s ../../sysdep/unix/krt.h krt.h ; \
	ln -s ../../sysdep/linux/netlink/krt-iface.h krt-iface.h ; \
	ln -s ../../sysdep/linux/netlink/krt-scan.h krt-scan.h ; \
	ln -s ../../sysdep/linux/netlink/krt-set.h krt-set.h ; \
	ln -s ../../lib/lists.c lists.c ; \
	ln -s ../../lib/lists.h lists.h ; \
	ln -s ../../sysdep/unix/log.c log.c ; \
	ln -s ../../sysdep/unix/main.c main.c ; \
	ln -s ../../lib/md5.c md5.c ; \
	ln -s ../../lib/md5.h md5.h ; \
	ln -s ../../lib/mempool.c mempool.c ; \
	ln -s ../../sysdep/linux/netlink/netlink.c netlink.c ; \
	ln -s ../../lib/patmatch.c patmatch.c ; \
	ln -s ../../lib/printf.c printf.c ; \
	ln -s ../../sysdep/unix/random.c random.c ; \
	ln -s ../../lib/resource.c resource.c ; \
	ln -s ../../lib/resource.h resource.h ; \
	ln -s ../../lib/slab.c slab.c ; \
	ln -s ../../lib/slists.c slists.c ; \
	ln -s ../../lib/slists.h slists.h ; \
	ln -s ../../lib/socket.h socket.h ; \
	ln -s ../../lib/string.h string.h ; \
	ln -s ../../sysdep/linux/sysio.h sysio.h ; \
	ln -s ../../sysdep/unix/timer.h timer.h ; \
	ln -s ../../lib/unaligned.h unaligned.h ; \
	ln -s ../../sysdep/unix/unix.h unix.h ; \
	ln -s ../../lib/xmalloc.c xmalloc.c ; \
	)

bird: bird-symlinks
	$(MAKE) -C bird

bird-install:
	install -D bird/bird $(INSTALLDIR)/bird/usr/sbin/bird
	$(STRIP) $(INSTALLDIR)/bird/usr/sbin/bird

bird-clean:
	$(MAKE) -C bird clean

bird-distclean:
	$(MAKE) -C bird clean
	rm -rf bird/obj/nest bird/obj/filter bird/obj/proto
	rm -f bird/obj/.dir-stamp

shorewall-install:
	install -D shorewall/common.def     $(INSTALLDIR)/shorewall/usr/sbin/common.def
	install -D shorewall/firewall       $(INSTALLDIR)/shorewall/usr/sbin/firewall
	install -D shorewall/functions      $(INSTALLDIR)/shorewall/usr/sbin/functions
	install -D shorewall/shorewall      $(INSTALLDIR)/shorewall/usr/sbin/shorewall
	install -D shorewall/shorewall.conf $(INSTALLDIR)/shorewall/usr/sbin/shorewall.conf
	install -D shorewall/version        $(INSTALLDIR)/shorewall/usr/sbin/version

shorewall-clean:
	$(MAKE) -C shorewall clean


bpalogin-install:
	install -D bpalogin/bpalogin $(INSTALLDIR)/bpalogin/usr/sbin/bpalogin
	$(STRIP) $(INSTALLDIR)/bpalogin/usr/sbin/bpalogin

dropbear:
	$(MAKE) -C dropbear PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" SCPPROGRESS=1 MULTI=1

dropbear-install:
	install -D dropbear/dropbearmulti $(INSTALLDIR)/dropbear/usr/sbin/dropbear
	$(STRIP) $(INSTALLDIR)/dropbear/usr/sbin/dropbear
	cd $(INSTALLDIR)/dropbear/usr/sbin && ln -sf dropbear dropbearconvert && ln -sf dropbear dropbearkey && ln -sf dropbear dbclient
	mkdir -p $(INSTALLDIR)/dropbear/usr/bin
	cd $(INSTALLDIR)/dropbear/usr/bin && ln -sf /usr/sbin/dropbear ssh && ln -sf /usr/sbin/dropbear scp

iproute2:
	$(MAKE) -C iproute2 all

iproute2-install:
	chmod 0644 iproute2/tc/tc
	install -D iproute2/tc/tc $(INSTALLDIR)/iproute2/usr/sbin/tc
	$(STRIP) $(INSTALLDIR)/iproute2/usr/sbin/tc
	chmod 0644 iproute2/ip/ip
	install -D iproute2/ip/ip $(INSTALLDIR)/iproute2/usr/sbin/ip
	$(STRIP) $(INSTALLDIR)/iproute2/usr/sbin/ip
ifeq ($(CONFIG_WSHAPER),y)
	chmod 0644 iproute2/wshaper.htb
	install -D iproute2/wshaper.htb $(INSTALLDIR)/iproute2/usr/sbin/wshaper
else
	@true
endif

ifeq ($(CONFIG_SVQOS),y)
	chmod 0644 iproute2/svqos.htb
	install -D iproute2/svqos.htb $(INSTALLDIR)/iproute2/usr/sbin/svqos
else
	@true
endif

adm6996:
	$(MAKE) -C adm6996

adm6996-install:
	install -D adm6996/adm6996.o $(INSTALLDIR)/adm6996/lib/modules/2.4.20/kernel/drivers/net/adm6996/adm6996.o
#	$(STRIP) $(INSTALLDIR)/adm6996/lib/adm6996.o

parprouted:
ifeq ($(CONFIG_PARPROUTED),y)
	$(MAKE) -C parprouted
else
	@true
endif

parprouted-clean:
	$(MAKE) -C parprouted clean

parprouted-install:
ifeq ($(CONFIG_PARPROUTED),y)
	install -D parprouted/parprouted $(INSTALLDIR)/parprouted/usr/sbin/parprouted
	$(STRIP) $(INSTALLDIR)/parprouted/usr/sbin/parprouted
else
        # So that generic rule does not take precedence
	@true
endif

wpa_supplicant:
ifeq ($(CONFIG_WPA_SUPPLICANT),y)
	$(MAKE) -C wpa_supplicant
else
	@true
endif

wpa_supplicant-clean:
	$(MAKE) -C wpa_supplicant clean

wpa_supplicant-install:
ifeq ($(CONFIG_WPA_SUPPLICANT),y)
	install -D wpa_supplicant/wpa_supplicant $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_supplicant
	install -D wpa_supplicant/wpa_passphrase $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_passphrase
	install -D wpa_supplicant/wpa_cli $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_cli
	$(STRIP) $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_supplicant
	$(STRIP) $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_passphrase
	$(STRIP) $(INSTALLDIR)/wpa_supplicant/usr/sbin/wpa_cli
else
	# So that generic rule does not take precedence
	@true
endif

#
# Generic rules
#

%:
	[ ! -d $* ] || $(MAKE) -C $*

%-distclean:
	[ ! -d $* ] || $(MAKE) -C $* clean

%-clean:
	[ ! -d $* ] || $(MAKE) -C $* clean

%-install:
	[ ! -d $* ] || $(MAKE) -C $* install INSTALLDIR=$(INSTALLDIR)/$*

$(obj-y) $(obj-n) $(obj-clean) $(obj-install): dummy

.PHONY: all clean distclean mrproper install package
.PHONY: conf mconf oldconf kconf kmconf config menuconfig oldconfig
.PHONY: dummy fixkc libnet libpcap



