#
# Makefile for DocoMomo - USB boot Linux
# Targets to install disk images into the USB memory on Linux
#
# Copyright (C) 2006 zunda <zunda at freeshell.org>
# 
# Permission is granted for copying, modification, distribution,
# and distribution of modified versions of this work under the
# terms of GPL version 2 or later.
#
# Read the GPL.txt file or gpl-3.0.txt file for more details.
#

PACKAGE = docomomo
VERSION = 0.11.2

# DocoMomo configuration
home_size = 100M
usb_disk = `dmesg | grep -i 'Attached scsi removable disk' | tail -1 | sed 's/.*\(sd[a-z]\).*/\1/'`
mbr_dev = /dev/$(usb_disk)
target_dev = /dev/${usb_disk}1
skelton_dir = skel

# Directory settings
top_srcdir = .
build_dir = .

conf_dir = $(top_srcdir)/conf
script_dir = $(top_srcdir)/scripts

# Directory to be used during preparation and installation
tmp_dir = $(build_dir)/tmp
root_dir = $(build_dir)/root
absolute_root_dir = `python $(script_dir)/absolute-path.py $(root_dir)`
initrd_dir = $(build_dir)/initrd
mnt_dir = $(build_dir)/mnt
absolute_mnt_dir = `python $(script_dir)/absolute-path.py $(mnt_dir)`

# Extract /home for unlevel 3
PHONY: create-home-image
create-home-image: $(tmp_dir)/home.ext
$(tmp_dir)/home.ext: $(root_dir)/home
	@echo '*** Creating home image'
	dd if=/dev/zero of=$@ bs=$(home_size) count=1
	/sbin/mke2fs -F $@
	rm -rf $(tmp_dir)/mnt/home
	mkdir -p $(tmp_dir)/mnt/home
	mount -o loop $@ $(tmp_dir)/mnt/home
	cp -a $(root_dir)/home $(tmp_dir)/mnt
	umount $(tmp_dir)/mnt/home

PHONY: mount-usb
mount-usb:
	@echo '*** Mounting USB memory'
	mkdir -p $(mnt_dir)
	grep -q $(absolute_mnt_dir) /proc/mounts || mount $(target_dev) $(absolute_mnt_dir)
	grep -q $(absolute_mnt_dir) /proc/mounts
	mkdir -p $(mnt_dir)/boot/grub

PHONY: install-initrd-image
install-initrd-image: $(mnt_dir)/boot/initrd.img
$(mnt_dir)/boot/initrd.img: $(tmp_dir)/initrd.img mount-usb $(root_dir)/etc/docomomo-release
	@echo '*** Installing initrd image'
	if [ $< -nt $@ ]; then \
		cp $< $@; \
		cat $(root_dir)/etc/docomomo-release > $(mnt_dir)/boot/docomomo-release.txt; \
		if [ -e $(mnt_dir)/boot/etc.tgz ]; then \
			mv $(mnt_dir)/boot/etc.tgz $(mnt_dir)/boot/etc.bak; \
		fi; \
	fi

PHONY: install-usr-image
install-usr-image: $(mnt_dir)/boot/usr.sqs
$(mnt_dir)/boot/usr.sqs: $(tmp_dir)/usr.sqs mount-usb
	@echo '*** Installing /usr image'
	if [ $< -nt $@ ]; then cp $< $@; fi

PHONY: install-home-image
install-home-image: $(mnt_dir)/boot/home.ext
$(mnt_dir)/boot/home.ext: $(tmp_dir)/home.ext mount-usb
	@echo '*** Installing /home image'
	if [ ! -e $@ ]; then cp $< $@; fi

PHONY: install-images
install-images: install-initrd-image install-usr-image install-home-image

PHONY: install-grub-conf
install-grub-conf: $(mnt_dir)/boot/grub/grub.conf
$(mnt_dir)/boot/grub/grub.conf: $(conf_dir)/grub.conf mount-usb
	@echo '*** Installing grub.conf'
	if [ $< -nt $@ ]; then \
		usb_disk=${usb_disk}; \
		uuid=`find /dev/disk/by-uuid -lname \*$${usb_disk}1 -printf %f`; \
		echo Target UUID is $$uuid; \
		if [ -n "$$uuid" ]; then \
			sed "s/%UUID%/docomomo_uuid=$$uuid/g" $< > $@; \
		else \
			sed 's/ *%UUID%//g' $< > $@; \
		fi \
	fi

# Sorry, grub in Momonga comes with the splash image feature anyway
PHONY: install-grub-splash
install-grub-splash: $(mnt_dir)/boot/grub/docomomo.xgz
$(mnt_dir)/boot/grub/docomomo.xgz: $(conf_dir)/docomomo.xgz mount-usb
	@echo '*** Installing the splash image'
	if [ $< -nt $@ ]; then cp $< $@; fi

PHONY: install-grub
install-grub: $(mnt_dir)/boot/vmlinuz
$(mnt_dir)/boot/vmlinuz: $(root_dir)/boot/vmlinuz mount-usb
	@echo '*** Installing grub files'
	if [ $(root_dir)/boot/vmlinuz -nt $@ ]; then cp $(root_dir)/boot/vmlinuz $@; fi
	# sorry, we use grub files from the parent system
	if [ ! -e $(mnt_dir)/boot/grub/device.map ]; then /sbin/grub-install --root-directory=`python $(script_dir)/absolute-path.py $(mnt_dir)` $(mbr_dev); fi

PHONY: install-docs
install-docs: $(mnt_dir)/boot/docomomo-$(VERSION).txt
$(mnt_dir)/boot/docomomo-$(VERSION).txt: $(top_srcdir)/README.ja mount-usb
	if [ $< -nt $@ ]; then \
		rm -f $(mnt_dir)/boot/docomomo-*.*.*.txt; \
		nkf -s $< > $@; \
	fi

PHONY: install
install: install-images install-grub-conf install-grub-splash install-grub install-docs
	@echo "*** Do not forget to umount $(target_dev) or make umount-usb"

PHONY: umount-usb
umount-usb:
	if ( grep -q $(absolute_mnt_dir) /proc/mounts ); then \
		umount $(absolute_mnt_dir); \
	fi

PHONY: clean-install
clean-install: umount-usb
	rm -rf $(mnt_dir)
