#
# Copyright 2001-2004, 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.
#

include .config

#
# Paths
#

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

export TRXDIR := $(shell (cd $(SRCBASE)/../tools && pwd -P))
export TRX := $(TRXDIR)/trx


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


ifeq ($(CONFIG_BCMWPA2),y)
export CFLAGS += -DBCMWPA2 
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 STRIPPROG := $(CROSS_COMPILE)strip
export SIZE := $(CROSS_COMPILE)size

#
# Install and target directories
#

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

# always build crypto
obj-y += libcrypto 

#
# Configuration
#

obj-$(CONFIG_NETVARPLUGIN) += netvarplugin
obj-$(CONFIG_HTTPD) += httpd
obj-$(CONFIG_SAMPLENETREGISTRATION) += samplenetregistration
obj-$(CONFIG_SHARED) += shared
obj-$(CONFIG_RC) += rc
obj-$(CONFIG_LIBBCM) += libbcm
obj-$(CONFIG_FIREWALL) += firewall
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_NAS) += nas
obj-$(CONFIG_SES) += ses
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_TCP_WRAPPER) += tcp_wrapper
obj-$(CONFIG_PORTMAP) += portmap
obj-$(CONFIG_NFS) += nfs-utils
obj-$(CONFIG_POPT) += popt
obj-$(CONFIG_RAID) += raidtools
obj-$(CONFIG_LVM) += lvm
obj-$(CONFIG_MKFS) += mkfs
obj-$(CONFIG_JFSUTILS) += jfsutils
obj-$(CONFIG_SAMBA) += samba
obj-$(CONFIG_BUILDINFO) += buildinfo
obj-$(CONFIG_SMART) += smart
obj-$(CONFIG_IPROUTE2) += iproute2
obj-$(CONFIG_MISCIOCLI) += miscio
obj-$(CONFIG_SNMP) += snmp
obj-$(CONFIG_USER_GROUP) += user_group
obj-$(CONFIG_ARCHIVE) += archive
obj-$(CONFIG_REISERFSPROGS) += reiserfsprogs
obj-$(CONFIG_SMTPCLIENT) += smtpclient
obj-$(CONFIG_LPRNG) += lprng
obj-$(CONFIG_HDPARM) += hdparm
obj-$(CONFIG_PRINTSERVER) += printserver
obj-$(CONFIG_SECSRL) += sec_srl
obj-$(CONFIG_WIPEDISKS) += wipedisks
obj-$(CONFIG_ATTR) += attr
obj-$(CONFIG_ACL) += acl
obj-$(CONFIG_GMP) += gmp
obj-$(CONFIG_SEUTIL) += se-utils
obj-$(CONFIG_USER_NOTE) += user_note
obj-$(CONFIG_SENSORD) += tempsensor
obj-$(CONFIG_UTELNETD) += utelnetd
obj-$(CONFIG_WEBHOOKS) += webhooks
obj-$(CONFIG_NETWEBHOOKS) += netwebhooks
obj-$(CONFIG_DISKWEBHOOKS) += diskwebhooks
obj-$(CONFIG_WINNET) += winnet
obj-$(CONFIG_PRINTWEBHOOKS) += printwebhooks
obj-$(CONFIG_COREWEBHOOKS) += corewebhooks
obj-$(CONFIG_MKDOSFS) += mkdosfs
obj-$(CONFIG_MTOOLS) += mtools
obj-$(CONFIG_DOSFSTOOLS) += dosfstools
obj-$(CONFIG_PIVOT_ROOT) += pivot_root
obj-$(CONFIG_MFGTEST_ROOT) += mfgtest_root
obj-$(CONFIG_RESPART) += respart
obj-$(CONFIG_DISKTESTWAIT) += disktestwait
obj-$(CONFIG_DISCONNECT_DISK) += disconnect_disk
obj-$(CONFIG_SAMPLEHOOKS) += samplehooks
obj-$(CONFIG_RUNWEBHOOKS) += runwebhooks
obj-$(CONFIG_SOFT_POWER_WAITER) += soft_power_waiter
obj-$(CONFIG_TRANSLATION) += translation
obj-n += translation_database
obj-$(CONFIG_TIMEZONES) += timezones
obj-$(CONFIG_UPNPAV) += upnpav
obj-$(CONFIG_VSFTPD) += vsftpd
obj-$(CONFIG_VARIABLE_INFO) += variable_info
obj-$(CONFIG_CORE_VARIABLE_INFO) += core_variable_info
obj-$(CONFIG_CUSTOM_VARIABLE_INFO) += custom_variable_info
obj-$(CONFIG_CHECK_NVRAM_VARIABLES) += check_nvram_variables
obj-$(CONFIG_CHECK_IMAGE_VARIABLES) += check_image_variables
obj-$(CONFIG_CHECK_SHARE_VARIABLES) += check_share_variables
obj-$(CONFIG_MAKECASETABLES) += makecasetables
obj-$(CONFIG_VLAN) += vlan
obj-$(CONFIG_FREESWAN_VPN) += vpn
obj-$(CONFIG_MXOHNSINIT) += mxohnsinit
obj-$(CONFIG_MXOHNSUTILITIES_RDV) += mxohnsutilities/rdv/mDNSResponder-58.8/mDNSPosix
obj-$(CONFIG_MXOHNSUTILITIES_LIBUPNP) += mxohnsutilities/libupnp
obj-$(CONFIG_MXOHNSUTILITIES_GDBM) += mxohnsutilities/gdbm
obj-$(CONFIG_MXOHNSUTILITIES_LIBID3TAG) += mxohnsutilities/libid3tag
obj-$(CONFIG_MXOHNSUTILITIES_ZLIB) += mxohnsutilities/zlib
obj-$(CONFIG_MXOHNSUTILITIES_DAAPD) += mxohnsutilities/daapd

obj-clean := $(foreach obj,$(obj-y) $(obj-n),$(obj)-clean)
obj-install := $(foreach obj,$(obj-y),$(obj)-install)
obj-package-dir :=
ifeq ($(CONFIG_TRANSLATION_DATABASE),y)
obj-package-dir += translation_database
endif

#
# Basic rules
#

all: $(obj-y) $(LINUXDIR)/.config
        # Also build kernel
	$(MAKE) -C $(LINUXDIR) zImage
	$(MAKE) -C $(LINUXDIR) modules

clean: $(obj-clean)
	rm -rf $(TARGETDIR)
	rm -f $(PLATFORMDIR)/broadnas.trx $(PLATFORMDIR)/vmlinuz $(PLATFORMDIR)/target.cramfs $(PLATFORMDIR)/root.trx
#	$(MAKE) -C $(LINUXDIR) clean

distclean mrproper: clean
	rm -f .config $(LINUXDIR)/.config

packaging-ready: $(filter-out lib-install,$(obj-install))
ifeq ($(CONFIG_TRANSLATION_DATABASE),y)
        # Install the translation database library (which requires everything
        # else to already be installed).
	$(MAKE) -C translation_database install INSTALLDIR=$(INSTALLDIR)/translation_database
endif
        # Install binaries into target directory
	rm -rf $(TARGETDIR)
	install -d $(TARGETDIR)
	for dir in $(wildcard $(patsubst %,$(INSTALLDIR)/%,$(obj-y))) $(patsubst %,$(INSTALLDIR)/%,$(obj-package-dir)) ; do \
	    (cd $${dir} && tar cpf - .) | (cd $(TARGETDIR) && tar xpf -) \
	done
	# optimize the crypto library by removing unneeded symbols
	[ ! -d libcrypto ] || $(MAKE) -C libcrypto optimize
        # Install (and possibly optimize) C library
	$(MAKE) lib-install
        # Install modules into filesystem
	$(MAKE) -C $(LINUXDIR) modules_install DEPMOD=/bin/true INSTALL_MOD_PATH=$(TARGETDIR)

do-packaging:
        # Prepare filesystem
	cd $(TARGETDIR) && $(TOP)/misc/rootprep.sh && mkcramfs . $(PLATFORMDIR)/target.cramfs
        # Package kernel and filesystem
	cp $(LINUXDIR)/arch/mips/brcm-boards/bcm947xx/compressed/vmlinuz $(PLATFORMDIR)/
	$(TRX) -o $(PLATFORMDIR)/broadnas.trx $(PLATFORMDIR)/vmlinuz $(PLATFORMDIR)/target.cramfs
	$(TRX) -o $(PLATFORMDIR)/root.trx $(PLATFORMDIR)/target.cramfs

install package: packaging-ready do-packaging


#
# Configuration rules
#

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

oldconf: .config
	$(MAKE) -C config
	@./config/conf -o ./config/Config
        # 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-bcm94780 $@

#
# Overrides
#

acl: attr

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

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

busybox:
	mv busybox/include/config.h busybox/include/config.h.orig
	cd busybox && rm -f .config && ln -s mxo_config .config
	$(MAKE) -C busybox dep
	$(MAKE) -C busybox STRIPTOOL=$(STRIP)

busybox-install:
	rm -rf $(INSTALLDIR)/busybox
	$(MAKE) -C busybox STRIPTOOL=$(STRIP) PREFIX=$(INSTALLDIR)/busybox install
	mkdir $(INSTALLDIR)/busybox/etc
#	cd $(INSTALLDIR)/busybox/etc && ln -sf /shares/mss-hdd/dev/inetd.conf inetd.conf
	cp busybox/inetd.conf $(INSTALLDIR)/busybox/etc/inetd.conf

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

firewall: netconf nvram shared webhooks

shared: translation

netvarplugin: translation

samplenetregistration : netvarplugin

httpd: nvram shared webhooks translation

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

iptables-install:
	rm -rf $(INSTALLDIR)/iptables
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
else
        # So that generic rule does not take precedence
	@true
endif

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

jfsutils : mkfs

miscio: nvram

nas: nvram shared

netconf: iptables webhooks

nfs-utils-install:
	rm -rf $(INSTALLDIR)/nfs-utils
	$(MAKE) -C nfs-utils install INSTALLDIR=$(INSTALLDIR)/nfs-utils
	install -d $(INSTALLDIR)/nfs-utils/etc
	cd $(INSTALLDIR)/nfs-utils/etc && ln -sf /tmp/hosts.allow hosts.allow
	cd $(INSTALLDIR)/nfs-utils/etc && ln -sf /tmp/hosts.deny hosts.deny
	install -d $(INSTALLDIR)/nfs-utils/shares

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

portmap: tcp_wrapper

ppp ppp-%:
	rm -rf $(INSTALLDIR)/ppp
	$(MAKE) -C ppp/pppoecd $* INSTALLDIR=$(INSTALLDIR)/ppp

raidtools: popt

rc: nvram shared netconf webhooks

samba: attr acl

se-utils: nvram sec_srl gmp

ses: nvram shared

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

upnp: netconf nvram shared webhooks

mxohnsutilities/gdbm: nvram shared
mxohnsutilities/libid3tag: nvram shared
mxohnsutilities/zlib: nvram shared
mxohnsutilities/daapd: mxohnsutilities/libid3tag mxohnsutilities/gdbm mxohnsutilities/zlib

user_group: nvram shared

archive: nvram shared

wlconf: nvram shared

user_note: shared

tempsensor: nvram shared

corewebhooks: nvram webhooks shared

netwebhooks: nvram webhooks shared netconf

diskwebhooks: nvram webhooks shared translation

winnet: nvram webhooks shared translation

printwebhooks: nvram webhooks shared

respart: nvram shared

disktestwait: nvram shared

disconnect_disk: nvram shared

pivot_root : nvram shared

mfgtest_root : shared busybox netconf nvram udhcpd utils smart hdparm tempsensor webhooks

samplehooks : nvram shared webhooks

runwebhooks : nvram shared webhooks

soft_power_waiter : nvram shared

translation : nvram

core_variable_info: variable_info

custom_variable_info: variable_info

check_nvram_variables: variable_info

check_image_variables: variable_info

check_share_variables: variable_info


www www-%:
	rm -rf $(INSTALLDIR)/www
	$(MAKE) -C www/$(CONFIG_VENDOR) $* INSTALLDIR=$(INSTALLDIR)/www

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


#
# Generic rules
#

%:
	[ ! -f $*/Makefile ] || $(MAKE) -C $*

%-clean:
	[ ! -f $*/Makefile ] || $(MAKE) -C $* clean

%-install:
	[ ! -f $*/Makefile ] || rm -rf $(INSTALLDIR)/$*
	[ ! -f $*/Makefile ] || $(MAKE) -C $* install INSTALLDIR=$(INSTALLDIR)/$*

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

.PHONY: all clean distclean mrproper install packaging-ready do-packaging
.PHONY: package conf mconf oldconf kconf kmconf config
.PHONY: menuconfig oldconfig dummy
