#!/bin/ash -efu

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

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

verbose "has started executing."

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

pkgs='yaboot kernel-bootwrapper binutils'
mki-install $pkgs ||
	fatal "failed to install packages: $pkgs."

[ -f "$chroot/.image/$imgsubdir/etc/yaboot.conf" ] ||
	message "Warning: /.image/$imgsubdir/etc/yaboot.conf: not found."

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

mkdir $verbose -p -- \
	/.image/$imgsubdir/etc \
	/.image/$imgsubdir/ppc/chrp \
	/.image/$imgsubdir/ps3/otheros

cd /.image/$imgsubdir/

initrd=
[ ! -f /boot/full.cz ] ||
	initrd=/boot/full.cz

cp $verbose -aLf \
	/boot/vmlinuz /usr/lib/yaboot/yaboot \$initrd \
	./ppc/chrp/

wrapper -p ps3 \
	\${initrd:+-i \$initrd} \
	-o ./ps3/otheros/otheros.bld \
	/boot/vmlinuz

rm -f -- ./ps3/otheros/otheros.bld.bin
EOF

mki_exec_once "$chroot/.host/packimage.sh"
