# $Progeny: Makefile,v 1.53 2002/04/23 20:38:28 branden Exp $

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

# Makefile quoting is nasty... double $ and \# lie within
# This gets rid of comment lines, empty lines, and lines
# containing nothing but white space.
COMMENTFILTER=	(sed -e 's/\#.*$$//' | egrep -v '^[[:space:]]*$$')

all: $(TDIR)/live

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

# Compile the live filesystem's version of the busybox binary.
$(TDIR)/busybox.live: $(TDIR)/$(BUSYBOX)-live
	cp $(PGI_DIR)/busybox.h $(TDIR)/$(BUSYBOX)-live/Config.h
	cd $(TDIR)/$(BUSYBOX)-live && (echo -n "APPLET_SOURCES := "; BB_SRC_DIR= sh $(PGI_DIR)/busybox.sh Config.h -DPGI_LIVE_FS) > applet_source_list
	cd $(TDIR)/$(BUSYBOX)-live && $(MAKE) CFLAGS_EXTRA=-DPGI_LIVE_FS
	cp $(TDIR)/$(BUSYBOX)-live/busybox $@

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

# Generate the list of "extra packages" we will be passing to
# debootstrap for installation.
$(TDIR)/extra_packages:
	-cat internal.extra_packages $(ARCH)/extra_packages $(ETC_DIR)/extra_packages \
	  | $(COMMENTFILTER) > $@.in
	python $(TOOLS)/pgi-calc-deps.py < $@.in > $@.tmp
	rm $@.in
	mv $@.tmp $@

# Generate a Glade interface file for the graphical installer.
$(TDIR)/installer.glade:
	sed -e "s/@VERSION@/$(VERSION)/" < installer.glade.in > $@

# Generate the list of devices that we will place on the live
# filesystem.
$(TDIR)/live.devices.list:
	-cat internal.devices.list $(ARCH)/devices.list $(ETC_DIR)/live.devices.list > $@

# Generate the list of directories to be created on the live
# filesystem.
$(TDIR)/live.dirs.list:
	-cat internal.dirs.list $(ARCH)/dirs.list $(ETC_DIR)/live.dirs.list > $@

# Generate the list of files to be copied to the live filesystem
# (not including shared libraries needed by anything in this
# list).  Also, check to make sure that everything we need is
# actually present.
$(TDIR)/live.files.list:
	-cat internal.files.list $(ARCH)/files.list $(ETC_DIR)/live.files.list > $@.tmp
	cat $@.tmp | $(COMMENTFILTER) | awk '{ print $$1 }' \
		| (while read file; do if [ ! -e $$file ]; then echo $$file; \
			fi; done) \
		> $(TDIR)/missing-files
	if [ $$(cat $(TDIR)/missing-files | wc -l) -gt 0 ]; then \
		echo "Files missing; look in $(TDIR)/missing-files for a list"; \
		exit 1; \
	fi
	rm $(TDIR)/missing-files
	mv $@.tmp $@

# Generate the messages file to be used by the "welcome" and
# "finished" screens of the text-mode and graphical installers.
$(TDIR)/messages:
	-cat $(ETC_DIR)/messages > $@

# Compile a simple init replacement that launches shells on
# virtual consoles 2 through 6.
$(TDIR)/shellspawn: shellspawn.c
	cc -o $@ shellspawn.c

# Produce the live filesystem.
$(TDIR)/live: $(TDIR)/busybox.live \
              $(TDIR)/busybox.live.links \
              $(TDIR)/extra_packages \
              $(TDIR)/installer.glade \
              $(TDIR)/live.devices.list  \
              $(TDIR)/live.dirs.list \
              $(TDIR)/live.files.list \
              $(TDIR)/messages \
              $(TDIR)/shellspawn
	-$(SU_CMD) rm -r $@.tmp $@
	mkdir $@.tmp
	@echo Creating live filesystem...
	@printf "\tcreating required directories...\n"
	cat $(TDIR)/live.dirs.list | $(COMMENTFILTER) | \
		while read mode dir; do \
			echo mkdir -p -m $$mode $@.tmp/$$dir; \
			mkdir -p -m $$mode $@.tmp/$$dir || exit 1; \
		done
	ln -s . $@.tmp/usr
	ln -s bin $@.tmp/sbin
	ln -s . $@.tmp/usr/X11R6
	ln -s bash $@.tmp/bin/sh
	@printf "\tcopying the discover databases...\n"
	# Copy in discover databases.  Use kernel 2.4 databases
	# preferentially, if they exist.
	for discoverdb in pci pcmcia usb; do \
		basekern=$$(echo $$KERNEL_VERSION | cut -d. -f1-2); \
		if [ -f /usr/share/discover/$${discoverdb}-$$basekern.list ]; then \
			discoverfn=$$discoverdb-$$basekern.lst; else \
			discoverfn=$$discoverdb.lst; \
		fi; \
		install -m 644 /usr/share/discover/$$discoverfn \
			$@.tmp/share/discover/$$discoverfn; \
	done
	@printf "\tcopying the PGI Python modules...\n"
	install -m 755 -d $@.tmp/lib/python2.1/site-packages/pgi
	install -m 644 $(PGI_DIR)/python/pgi/__init__.py $@.tmp/lib/python2.1/site-packages/pgi
	install -m 644 $(PGI_DIR)/python/pgi/auto_partition_base.py $@.tmp/lib/python2.1/site-packages/pgi
	install -m 644 $(PGI_DIR)/python/pgi/auto_partition-$(ARCH).py $@.tmp/lib/python2.1/site-packages/pgi/auto_partition.py
	install -m 644 $(PGI_DIR)/python/pgi/base.py $@.tmp/lib/python2.1/site-packages/pgi
	install -m 644 $(PGI_DIR)/python/pgi/bootloader-$(ARCH).py $@.tmp/lib/python2.1/site-packages/pgi/bootloader.py
	install -m 644 $(PGI_DIR)/python/pgi/common.py $@.tmp/lib/python2.1/site-packages/pgi
	@printf "\tcopying the PGI installer...\n"
	install -m 755 $(PGI_DIR)/stage1/stage1.sh $@.tmp/share/installer
	install -m 755 $(PGI_DIR)/stage1/installer-druid $@.tmp/share/installer
	install -m 755 $(PGI_DIR)/stage1/xinitrc $@.tmp/bin
	install -m 755 $(PGI_DIR)/stage1/list-devices.py $@.tmp/bin/list-devices
	install -m 644 $(ETC_DIR)/sources.list.dist $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/syslog-install.conf $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/theme.jl $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/arrow.xpm $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/check.xpm $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/installer-text.sh $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/installer-x.sh $@.tmp/share/installer
	install -m 755 $(PGI_DIR)/stage1/pgilogs2disk $@.tmp/bin
	install -m 644 $(TDIR)/messages $@.tmp/share/installer
	cat $(TDIR)/extra_packages | $(COMMENTFILTER) \
		> $@.tmp/share/installer/extra_packages
	if [ -e $(ETC_DIR)/preinst.sh ]; then \
		install -m 644 $(ETC_DIR)/preinst.sh $@.tmp/share/installer; \
	fi
	if [ -e $(ETC_DIR)/postinst.sh ]; then \
		install -m 644 $(ETC_DIR)/postinst.sh $@.tmp/share/installer; \
	fi
	if [ -e $(ETC_DIR)/logoicon.xpm ]; then \
		install -m 644 $(ETC_DIR)/logoicon.xpm $@.tmp/share/installer; \
	fi
	if [ "$$(echo $(ETC_DIR)/root_window.*)" != "$(ETC_DIR)/root_window.*" ]; then \
		install -m 644 $(ETC_DIR)/root_window.* $@.tmp/share/installer; \
	fi
	install -m 644 $(PGI_DIR)/stage1/setup-base-text.py $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/setup-bootloader-text.py $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/setup-finalize-text.py $@.tmp/share/installer
	install -m 644 $(PGI_DIR)/stage1/get-message.py $@.tmp/share/installer
	@printf "\tcopying $(KERNEL_VERSION) kernel and modules...\n"
	install -m 755 -d $@.tmp/kernel/boot
	install -m 755 -d $@.tmp/kernel/lib/modules/$(KERNEL_VERSION)
	install -m 644 /boot/System.map-$(KERNEL_VERSION) $@.tmp/kernel/boot
	install -m 644 /boot/config-$(KERNEL_VERSION) $@.tmp/kernel/boot
# XXX: might want to define a KERNEL_PATH variable in the options file
ifeq ($(ARCH),powerpc)
	install -m 644 /boot/vmlinux-$(KERNEL_VERSION) $@.tmp/kernel/boot
else
	install -m 644 /boot/vmlinuz-$(KERNEL_VERSION) $@.tmp/kernel/boot
endif
#	-install -m 644 /boot/initrd.img-$(KERNEL_VERSION) $@.tmp/kernel/boot
	install -m 644 $(TDIR)/initrd.system.gz \
	  $@.tmp/kernel/boot/initrd.img-$(KERNEL_VERSION)
	cp -a /lib/modules/$(KERNEL_VERSION) $@.tmp/kernel/lib/modules
	@printf "\tcopying generated installer files...\n"
	install -m 644 $(TDIR)/installer.glade $@.tmp/share/installer
	@printf "\tcopying shellspawn binary...\n"
	install -m 755 $(TDIR)/shellspawn $@.tmp/sbin
	@printf "\tcopying required files...\n"
	cat $(TDIR)/live.files.list | $(COMMENTFILTER) | \
		while read file name; do \
			[ -n "$$name" ] || name=$$file; \
			echo install -m 755 -d $$(dirname $$name); \
			install -m 755 -d $@.tmp/$$(dirname $$name); \
			echo cp --preserve --dereference $$file $@.tmp/$$name; \
			cp --preserve --dereference $$file $@.tmp/$$name || exit 1; \
		done
	@printf "\tbusybox...\n"
	install -m 755 $(TDIR)/busybox.live $@.tmp/bin/busybox
	@(cd $@.tmp/bin; \
		while read bin; do \
			echo ln -sf busybox $$(basename $$bin); \
			ln -sf busybox $$(basename $$bin) || exit 1; \
		done ) < $(TDIR)/busybox.live.links
	@printf "\tdebootstrap (from ${DEBOOTSTRAP_INSTALL_DIR}/)...\n"
	cp -a ${DEBOOTSTRAP_INSTALL_DIR}/usr/lib/debootstrap $@.tmp/usr/lib
	cp ${DEBOOTSTRAP_INSTALL_DIR}/usr/sbin/debootstrap $@.tmp/usr/sbin
	@printf "\tcopying XF86Config.template...\n"
	install -m 644 XF86Config.template $@.tmp
	@printf "\tmtools symlink hack...\n"
	ln -sf mtools $@.tmp/bin/mformat
	@printf "\txkb dir copy hack...\n"
	cp --dereference -r /etc/X11/xkb $@.tmp/lib/X11/xkb
	rm -rf $@.tmp/lib/X11/xkb/compiled
	ln -sf /var/lib/xkb $@.tmp/lib/X11/xkb/compiled
	@printf "\tX server modules directory hack...\n"
	cp --dereference -r /usr/X11R6/lib/modules $@.tmp/lib
	@printf "\tX fonts copy hack...\n"
	mkdir -p $@.tmp/lib/X11/fonts
	cp --dereference -r /usr/X11R6/lib/X11/fonts/100dpi $@.tmp/lib/X11/fonts
	cp --dereference -r /usr/X11R6/lib/X11/fonts/misc $@.tmp/lib/X11/fonts
	@printf "\tX locale support copy hack...\n"
	mkdir -p $@.tmp/lib/X11/locale
	cp --dereference -r /usr/X11R6/lib/X11/locale/C $@.tmp/lib/X11/locale
	cp --dereference -r /usr/X11R6/lib/X11/locale/iso8859-1 $@.tmp/lib/X11/locale
	cp /usr/X11R6/lib/X11/locale/locale.alias \
		/usr/X11R6/lib/X11/locale/locale.dir \
		$@.tmp/lib/X11/locale
	@printf "\txkbcomp symlink hack...\n"
	ln -sf /live/bin/xkbcomp $@.tmp/lib/X11/xkb/xkbcomp
	@printf "\tcopying libraries required by commands...\n"
	for i in $$(sh $(PGI_DIR)/tools/so-deps.sh \
			$@.tmp/bin/* \
			$@.tmp/usr/bin/* \
			$@.tmp/sbin/* \
			$@.tmp/usr/sbin/*); do \
		cp -f $$i $@.tmp/lib; \
	done
	@printf "\tcopying libraries required by Python...\n"
	find $@.tmp/lib/python2.1 -name "*.so" | \
		xargs sh $(PGI_DIR)/tools/so-deps.sh | \
		while read lib; do \
			cp -f $$lib $@.tmp/lib; \
		done
	$(SU_CMD) chown root.root -R $@.tmp
	@printf "\tcreating device files...\n"
	cat $(TDIR)/live.devices.list | $(COMMENTFILTER) | \
		( cd $@.tmp/dev; \
			while read dev; do \
				echo $(SU_CMD) /sbin/MAKEDEV $$dev; \
				$(SU_CMD) /sbin/MAKEDEV $$dev || exit 1; \
			done )
	mv $@.tmp $@

clean:
	rm -f \
		$(TDIR)/busybox.live \
		$(TDIR)/busybox.live.links \
		$(TDIR)/extra_packages \
		$(TDIR)/installer.glade \
		$(TDIR)/messages \
		$(TDIR)/missing-files \
		$(TDIR)/live.devices.list \
		$(TDIR)/live.dirs.list \
		$(TDIR)/live.files.list \
		$(TDIR)/shellspawn
	rm -rf \
		$(TDIR)/$(BUSYBOX)-live \
		$(TDIR)/live

distclean: clean

.PHONY: all clean distclean

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