#!/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

MKI_OUTNAME="${MKI_OUTNAME:-}"
cleanup="${CLEANUP_OUTDIR:-}"
subprofile="${SUBPROFILE:-0}"

verbose "has started executing."

if [ "$MKI_OUTNAME" != "${MKI_OUTNAME##*/}" ]; then
	outdir="$(readlink -ev "${MKI_OUTNAME%/*}")"
	cleanup=
fi

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

get_copy_args "$outdir" "$chroot/.our"

[ -z "$cleanup" ] || [ "$subprofile" = 0 ] ||
	find "$outdir/" -mindepth 1 -maxdepth 1 -execdir rm ${verbose-} -rf -- '{}' '+'

cd "$chroot/.our"
find . -mindepth 1 -maxdepth 1 \
	-exec cp ${cp_args-} ${verbose-} -aft "$outdir" -- '{}' '+'

mki-run /.host/find /.our -mindepth 1 -maxdepth 1 -execdir rm -rf -- '{}' '+'
