# $Progeny: Makefile,v 1.65 2002/04/22 21:15:44 branden Exp $

SHELL=	/bin/sh -e

ifdef DO_PCMCIA_ARCHIVE
PCMCIA_ARCHIVE=$(TDIR)/pcmcia.tgz
endif

# XXX: put ISOLINUX support here

SYSLINUX_CONF_FILES = syslinux.cfg
SYSLINUX_TEXT_FILES = $(TDIR)/screen01.txt \
                    $(TDIR)/screen02.txt \
                    $(TDIR)/screen03.txt \
                    $(TDIR)/screen04.txt \
                    $(TDIR)/screen05.txt \
                    $(TDIR)/screen06.txt \
                    $(TDIR)/screen07.txt \
                    $(TDIR)/screen08.txt \
                    $(TDIR)/screen09.txt \
                    $(TDIR)/screen10.txt

# 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-2.88 $(TDIR)/modules.tgz $(PCMCIA_ARCHIVE)

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

# 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
# Command and options for building ISOs.
MKISOFS=/usr/bin/mkisofs
MKISOFS_OPTS=-l -r
# name of ISO image we'll be building
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 a 2.88 MB floppy image for El Torito bootable CD-ROMs.
$(TDIR)/install-2.88: $(TDIR)/install-2.88.empty $(INITRD) $(SYSLINUX_CONF_FILES) $(SYSLINUX_TEXT_FILES)
	@echo Creating $$(basename $@)...
	cp $(TDIR)/install-2.88.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)
	$(SU_CMD) install $(SYSLINUX_CONF_FILES) $(SYSLINUX_TEXT_FILES) $@.mnt || ($(SU_CMD) umount $@.mnt; rm -rf $@.mnt; exit 1)
	$(SU_CMD) umount $@.mnt
	rmdir $@.mnt
	$(SU_CMD) syslinux $(SYSLINUX_OPTIONS) $@.image
	mv $@.image $@

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

# Create a compressed archive of all kernel modules corresponding
# to the kernel being used.  This archive is not unpacked into
# the initrd; it is too large.  Instead, discover is used to
# cherry-pick required modules from it.
$(TDIR)/modules.tgz:
	tar -cf - /lib/modules/$(KERNEL_VERSION) | gzip -9 > $@.tmp
	mv $@.tmp $@

# If the kernel being used has PCMCIA support, create a compressed
# archive of corresponding PCMCIA kernel modules.  This is
# unpacked into the initrd because we can't know what PCMCIA
# modules we need without loading some of these first.
$(PCMCIA_ARCHIVE):
	tar -cf - /lib/modules/$(KERNEL_VERSION)/pcmcia | gzip -9 > $@.tmp
	mv $@.tmp $@

# Generate the information screens displayed by SYSLINUX.
$(SYSLINUX_TEXT_FILES):
	sed -e "s/@DATE@/$$(date --iso-8601=seconds --utc)/" \
	    -e "s/@BUILDER@/$(BUILDER)/" \
	    -e "s/@PGI_VERSION@/$(PGI_VERSION)/" \
	    -e "s/@VERSION@/$(VERSION)/" < $(ETC_DIR)/syslinux.$$(basename $@).in > $@.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" \
	 -b install-2.88 -o $@.tmp $(INSTALLER_FILES_FS)
	mv --backup=numbered $@.tmp $@

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

distclean: clean

.PHONY: all clean distclean

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