#!/bin/sh -e

[ -n "$spt_dir" ] || spt_dir=/usr/share/spt3
. $spt_dir/init

# bootsplash
if [ -d "$chroot/etc/bootsplash/themes/current/config" -a -x "$chroot/sbin/splash" ]; then
	chroot_install cpio
	Verbose "Installing bootsplash"
	cat >"$tmp_dir/script" <<\__EOF__
#!/bin/sh -e
cd /etc/bootsplash/themes/current/config
mkdir -p /.splash /.isolinux
for n in bootsplash-*.cfg; do
	name="$(basename $n)"
	X="$(echo $name | sed -e 's/.*-\(.*\)x\(.*\)\.cfg/\1/' | xargs printf '%04d')"
	Y="$(echo $name | sed -e 's/.*-\(.*\)x\(.*\)\.cfg/\2/' | xargs printf '%04d')"
	filename="$X$Y.spl"
	tmpspl="$(mktemp -d /.splash/spl.XXXXXXXXXX)"
	/sbin/splash -f -s $n >$tmpspl/bootsplash
	echo "file /bootsplash $tmpspl/bootsplash 0777 0 0" |
		/usr/bin/gencpio - >/.isolinux/$filename
	rm -rf $tmpspl
done
rm -Rf /.splash
__EOF__
	chroot_exec "$tmp_dir/script" \
		&& Verbose 'Success: bootsplash' \
		|| Fatal 'Failed: bootsplash'
fi

# move .isolinux prepare inside chroot to the outside
if [ -d "$chroot/.isolinux" ]; then
	cp -a $chroot/.isolinux/* $out_dir/isolinux/
	chroot_run rm -Rf /.isolinux
	Verbose "Moved prepared isolinux to output directory"
fi
