#!/usr/bin/make -f


SHELL=/bin/sh


# Version and filename info.
VERSION=3.0
INSTANT_EPRINTS=Instant_EPrints_$(VERSION)_i386
UBUNTU=Ubuntu_6.10_i386

# Directories.
LIVECD=livecd
OLD_SQUASH=old-squashfs
NEW_SQUASH=new-squashfs

# Miscellaneous files.
REMOVE_FILES=$(NEW_SQUASH)/etc/resolv.conf $(NEW_SQUASH)/etc/apt/apt.conf \
	$(NEW_SQUASH)/Makefile.chroot $(NEW_SQUASH)/usr/sbin/policy-rc.d

# Commands locations.
ECHO=/bin/echo
SUDO=/usr/bin/sudo
RM=/bin/rm
CP=/bin/cp
MOUNT=/bin/mount
UMOUNT=/bin/umount
MKDIR=/bin/mkdir
SED=/bin/sed
DD=/bin/dd
CHMOD=/bin/chmod


.PHONY: filesystem squashfs iso cleanup


# Build ISO image.

iso: $(INSTANT_EPRINTS).iso

$(INSTANT_EPRINTS).iso: $(LIVECD)/casper/filesystem.squashfs
	@$(ECHO)
	@$(ECHO) "=== BUILDING ISO IMAGE ==="

	@# Update live CD MD5 hash. We do a /bin/sh -c to run the find command
	@# so that we can use output redirection. Otherwise we'll get permission
	@# errors.
	@$(ECHO)
	@$(ECHO) "=== Updating live CD MD5 hash (takes about 1.5 minutes)."
	$(SUDO) $(RM) -f $(LIVECD)/md5sum.txt
	@$(ECHO) "find . -type f -print0 | xargs -0 md5sum > md5sum.txt"
	@cd $(LIVECD) && $(SUDO) /bin/sh -c \
		'/usr/bin/find . -type f -print0 | xargs -0 md5sum > md5sum.txt'

	@# Create live CD ISO.
	@$(ECHO)
	@$(ECHO) "=== Building final ISO image (takes about 40 seconds)."
	$(SUDO) /usr/bin/mkisofs \
		-o $@ \
		-b isolinux/isolinux.bin \
		-c isolinux/boot.cat \
		-no-emul-boot \
		-boot-load-size 4 \
		-boot-info-table \
		-r \
		-V "Instant EPrints 3.0 i386" \
		-cache-inodes \
		-J \
		-input-charset iso8859-1 \
		-allow-leading-dots \
		$(LIVECD)

	@$(ECHO)
	@$(ECHO) "=== Done!"


# Build the new squashfs for the ISO.

squashfs: $(LIVECD)/casper/filesystem.squashfs

$(LIVECD)/casper/filesystem.squashfs: $(INSTANT_EPRINTS)_work.ext2
	@$(ECHO)
	@$(ECHO) "=== BUILDING NEW SQUASHFS ==="

	@# Mount squashfs source filesystem and set up work environment.
	@$(ECHO)
	@$(ECHO) "=== Initialising."
	@$(SUDO) $(MOUNT) -o loop $< $(NEW_SQUASH)
	$(SUDO) $(MOUNT) -t proc --bind /proc $(NEW_SQUASH)/proc
	$(SUDO) $(CP) -p /etc/resolv.conf $(NEW_SQUASH)/etc
	@# Reconfigure the panel icons by means of pre-built gconf defaults.
	@# I did it this way because I couldn't figure out how to do it on
	@# the fly with gconftool (if that's even possible).
	$(SUDO) $(CP) -p gconf-tree.xml \
		$(NEW_SQUASH)/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

	@# Copy in cached .deb files to save on downloads and set up apt
	@# sources, etc. An appropriate sources.list can be generated
	@# using the utility at http://www.ubuntu-nl.org/source-o-matic/.
	@$(ECHO)
	@$(ECHO) "=== Setting up apt (takes about 10 seconds)."
	$(SUDO) $(CP) -a apt-cache/. $(NEW_SQUASH)/var/cache/apt/archives
	@# HACK: I've been having problems with the update servers. Since it
	@# takes so long to get back to this point in the process, I've
	@# cheated and pre-generated the various index files. Just drop 'em
	@# in! They should be safe to leave there afterwards.
	$(SUDO) $(CP) -a apt-lists/. $(NEW_SQUASH)/var/lib/apt/lists
	$(SUDO) $(CP) -p /etc/apt/apt.conf $(NEW_SQUASH)/etc/apt
	$(SUDO) $(CP) sources.list $(NEW_SQUASH)/etc/apt
	@# The following stops the daemons from attempting to restart, which
	@# will fail miserably in a chroot environment. Make sure to set the
	@# permissions correctly, just in case they've been eaten by CVS or
	@# something.
	$(SUDO) $(CP) policy-rc.d $(NEW_SQUASH)/usr/sbin
	$(SUDO) $(CHMOD) 755 $(NEW_SQUASH)/usr/sbin/policy-rc.d
	@# Finally, preseed the debconf database so that we don't get asked
	@# any annoying configuration questions during installs.
	$(SUDO) $(CP) -a debconf-db/. $(NEW_SQUASH)/var/cache/debconf

	@# Chroot to the squashfs source filesystem and reconfigure
	@# installed packages. Note that we need to stop Webmin for the
	@# duration, otherwise installing Webmin in the chroot fails ("port
	@# 10000 is already in use").
	@$(ECHO)
	@$(ECHO) "=== Reconfiguring installed packages (takes about 2.5 minutes)."
	@-$(SUDO) /etc/init.d/webmin stop
	@$(SUDO) $(CP) Makefile.chroot $(NEW_SQUASH)
	@$(SUDO) chroot $(NEW_SQUASH) /usr/bin/env HOME=/root LC_ALL=C \
		DEBIAN_FRONTEND=noninteractive make -f Makefile.chroot
	@-$(SUDO) /etc/init.d/webmin start

	@# Clean up.
	@$(ECHO)
	@$(ECHO) "=== Cleaning up."
	$(SUDO) $(UMOUNT) $(NEW_SQUASH)/proc
	$(SUDO) $(RM) -f $(REMOVE_FILES)
	$(SUDO) $(RM) -rf $(NEW_SQUASH)/tmp/*

	@# Update manifest and disk name.
	@$(ECHO)
	@$(ECHO) "=== Updating manifest and disk name."
	$(SED) -i -e 's/DISKNAME  .*/DISKNAME  Instant EPrints 3.0 - i386/' \
		$(LIVECD)/README.diskdefines
	$(SUDO) chroot $(NEW_SQUASH) \
		dpkg-query -W --showformat='$(Package) $(Version)\n' \
		> $(LIVECD)/casper/filesystem.manifest
	$(SUDO) $(CP) $(LIVECD)/casper/filesystem.manifest \
		$(LIVECD)/casper/filesystem.manifest-desktop
	$(SED) -i -e '/ubiquity/d' $(LIVECD)/casper/filesystem.manifest-desktop

	@# Clear free space on the squashfs source filesystem.
	@$(ECHO)
	@$(ECHO) "=== Clearing free space on new squashfs."
	-$(SUDO) $(DD) if=/dev/zero of=$(NEW_SQUASH)/dummyfile
	$(SUDO) $(RM) $(NEW_SQUASH)/dummyfile

	@# Regenerate the live CD squashfs.
	@$(ECHO)
	@$(ECHO) "=== Building new squashfs (takes about 10 minutes)."
	$(SUDO) $(RM) -f $@
	$(SUDO) mksquashfs $(NEW_SQUASH) $@

	@# Unmount the squashfs source filesystem.
	@$(SUDO) $(UMOUNT) $(NEW_SQUASH)

	@$(ECHO)
	@$(ECHO) "=== Done!"

$(INSTANT_EPRINTS)_work.ext2: $(INSTANT_EPRINTS).ext2
	@$(ECHO)
	@$(ECHO) "=== Making working copy of original filesystem (takes about 2.5 minutes)."
	$(SUDO) $(CP) $< $@


# Build source filesystem.

filesystem: $(INSTANT_EPRINTS).ext2

$(INSTANT_EPRINTS).ext2: $(UBUNTU).iso
	@$(ECHO)
	@$(ECHO) "=== BUILDING SOURCE FILESYSTEM ==="

	@# Make a copy of the original ISO.
	@$(ECHO)
	@$(ECHO) "=== Copying original ISO (takes about 30 seconds)."
	@$(SUDO) $(MOUNT) -t iso9660 -o loop $< /cdrom
	@$(MKDIR) -p $(LIVECD)
	$(SUDO) $(RM) -rf $(LIVECD)/*
	$(CP) -a /cdrom/. $(LIVECD)
	$(CHMOD) -R u+w $(LIVECD)
	@$(SUDO) $(UMOUNT) /cdrom

	@# Remove unnecessary Windows cruft.
	@$(ECHO)
	@$(ECHO) "=== Removing "`/usr/bin/du -smh $(LIVECD)/programs | /usr/bin/cut -d'M' -f1`"MiB of unnecessary Windows crud."
	$(RM) -rf $(LIVECD)/programs

	@# Mount original compressed filesystem.
	@$(MKDIR) -p $(OLD_SQUASH)
	@$(SUDO) $(MOUNT) -t squashfs -o loop,ro \
		$(LIVECD)/casper/filesystem.squashfs $(OLD_SQUASH)

	@# Create image for new compressed filesystem. Add on an extra 500MiB
	@# to allow for the temporary files that we're going to copy in (mostly
	@# cached .deb files.
	@$(ECHO)
	@$(ECHO) "=== Creating new 2.5 GiB blank file system (takes about 1.5 minutes)."
	$(SUDO) $(DD) if=/dev/zero \
		of=$@ bs=1M count=2500
	$(SUDO) mke2fs -F -q $@

	@# Mount new compressed filesystem.
	@$(MKDIR) -p $(NEW_SQUASH)
	@$(SUDO) $(MOUNT) -o loop $@ $(NEW_SQUASH)

	@# Copy the old compressed filesystem.
	@$(ECHO)
	@$(ECHO) "=== Copying original compressed filesystem (takes about 2.5 minutes)."
	$(SUDO) $(CP) -a $(OLD_SQUASH)/. $(NEW_SQUASH)

	@# Unmount all compressed filesystems.
	@$(SUDO) $(UMOUNT) $(OLD_SQUASH)
	@$(SUDO) $(UMOUNT) $(NEW_SQUASH)
	@$(ECHO)
	@$(ECHO) "=== Done!"


# Unmount everything that might be mounted. Useful if the make dies partway
# through.

cleanup:
	@$(ECHO)
	@$(ECHO) "=== Cleaning up mounted items."
	-$(SUDO) $(UMOUNT) $(NEW_SQUASH)/proc
	-$(SUDO) $(UMOUNT) $(NEW_SQUASH)
	-$(SUDO) $(UMOUNT) $(OLD_SQUASH)
	-$(SUDO) $(UMOUNT) /cdrom
