#!/bin/bash -x
# TODO:
# - pijul diff --short | wc -l inside loop, must be always 0.


# mount -t tmpfs -o size=3g tmpfs /mnt/linux2x
R="/mnt/linux2x/repo/"
rm -rf ${R:?}

pijul init "$R"
tar -xJf kernel/linux-2.0.1.tar.xz -C "$R" --strip-components=1
(cd "$R" && pijul add -r * && pijul rec -am".")

for i in {2..4}
do
	xzcat patches/patch-2.0."$i".xz | patch -p1 -sd "$R"
	(cd "$R" && pijul add -r * && pijul rec -am".")
done

# record to branch, apply 2.0.5 back to main
(cd "$R" && pijul fork 205 && pijul channel switch 205)
xzcat patches/patch-2.0.5.xz | patch -p1 -sd "$R"
(cd "$R" && pijul add -r * && pijul rec -am".")
H=$(cd "$R" && pijul log --hash-only | head -1)
(cd "$R" ; pijul apply --channel main "$H" ; pijul channel switch main ; pijul channel delete 205)

for i in {6..9}
do
	xzcat patches/patch-2.0."$i".xz | patch -p1 -sd "$R"
	(cd "$R" && pijul add -r * && pijul rec -am".")
done