<?xml version="1.0"?>
<package name="make-live-image">
	<description>EFI Bootable Rescue / Live / Installation Image</description>
	<releases>
		<release version="20220319" revision="1">
			<settings>
				<register>no</register>
			</settings>
			<postbuild>
				<cmd>cd $CHROOTDIR</cmd>

				<!-- replace /etc/machine-id by empty file for Live CD
				     systemd will replace empty file on startup -->
				<cmd>rm -vf etc/machine-id</cmd>
				<cmd>touch etc/machine-id</cmd>

				<cmd check="no">umount -l .{/dev,/proc,/sys,$CACHEDIR}</cmd>
				<cmd>rm -rf tmp/*</cmd>
				<cmd>sed -i -e 's%.* / .*%overlayfs / overlayfs defaults 0 0%' etc/fstab</cmd>
				<cmd>sed -i -e 's%^@BOOTDEV@%# &amp;%' etc/fstab</cmd>

				<cmd>sed -i -e 's/cachedir/&amp; chroot="yes"/' etc/upkg.conf</cmd>

				<cmd>sed -i -e 's@^options .*@options root=/dev/disk/by-label/PALDO-LIVE rootfstype=vfat@' boot/loader/entries/*.conf</cmd>

				<cmd>install -dv rootfs</cmd>
				<cmd>mv -v bin dev etc home lib* mnt proc root run sbin sys tmp usr var rootfs</cmd>
				<cmd>install -dv rootfs/{boot,upkg}</cmd>
				<cmd>touch rootfs/.readonly</cmd>
				<cmd>ln -svf /upkg/stage{1,2} rootfs</cmd>
				<cmd>mksquashfs rootfs boot/rootfs.img -no-progress</cmd>
				<cmd>rm -rf rootfs</cmd>

				<!-- hardcoded /var/cache/upkg as this is in paldo and we know that this path is correct in paldo -->
				<cmd>DISK_IMAGE=/var/cache/upkg/paldo.img</cmd>
				<cmd>rm -vf $DISK_IMAGE</cmd>

				<!-- number of 4 kB blocks needed for partition image, add 8 MB to account for filesystem overhead -->
				<cmd>BLOCKS=`du --apparent-size -s -B 4096 boot 2>/dev/null | sed -e 's/^\([0-9]\+\).*/\1/'`</cmd>
				<cmd>PARTSIZE=$(($BLOCKS / 256 + 8))</cmd>
				<cmd>DISKSIZE=$(($PARTSIZE + 2))</cmd>
				<cmd>dd if=/dev/zero of=$DISK_IMAGE bs=1048576 count=$DISKSIZE</cmd>

				<!-- write partition table -->
				<cmd>echo start=1MiB, size=${PARTSIZE}MiB, type=c12a7328-f81f-11d2-ba4b-00a0c93ec93b, 'name="ESP System Partition"' | sfdisk --label gpt $DISK_IMAGE</cmd>

				<!-- format partition image and copy content -->
				<cmd>mformat -i $DISK_IMAGE@@1M -F -v paldo-live -T${PARTSIZE}M ::</cmd>
				<cmd>mcopy -i $DISK_IMAGE@@1M -sQv boot/* ::</cmd>
			</postbuild>
		</release>
	</releases>
</package>
