#!/bin/ash -efu
#
# mki-copy-tree
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

COPY_TREE="${COPY_TREE:?Data required}"

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

exit_handler() {
	local rc=$?
	trap - EXIT
	find "$chroot/.in/" -mindepth 1 -maxdepth 1 \
		-execdir rm -rf -- '{}' '+'
	exit $rc
}

trap exit_handler HUP INT QUIT TERM EXIT

for d in $COPY_TREE; do
	d="$(readlink -ve "$d")"

	get_copy_args "$d" "$chroot/.in"

	if [ -d "$d" ]; then
		find "$d" -mindepth 1 -maxdepth 1 \
			-execdir cp $cp_args -aft "$chroot/.in/" -- '{}' '+'
	else
		cp $cp_args -aft "$chroot/.in/" -- "$d"
	fi
done

mki-run /.host/find /.in -mindepth 1 -maxdepth 1 \
	-execdir cp $verbose -aft /.image/ -- '{}' '+'
