#!/bin/ash -efu

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

verbose "has started executing."

type="${MKI_PACKTYPE:?pack type required}"

outname="${MKI_OUTNAME:-outname}"
outname="${outname##*/}"

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

if [ -x "$type" ]; then
	message "$type: executable file."

	install -m755 -- "$type" "$chroot/tmp/script.sh"

	cat >"$chroot/tmp/packimage.sh"<<-EOF
	#!/bin/sh -efu
	WORKDIR=/.image
	OUTNAME='/.our/$outname'
	export WORKDIR OUTNAME
	/tmp/script.sh
	EOF
else
	fatal "$type: unknown pack type."
fi

rc=0
mki-exec "$chroot/tmp/packimage.sh" || rc=$?
rm -f -- "$chroot/tmp/packimage.sh" "$chroot/tmp/script.sh"
[ "$rc" -eq 0 ] ||
	exit $rc
mki-copy-our2out
