#!/bin/ash -efu

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

nproc="${PACK_SQUASHFS_PROCESSORS:-}"
nproc="$(opt_check_number 'PACK_SQUASHFS_PROCESSORS' "$nproc")"

opts="${PACK_SQUASHFS_OPTS:-}"

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

imgsubdir="${MKI_IMAGESUBDIR:-}"
imgsubdir="${imgsubdir#/}"

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

mki-install squashfsprogs

cat >"$chroot/.host/packimage.sh"<<EOF
#!/bin/sh -efu

imgdir=/.image
[ -z "$imgsubdir" -o ! -d "/.image/$imgsubdir" ] ||
	imgdir="/.image/$imgsubdir"

[ ! -e '/.our/$outname' ] ||
	rm -f -- '/.our/$outname'

mksquashfs "\$imgdir/" '/.our/$outname' $opts ${nproc:+-processors $nproc} -e .in .host .fakedata
EOF

mki_exec_once "$chroot/.host/packimage.sh"
mki-copy-our2out
