#!/bin/ash -efu
#
# mki-pack-isoboot
#
# 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."

pkgs="${PACKAGES_REQUIRED_PACK_ISOBOOT:-}"

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

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

[ "$MKI_OUTNAME" = "${MKI_OUTNAME##*/}" ] ||
	outdir="$(readlink -ev "${MKI_OUTNAME%/*}")"

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

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

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

run_chrooted "$chroot"  >"$outdir/$outname" <<EOF
#!/bin/sh -efu

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

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

if [ -n "${BOOT_LANG-}" ]; then
	mkdir -p -- \$imgdir/syslinux
	printf '%s\\n' "${BOOT_LANG-}" > \$imgdir/syslinux/lang
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 \
	"\$imgdir/" || rc=\$?

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