#!/bin/ash -efu

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

verbose "has started executing."

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

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

[ -f "$chroot/.image/syslinux/isolinux.cfg" ] ||
	fatal '/.image/syslinux/isolinux.cfg: not found.'

pkgs='mkisofs'
mki-install $pkgs ||
	fatal "faild to install packages: $pkgs."

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

if [ ! -f /.image/syslinux/isolinux.bin ]; then
	printf '%s\\n' "$PROG: /.image/syslinux/isolinux.bin: not found"
	exit 1
fi

cd /tmp
cat > .mkisofsrc <<-__EOF__
	APPI="${BOOT_APPI:-}"
	COPY="${BOOT_COPY:-}"
	ABST="${BOOT_ABST:-}"
	BIBL="${BOOT_BIBL:-}"
	PREP="${BOOT_PREP:-}"
	PUBL="${BOOT_PUBL:-}"
	SYSI="${BOOT_SYSI:-}"
	VOLI="${BOOT_VOLI:-}"
	VOLS="${BOOT_VOLS:-}"
__EOF__

rc=0
mkisofs $verbose -J -l -r \
	-b syslinux/isolinux.bin \
	-c syslinux/boot.cat \
	-no-emul-boot \
	-boot-info-table \
	-boot-load-size 4 \
	-o '/.our/$outname' /.image/ || rc=\$?

rm -f -- .mkisofsrc
exit \$rc
EOF

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