#!/bin/zsh
DO=$0
main() {
	args build_dir fstab sysrcd_dir supplement_dir
	depend sysresccd-image-mounted build_dir:source_dir fstab:prev_fstab
	init-build-dir-vars

	depend sysresccd-supplement-enabled build_dir:sysrcd_dir fstab:sysrcd_fstab
	depend mksquashfs build_dir:sysrcd_dir fstab:sysrcd_fstab \
		filename:sysrcd_dat options=-comp\ gzip

	depend sysresccd-supplement build_dir:supplement_dir fstab:supplement_fstab
	depend mksquashfs build_dir:supplement_dir fstab:supplement_fstab \
		filename:supplement_dat options=-comp\ gzip

	depend sysresccd-patched-initrd build_dir:initrd_dir fstab:initrd_fstab
	depend pack-cpio build_dir:initrd_dir fstab:initrd_fstab \
		filename:initram_igz compressor=xz

	eval $(getvars)
	output-dir $changes_dir
	output-dir $work_dir

	dep_add_file $sysrcd_dat
	dep_add_file $supplement_dat
	dep_add_file $initram_igz

	touch $changes_dir/sysrcd.dat || die
	touch $changes_dir/supplement.dat || die
	mkdir $changes_dir/isolinux || die
	touch $changes_dir/isolinux/initram.igz || die

	mkdir -p $build_dir || \
		die "$0: Could not create build directory ${(qqq)build_dir}"

	local fstab_add
	fstab_add=$(build-overlay-fstab "$source_dir") || die "$0: build-overlay-fstab failed"
	fstab-add-and-mount \
		"$fstab_add" \
		"$sysrcd_dat	$build_dir:A/sysrcd.dat	none	bind,ro	0 0" \
		"$supplement_dat	$build_dir:A/supplement.dat	none	bind,ro	0 0" \
		"$initram_igz	$build_dir:A/isolinux/initram.igz	none	bind,ro	0 0" \

	(cd $build_dir && md5sum sysrcd.dat >sysrcd.md5) || die
	(cd $build_dir && md5sum supplement.dat >supplement.md5) || die
}
. ./common.zsh
# vim: fileencoding=utf8 ft=zsh noet sw=4 ts=4 sts=4