# $Progeny: Makefile,v 1.18 2002/04/22 21:16:12 branden Exp $

SHELL=	/bin/sh -e

# XXX: put ISOLINUX support here

# initrd we're using, created by stage0
INITRD = $(TDIR)/initrd.installer.gz

# list of files to be placed on the boot media
INSTALLER_FILES = $(TDIR)/install

# directory containing $(INSTALLER_FILES) and the live filesystem
INSTALLER_FILES_FS = $(TDIR)/installer-files.fs

# size (in kB) of the EFI filesystem we will create to house elilo, the kernel, etc.
IMAGE_SIZE = 10000

# What we build in this Makefile depends on our final product.
# If it's an installer-only ISO, we'll have everything we need,
# and can produce one here.  Otherwise, we'll merely generate
# files for the ISO that the archive/ directory will produce, and
# calculate the space these files will occupy, since that space
# will be unavailable for a package archive on the ISO.

ifdef INSTALLER_ONLY
MKISOFS=/usr/bin/mkisofs
MKISOFS_OPTS=-l -r
ISO=$(OUT)/$(CODENAME)-installer-$(ARCH).iso
else
# file containing calculated size of $(INSTALLER_FILES_FS) (see archive/Makefile)
INSTALLER_FILES_FS_SIZE = $(TDIR)/installer-files.fs.size
endif

all: $(INSTALLER_FILES_FS_SIZE) $(ISO)

# Create an EFI (VFAT) filesystem that contains elilo, the
# kernel, and the initrd; everything we need to boot.
$(TDIR)/install: $(TDIR)/install.empty $(INITRD)
	@echo Creating $$(basename $@)...
	cp $(TDIR)/install.empty $@.image
	mkdir $@.mnt
	$(SU_CMD) mount -oloop -tvfat $@.image $@.mnt
	$(SU_CMD) install /boot/vmlinuz-$(KERNEL_VERSION) $@.mnt/linux || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) install $(INITRD) $@.mnt/initrd || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	# install ELILO to the install image
	$(SU_CMD) mkdir -p $@.mnt/boot/efi || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) install /usr/lib/elilo/elilo.efi $@.mnt || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) install /usr/lib/elilo/elilo.efi $@.mnt/boot/efi/bootia64.efi || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) echo "elilo linux" > $@.mnt/install.nsh || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) install elilo.conf $@.mnt || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) umount $@.mnt
	rmdir $@.mnt
	mv $@.image $@

# Create an empty EFI (VFAT) filesystem.
$(TDIR)/install.empty:
	mkdosfs -C $@.tmp $(IMAGE_SIZE) >& /dev/null
# mkdosfs makes the image executable for some silly reason
	chmod ugo-x $@.tmp
	mv $@.tmp $@

# Create a directory tree containing everything that will be
# placed on the ISO (not including package archives, which are
# generated in archive/ if required).
$(INSTALLER_FILES_FS): $(INSTALLER_FILES)
	-rm -r $@.tmp $@
	mkdir $@.tmp
	cp -a $(INSTALLER_FILES) $@.tmp
	cp -a $(TDIR)/live/* $@.tmp
	mv $@.tmp $@

# Calculate the disk consumption of the files to be placed on the
# ISO, so we know how much less room will be available on the
# (first) ISO for use by a package archive.
$(INSTALLER_FILES_FS_SIZE): $(INSTALLER_FILES_FS)
	du -bs $(INSTALLER_FILES_FS) | awk '{print $$1}' > $@.tmp
	mv $@.tmp $@

# If we're building an installer-only ISO, create it now.
$(ISO): $(INSTALLER_FILES_FS)
	@echo "Generating the installer ISO image..."
	$(MKISOFS) $(MKISOFS_OPTS) -V "$(CODENAME) Installer" \
	  -no-emul-boot -J -b install -o $@.tmp $(INSTALLER_FILES_FS)
	mv --backup=numbered $@.tmp $@

clean:
	rm -rf \
	    $(INSTALLER_FILES) \
	    $(INSTALLER_FILES_FS) \
	    $(INSTALLER_FILES_FS_SIZE) \
	    $(TDIR)/install.empty \
	    $(TDIR)/install.image \
	    $(TDIR)/install.mnt \

distclean: clean

.PHONY: all clean distclean

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