# $Progeny: Makefile,v 1.59 2002/04/22 21:29:14 branden Exp $

SHELL=		/bin/sh -e
TOOLS=		$(RELEASE_tools)

all: $(TDIR)/initrd-reboot $(TDIR)/initrd.installer.gz $(TDIR)/initrd.system.gz

# Generate a modules.dep file for the kernel to be used by the initrds.
$(TDIR)/modules.dep:
	-rm -r $(TDIR)/depmod_kludge
	mkdir -p $(TDIR)/depmod_kludge/lib/modules
	# this really sucks; I wish you could tell depmod to write to standard output
	cp --dereference --recursive /lib/modules/$(KERNEL_VERSION) $(TDIR)/depmod_kludge/lib/modules
	$(SU_CMD) depmod -b $(TDIR)/depmod_kludge -F /boot/System.map-$(KERNEL_VERSION) -a $(KERNEL_VERSION)
	cp $(TDIR)/depmod_kludge/lib/modules/$(KERNEL_VERSION)/modules.dep $@.tmp
	rm -r $(TDIR)/depmod_kludge
	mv $@.tmp $@

# Compile the quick-and-easy non-init-based reboot utility.
$(TDIR)/initrd-reboot: initrd-reboot.c
	$(CC) -o $@.tmp initrd-reboot.c
	strip --strip-unneeded --remove-section=.note \
	 --remove-section=.comment $@.tmp
	mv $@.tmp $@

# Create a busybox source tree for use by the installer initrd.
$(TDIR)/$(BUSYBOX)-initrd: /usr/src/$(BUSYBOX).tar.gz
	gzip -dc /usr/src/$(BUSYBOX).tar.gz | \
		(cd $(TDIR) && tar -xf -)
	mv $(TDIR)/$(BUSYBOX) $(TDIR)/$(BUSYBOX)-initrd

# Compile the installer initrd's version of the busybox binary.
$(TDIR)/busybox.initrd: $(TDIR)/$(BUSYBOX)-initrd
	cp $(PGI_DIR)/busybox.h $(TDIR)/$(BUSYBOX)-initrd/Config.h
	cd $(TDIR)/$(BUSYBOX)-initrd && $(MAKE)
	cp $(TDIR)/$(BUSYBOX)-initrd/busybox $@

# Generate the installer initrd's symlinks to the busybox binary.
$(TDIR)/busybox.initrd.links: $(TDIR)/busybox.initrd
	cd $(TDIR)/$(BUSYBOX)-initrd && \
		sh $(PGI_DIR)/busybox.mkll Config.h applets.h > $@

# Create the installer initrd's filesystem.
$(TDIR)/initrd.installer: $(TDIR)/busybox.initrd $(TDIR)/busybox.initrd.links $(TDIR)/modules.dep
	@(dpkg -s libc6-pic || dpkg -s libc6.1-pic ) 2> /dev/null | egrep -q "Status: install ok installed" \
	 || (echo "ERROR: You must install libc6-pic or libc6.1-pic to build a small initrd" >&2;\
	     exit 1)
	-$(SU_CMD) rm -rf $@.fs $@.mnt
	mkdir $@.mnt $@.fs
	TOOLS=$(TOOLS) $(SU_CMD) ./mkinitrd --installer $(KERNEL_VERSION) $@.fs \
	  $$(sh $(TOOLS)/detectmodules.sh $(KERNEL_VERSION))
	dd if=/dev/zero of=$@.tmp bs=1k count=$(INITRD_FS_SIZE)
	mke2fs -Fq $@.tmp
	$(SU_CMD) mount -oloop -text2 $@.tmp $@.mnt
	-$(SU_CMD) rmdir $@.mnt/lost+found
	$(SU_CMD) bash -c "(cd $@.fs ; tar -c .) | (cd $@.mnt ; tar -x)"
	$(SU_CMD) chown -R root.root $@.mnt
	$(SU_CMD) umount $@.mnt
	mv $@.tmp $@

# Compress the installer initrd.
$(TDIR)/initrd.installer.gz: $(TDIR)/initrd.installer
	gzip -9 < $(TDIR)/initrd.installer > $@

# Create the system initrd's filesystem.
$(TDIR)/initrd.system: $(TDIR)/busybox.initrd $(TDIR)/busybox.initrd.links $(TDIR)/modules.dep
	# ensure that we have a "reduceable" C library
	@(dpkg -s libc6-pic || dpkg -s libc6.1-pic ) | egrep -q "Status: install ok installed" \
	 || (echo "ERROR: You must install libc6-pic or libc6.1-pic to build a small initrd" >&2;\
	     exit 1)
	-$(SU_CMD) rm -rf $@.fs $@.mnt
	mkdir $@.mnt $@.fs
	TOOLS=$(TOOLS) $(SU_CMD) ./mkinitrd --system $(KERNEL_VERSION) $@.fs \
	  $$(sh $(TOOLS)/detectmodules.sh $(KERNEL_VERSION))
	dd if=/dev/zero of=$@.tmp bs=1k count=$(INITRD_FS_SIZE)
	mke2fs -Fq $@.tmp
	$(SU_CMD) mount -oloop -text2 $@.tmp $@.mnt
	-$(SU_CMD) rmdir $@.mnt/lost+found
	$(SU_CMD) bash -c "(cd $@.fs ; tar -c .) | (cd $@.mnt ; tar -x)"
	$(SU_CMD) chown -R root.root $@.mnt
	$(SU_CMD) umount $@.mnt
	mv $@.tmp $@

# Compress the system initrd.
$(TDIR)/initrd.system.gz: $(TDIR)/initrd.system
	gzip -9 < $(TDIR)/initrd.system > $@

clean:
	rm -f \
		$(TDIR)/modules.dep \
		$(TDIR)/initrd-reboot \
		$(TDIR)/busybox.initrd \
		$(TDIR)/busybox.initrd.links \
		$(TDIR)/initrd.installer \
		$(TDIR)/initrd.installer.gz \
		$(TDIR)/initrd.system \
		$(TDIR)/initrd.system.gz \
	rm -rf \
		$(TDIR)/$(BUSYBOX)-initrd \
		$(TDIR)/initrd.installer.fs \
		$(TDIR)/initrd.installer.mnt \
		$(TDIR)/initrd.system.fs \
		$(TDIR)/initrd.system.mnt

distclean: clean

.PHONY: all clean distclean

# vim:set ai noet sw=8 sts=8 ts=8 tw=0:
