BUSYBOX_VERSION = 1.15.2
BUSYBOX = busybox-$(BUSYBOX_VERSION)
$(BUSYBOX)_SOURCE = http://www.busybox.net/downloads/$(BUSYBOX).tar.bz2

PCIUTILS_VERSION = 2.1.11
PCIUTILS = pciutils-$(PCIUTILS_VERSION)
$(PCIUTILS)_SOURCE = ftp://ftp.kernel.org/pub/software/utils/pciutils/$(PCIUTILS).tar.gz

export PATH := $(UCLIBC_INSTALL)/bin:$(PATH)
#
BUILD_NCPUS = $(shell egrep -c "^cpu[0-9]+" /proc/stat || :)
if [ $(BUILD_NCPUS) -eq 0 ] ; then \
    $(BUILD_NCPUS) = 1 \
fi
if [ $(BUILD_NCPUS) -gt 4 ] ; then \
    $(BUILD_NCPUS) = 4 \
fi
MP_OPT = -j $(BUILD_NCPUS)

all : utils initrd-skel
	# Run "make tftpboot/initrd-kernel_module.img" to generate a suitable initrd
	# Run "make tftpboot/boot-kernel_module.nbi" to generate a suitable NBI
	# Run "make all-nbi" to generate a complete set of NBIs

%.tar.bz2 :
	[ -d $* ] || wget $($*_SOURCE)
	[ -f $*.t*gz ] && ( gunzip $*.t*gz ; bzip2 -9 $*.tar ) || true

%.tar.gz :
	[ -d $* ] || wget $($*_SOURCE)

# for DRBL, modified by Steven Shiau, wlanctl in not used now
#UTILS = udhcpc busybox wlanctl
UTILS = busybox pciutils

utils : $(UTILS)

clean : partlyclean
	rm -rf $(UCLIBC)
	rm -rf tftpboot/*

partlyclean :
	rm -rf $(UDHCP)
	rm -rf $(BUSYBOX)
	rm -rf initrd-skel
	rm -f *.img *.ird *.nbi
	rm -f $(UTILS)
	rm -f *.busybox *.wlanctl

.PHONY : all utils clean partlyclean

busybox : $(BUSYBOX)
	install -m 755 -s $(BUSYBOX)/$@ $@

$(BUSYBOX) : $(BUSYBOX).tar.bz2
	[ -d $@ ] || tar xvjf $<
	# Steven Shiau add PING|FEATURE_IFCONFIG_STATUS|STTY|VI for DRBL
	install -m 644 config.busybox/$(BUSYBOX).config $(BUSYBOX)/.config
	$(MAKE) -C $(BUSYBOX)
	install -m 644 $(BUSYBOX)/AUTHORS AUTHORS.busybox
	install -m 644 $(BUSYBOX)/LICENSE LICENSE.busybox

pciutils : $(PCIUTILS)
	#install -m 755 -s $(PCIUTILS)/$@ $@

$(PCIUTILS) : $(PCIUTILS).tar.gz
	[ -d $@ ] || tar xvzf $<
	perl -pi.orig -e \
	  's/^export.*/export LDFLAGS+=-static/' \
	  $(PCIUTILS)/Makefile
	$(MAKE) -C $(PCIUTILS)
	install -m 644 $(PCIUTILS)/lspci lspci-static
	install -m 644 $(PCIUTILS)/update-pciids.sh update-pciids.sh

initrd-skel : $(UTILS) linuxrc-or-init udhcpc-post
	rm -rf $@
	mkdir -p $@
	mkdir -p $@/dev
	mkdir -p $@/etc
	mkdir -p $@/bin
	mkdir -p $@/lib
	mkdir -p $@/lib/modules
	mkdir -p $@/proc
	mkdir -p $@/sysroot
	ln -s bin $@/sbin
	install -m 755 busybox scan_pci ls_pciid.sh get-nic-devs insert-modules $@/bin/
	# for DRBL, modified by Steven Shiau, wlanctl in not used now
	#install -m 755 wlanctl $@/bin/
	# for DRBL, modified by Steven Shiau,
	install -m 755 lspci-static $@/bin/
	install -m 644 pcitable pci.ids linuxrc.conf netdev.conf modules $@/etc/
	ln -s busybox $@/bin/sh
	ln -s busybox $@/bin/echo
	ln -s busybox $@/bin/mknod
	ln -s busybox $@/bin/chmod
	ln -s busybox $@/bin/insmod
	ln -s busybox $@/bin/ifconfig
	ln -s busybox $@/bin/route
	ln -s busybox $@/bin/mount
	ln -s busybox $@/bin/pivot_root
	ln -s busybox $@/bin/umount
	ln -s busybox $@/bin/[
	ln -s busybox $@/bin/sleep
	ln -s busybox $@/bin/grep
	# for DRBL, modified by Steven Shiau
	ln -s busybox $@/bin/sed
	ln -s busybox $@/bin/cat
	ln -s busybox $@/bin/cut
	ln -s busybox $@/bin/sort
	ln -s busybox $@/bin/head
	ln -s busybox $@/bin/uniq
	ln -s busybox $@/bin/modprobe
	ln -s busybox $@/bin/rmmod
	ln -s busybox $@/bin/lsmod
	ln -s busybox $@/bin/ping
	ln -s busybox $@/bin/df
	ln -s busybox $@/bin/id
	ln -s busybox $@/bin/more
	ln -s busybox $@/bin/ln
	ln -s busybox $@/bin/ls
	ln -s busybox $@/bin/gunzip
	ln -s busybox $@/bin/uname
	ln -s busybox $@/bin/tr
	ln -s busybox $@/bin/mkdir
	ln -s busybox $@/bin/rm
	# busybox 1.0 has udhcpc
	ln -s busybox $@/bin/udhcpc
	ln -s busybox $@/bin/switch_root
	ln -s busybox $@/bin/mountpoint
	ln -s busybox $@/bin/wc

	install -m 755 linuxrc-or-init $@/linuxrc-or-init
	install -m 755 udhcpc-post $@/bin/udhcpc-post
	cd $@/etc; ln -s ../dev/resolv.conf resolv.conf

tftpboot/initrd-%.img : initrd-skel
	./mkpxeinitrd-net -l `echo $* | tr . " "`

prefix = /usr
sysconfdir = /etc
bindir = $(prefix)/bin
libdir = $(prefix)/lib
mandir = $(prefix)/share/man
docdir = $(prefix)/share/doc
# for DRBL, Steven Shiau modified
#tftpbootdir = /var/lib/tftpboot
tftpbootdir = /tftpboot
initrdskeldir = $(prefix)/lib/mkpxeinitrd-net/initrd-skel
hooksdir = $(prefix)/lib/mkpxeinitrd-net/hooks

install :
	mkdir -p $(bindir)
	mkdir -p $(sysconfdir)
	mkdir -p $(tftpbootdir)
	mkdir -p $(initrdskeldir)
	mkdir -p $(hooksdir)
	# for DRBL, Steven Shiau modified, add
	# update-pciids.sh & mkpxeinitrd-net
	install -m 755 \
	        update-pciids.sh mkpxeinitrd-net parse-net-mod parse-nfs-mod $(bindir)/
	cp -a initrd-skel/* $(initrdskeldir)/
	cp -a hooks/* $(hooksdir)/
