# This is a convenience Makefile to add SystemDisk, BootX and
# a couple of current linux kernels to a bootable Mac OS CD.
# It tries to do the right thing (tm) ;-)
#
#
# $name: Makefile$ 
# $author: Christian Mueller <cmue81 at gmx dot de>$ 
# $copyright: Copyright (C) 2007 by Christian Mueller$
# $license: http://www.gnu.org/licenses/gpl.html$
# $license-version: GNU GPL version 2$ 
# $date: 2007-01-09$ 
#
#
# Dependencies: hfsutils, cdr-tools
#
# Usage: The only thing you should need to adjust is the
#        BOOTABLE_MAC_CD variable or *URL if the URLs listed
#        deliver a "404 not found".  If you have a rewritable
#        cd handy, adjust CDBURNER and try make cdrw.
#
# Motivation: Even in the lucky case you get bootloaders for
#             a Beige G3 working (miboot, quik, etc.) you
#             would still need a booting Mac OS System to
#             apply the System Disk patches in case they
#             get screwed up.  So if you're the owner of a
#             proprietary Mac OS CD let this Makefile convert
#             it to a useful utility CD in case you get stuck
#             with stand-alone-linux on your Mac-Box.
#
# WARNING: The image given with BOOTABLE_MAC_CD will get
#          modified! It might not work, so do backups !!
#
# NOTE: You cannot compress your kernels if you want to use
#       them with BootX ;-(
#
# Tested on a Beige G3 with OpenFirmware 2.4, might work on
# other models.  Hopefully we will not need this anymore
# some day, when support for Apple's oldworld machines has
# grown up ;-)  Ciao ..
#
BOOTABLE_MAC_CD = /media/hdb1/macos8.iso
CDBURNER = ATA:1,0,0


SELF = Makefile

SYSDISK = SystemDisk2.3.1.smi.bin

STUFFIT = stuffit520.611linux-i386.tar.gz
UNSTUFF = bin/unstuff

BOOTX = BootX_1.2.2.sit
BOOTX_DIR = BootX_1.2.2
BOOTX_DIR_KRNL = $(BOOTX_DIR)/Linux_Kernels

GEN_IMG = install-ppc-minimal-2006.1.iso


SYSDISKURL = ftp://ftp.apple.com/developer/macosxserver/utilities/$(SYSDISK)
STUFFITURL = http://www.allume.com/downloads/files/$(STUFFIT)
BOOTXURL = http://penguinppc.org/historical/benh/$(BOOTX)
DEBURL = http://people.debian.org/~wouter/d-i/powerpc/daily/powerpc
GENMIRROR = ftp://ftp.wh2.tu-dresden.de/pub/mirrors/gentoo
GENURL = $(GENMIRROR)/releases/ppc/current/ppc32/installcd/$(GEN_IMG)




modimage: hfsmount \
	$(SELF).treecopy \
	$(SYSDISK).treecopy \
	debianstuff \
	gentoostuff \
	$(BOOTX_DIR).treecopy \
	hfsumount

cd: modimage
	cdrecord -dao dev=$(CDBURNER) $(BOOTABLE_MAC_CD)

cdclean:
	cdrecord dev=$(CDBURNER) blank=fast

cdrw: cdclean cd

clean:
	umount -d genloop || exit 0
	rm -rf bin/ genloop/ "$(BOOTX_DIR)" \
          *.dirs *.files *irdbuild *irdmerge .*irdmerge

distclean: clean
	rm $(SYSDISK) $(STUFFIT) $(BOOTX) $(GEN_IMG)




hfsumount:
	humount

hfsmount:
	hmount $(BOOTABLE_MAC_CD)

TCSPEC1 = s|hcopy -m\(.*$(BOOTX_DIR_KRNL)\)|hcopy -r\1|
TCSPEC2 = s|hcopy -m\(.*$(SELF)\)|hcopy -t\1|
TREECOPYSPECIALS = $(TCSPEC1);$(TCSPEC2);
CPLSED = h;s|/|:|g;s|_| |g;s|^ '\(.*:\)\?.*| ':\1'|;H;g;s/\n//;s/^/hcopy -m/;
%.treecopy: hfsmount %
	echo "#!/bin/sh" > "$*.dirs" && chmod +x "$*.dirs"
	echo "#!/bin/sh" > "$*.files" && chmod +x "$*.files"
	find "$*" -type d -printf "hmkdir '/%p'\n" | \
          sed -e "s|/|:|g;s|_| |g;$(TREECOPYSPECIALS)" >> "$*.dirs"
	find "$*" -not -type d -printf " '%p'\n" | \
          sed -ne "$(CPLSED)$(TREECOPYSPECIALS);p" >> "$*.files"
	"./$*.dirs" 2>/dev/null || exit 0
	"./$*.files" || exit 0

%.irdmerge:
	mkdir -p "$(suffix $*).irdmerge" && \
	cd "$(suffix $*).irdmerge" && \
	gunzip -c "../$(BOOTX_DIR_KRNL)/$(subst .,,$(suffix $*))$(patsubst \
          %$(suffix $*),%,$*)ird.gz" > "initrd.tmp" && \
	cpio -i < "initrd.tmp" 2> /dev/null && \
	rm "initrd.tmp" && \
	cd .. && \
	touch "$@"

%.irdbuild:
	test -e "$(patsubst %$(suffix $*),%,$*).gz" || { \
	cd $(suffix $*).irdmerge && \
	find > ../files$(suffix $*).irdmerge && \
	cpio -o < ../files$(suffix $*).irdmerge > "../$(patsubst \
          %$(suffix $*),%,$*)" && \
	cd .. ; }

%.gz: %
	test -e "$@" || gzip -9 "$*"




$(STUFFIT):
	wget $(STUFFITURL)

$(UNSTUFF): $(STUFFIT)
	tar xzf $(STUFFIT) $(UNSTUFF)
	touch $(UNSTUFF)

$(BOOTX):
	wget $(BOOTXURL)

$(BOOTX_DIR): $(UNSTUFF) $(BOOTX)
	$(UNSTUFF) -m=on $(BOOTX)
	mv "$(subst _, ,$@)" "$@"

$(BOOTX_DIR_KRNL): $(BOOTX_DIR)
	mv "$(subst Linux_,Linux ,$@)" "$@"




$(GEN_IMG):
	wget $(GENURL)

$(GEN_IMG).mount: $(GEN_IMG)
	mkdir -p genloop && \
	mount -o loop $(GEN_IMG) genloop

$(BOOTX_DIR_KRNL)/genliveird.gz: genloop/boot/apple.igz
	cp "genloop/boot/apple.igz" "$@"

 .gen.irdmerge/image.squashfs: genloop/image.squashfs
	cp "genloop/image.squashfs" "$@"

$(BOOTX_DIR_KRNL)/genallinitrd: $(BOOTX_DIR_KRNL)/genliveird.gz \
        live.gen.irdmerge \
        .gen.irdmerge/image.squashfs \
        $(BOOTX_DIR_KRNL)/genallinitrd.gen.irdbuild

$(BOOTX_DIR_KRNL)/genvmlinux: genloop/boot/apple
	test -e "$@.gz" || cp "genloop/boot/apple" "$@"

gentoostuff: $(GEN_IMG).mount \
        $(BOOTX_DIR_KRNL) \
        $(BOOTX_DIR_KRNL)/genliveird.gz \
        $(BOOTX_DIR_KRNL)/genallinitrd.gz \
        $(BOOTX_DIR_KRNL)/genvmlinux
	umount -d genloop




$(BOOTX_DIR_KRNL)/deb%ird.gz:
	wget -O "$@" $(DEBURL)/$*/initrd.gz

$(BOOTX_DIR_KRNL)/deballinitrd: $(BOOTX_DIR_KRNL)/debnetbootird.gz \
        $(BOOTX_DIR_KRNL)/debcdromird.gz \
        $(BOOTX_DIR_KRNL)/debhd-mediaird.gz \
        netboot.deb.irdmerge \
        cdrom.deb.irdmerge \
        hd-media.deb.irdmerge \
        $(BOOTX_DIR_KRNL)/deballinitrd.deb.irdbuild

$(BOOTX_DIR_KRNL)/debvmlinux:
	test -e "$@.gz" || wget -O "$@" $(DEBURL)/cdrom/vmlinux

debianstuff: $(BOOTX_DIR_KRNL) \
        $(BOOTX_DIR_KRNL)/debnetbootird.gz \
        $(BOOTX_DIR_KRNL)/debcdromird.gz \
        $(BOOTX_DIR_KRNL)/debhd-mediaird.gz \
        $(BOOTX_DIR_KRNL)/deballinitrd.gz \
        $(BOOTX_DIR_KRNL)/debvmlinux




$(SYSDISK):
	wget $(SYSDISKURL)

