#!/bin/sh -efu
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2007-2023  Alexey Gladkov <gladkov.alexey@gmail.com>

# shellcheck source=tools/mki-sh-functions
. "${0%/*}"/mki-sh-functions
# shellcheck source=tools/mki-sh-image-prepare
. "${0%/*}"/mki-sh-image-prepare

verbose "has started executing."

[ -d "$subchroot" ] ||
	hsh_initroot_image "$subdir"

ln $verbose -nsf -- "$WORKDIRNAME/chroot" "$chroot/.image"

[ -f "$chroot/.fakedata" ] && [ -f "$chroot/.image/.fakedata" ] ||
	exit 0

inode1="$(stat -c '%i' "$chroot/.fakedata")"
inode2="$(stat -c '%i' "$chroot/.image/.fakedata")"
[ "$inode1" != "$inode2" ] ||
	exit 0

# NB: /.fakedata will get overwritten by the time hsh-run exits
mki-run sh -c 'rm -f /.fakedata. &&
	cat /.fakedata /.image/.fakedata > /.fakedata. &&
	ln -f /.fakedata. /.image/.fakedata'
mv -f -- "$chroot/.fakedata." "$chroot/.fakedata"
