#!/bin/ash -efu
#
# mki-pack-isodata
#
# 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_ISODATA:-}"

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."

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

run_chrooted "$chroot" >"$outdir/$outname" <<EOF
#!/bin/sh -efu
cd /tmp
cat > .mkisofsrc <<-__EOF__
	APPI="${ISODATA_APPI:-}"
	COPY="${ISODATA_COPY:-}"
	ABST="${ISODATA_ABST:-}"
	BIBL="${ISODATA_BIBL:-}"
	PREP="${ISODATA_PREP:-}"
	PUBL="${ISODATA_PUBL:-}"
	SYSI="${ISODATA_SYSI:-}"
	VOLI="${ISODATA_VOLI:-}"
	VOLS="${ISODATA_VOLS:-}"
__EOF__

if [ -n "${MKI_EXCLUDE:-}" ]; then
	for d in ${MKI_EXCLUDE:-}; do
		printf '%s\\n' "\$d"
	done
fi > .exclude

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

rc=0
mkisofs -T -J -l -r -U $verbose \
	-exclude-list /tmp/.exclude \
	"\$imgdir/" || rc=\$?

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