#!/bin/ash -efu
#
# mki-pack-yaboot
#
# This file is part of mkimage
# Copyright (C) 2008  Sergey Bolshakov <sbolshakov@altlinux.org>
# Copyright (C) 2008-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_YABOOT:-}"

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/etc/yaboot.conf" ] ||
	fatal "/.image/$imgsubdir/etc/yaboot.conf: 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/ppc/chrp/yaboot ]; then
	printf '%s\\n' "$PROG: \$imgdir/ppc/chrp/yaboot: not found" >&2
	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__

cat > hfs.map <<-__EOF__
	# ext.  xlate  creator  type    comment
	.rpm	Raw	'ALTL'	'bina'	"RPM package"
	.conf	Raw	'UNIX'	'conf'	"bootstrap"
	.cz	Raw	'UNIX'	'boot'	"bootstrap"
	.b	Raw	'UNIX'	'tbxi'	"bootstrap"
	yaboot	Raw	'UNIX'	'boot'	"bootstrap"
	vmlinuz	Raw	'UNIX'	'boot'	"bootstrap"
	*	Ascii	'????'	'????'	"Text file"
__EOF__

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

rc=0
mkisofs $verbose -J -l -r -hfs -part -probe \
	-map hfs.map \
	-exclude-list .exclude \
	-hfs-bless \$imgdir/ppc \
	-hfs-volid "${BOOT_VOLI:-}" \
	-prep-boot ppc/chrp/yaboot \
	-chrp-boot \
	-iso-level 2 \
	"\$imgdir/" || rc=\$?

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