#!/bin/sh

ddq()
{
	dd "$@" 2> /dev/null
}

read16()
{
	od -v -j $2 -N 2 -t u2 -An $1
}

read32()
{
	od -v -j $2 -N 4 -t u4 -An $1
}

write64()
{
	local a b c i n=$2
	for i in 1 2 3 4 ; do
		c="$(printf '/x%02X' $(($n&255)))"
		n=$(($n/256))
		a="$a$c"
		b="$c$b"
	done
	echo "$a$b" | sed 's|/|\\\\|g' | xargs  echo -en | \
		ddq conv=notrunc of=$1 seek=$3 bs=1
}

entry()
{
	local d n i iso=$1
	shift
	set -- $(echo $1 | sed 'y|abcdefghijklmnopqrstuvwxyz/|ABCDEFGHIJKLMNOPQRSTUVWXYZ |')
	d=$(read32 $iso $((2048*16+0x9E)))
	i=0
	while true; do
		n=$(read16 $iso $((2048*$d+$i)))
		[ $n -eq 0 ] && echo "Path not found." 1>&2 && exit
		e="$(ddq if=$iso bs=1 skip=$((2048*d+$i+33)) count=${#1})"
		if [ "$e" == "$1" ]; then
			shift
			[ -n "$1" ] || { echo $((2048*$d+$i)); return; }
			d=$(read32 $iso $((2048*$d+$i+2)))
			i=0
		else
			i=$(($i+$n))
		fi
	done
}

grub_conf()
{
	cat <<EOT
search --no-floppy --set=root --file /boot/isolinux/c32box.c32
menuentry 'SliTaz' {
	linux	/boot/bzImage rw root=/dev/null video=-32 autologin
	initrd	/boot/rootfs4.gz /boot/rootfs3.gz  /boot/rootfs2.gz  /boot/rootfs1.gz
}

menuentry 'Web boot' {
	insmod /boot/grub/i386-efi/linux16.mod
	linux16	/boot/ipxe
}

menuentry 'MemTest86+' {
	insmod /boot/grub/i386-efi/linux16.mod
	linux16	/boot/memtest
}

menuentry 'Search isolinux' {
	insmod /boot/grub/i386-efi/nativedisk.mod
	nativedisk
	search --no-floppy --set=root --file /boot/isolinux/c32box.c32
	chainloader +1
}

EOT
}

set -- ${1:-slitaz-rolling.iso}
[ -s "$1" ] || wget -O "$1" http://mirror1.slitaz.org/iso/rolling/$(basename $1)
shrink=true
TMP=/tmp/$(basename $0).$$
mkdir -p $TMP
mount -o loop,ro $1 /media/cdrom || exit
cp -a /media/cdrom $TMP/iso
cmp $TMP/iso/boot/bzImage $TMP/iso/boot/vmlinuz* &&
rm -f $TMP/iso/boot/bzImage && ln $TMP/iso/boot/vmlinuz* $TMP/iso/boot/bzImage
umount -d /media/cdrom
if [ ! -s "$2" ]; then
	set -- $1 /boot/efi/boot/bootia32.efi
	[ -s "$2" ] || tazpkg get-install grub2-efi
fi
fat=$TMP/iso/boot/isolinux/efi.img
ddq if=/dev/zero bs=1k count=720 of=$fat
[ -s /sbin/mkfs.fat ] || tazpkg get-install dosfstools
mkfs.fat -F 12 -s 4 -f 1 -r 16 $fat
mount -o loop,rw $fat /media/flash
mkdir -p /media/flash/efi/boot
cp $2 /media/flash/efi/boot
mkdir -p /media/flash/boot/grub
grub_conf > /media/flash/boot//grub/grub.cfg
umount -d /media/flash
mkdir -p $TMP/iso/efi/boot $TMP/iso/boot/grub/i386-efi
for i in nativedisk pata ata ahci uhci ehci ohci usb usbms linux16 ; do
	cp /boot/grub/i386-efi/$i.mod $TMP/iso/boot/grub/i386-efi
done
if $shrink; then
false &&
	ddq of=$fat bs=16 count=0 \
		seek=$((0x$(hd $fat | tail -n 3 | sed 's/0  0.*//;q')))
	touch $TMP/iso/efi/boot/$(basename $2)
else
	cp $2 $TMP/iso/efi/boot/
fi
grub_conf > $TMP/iso/boot/grub/grub.cfg
genisoimage -R  -o efi.iso  -hide-rr-moved \
	-b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat \
	-no-emul-boot   -boot-load-size 4   -boot-info-table \
	-eltorito-alt-boot -efi-boot boot/isolinux/efi.img -no-emul-boot \
	-V "${VOLUM_NAME:-SliTaz}"   -p "${PREPARED:-$(id -un)}" \
	-volset "SliTaz $SLITAZ_VERSION" -input-charset utf-8 \
	-A "tazlito $VERSION/$(genisoimage --version)" \
	-copyright README   -P "www.slitaz.org" $TMP/iso
if $shrink; then
	e=$(entry efi.iso boot/isolinux/efi.img | tee -a /tmp/log)
	write64 efi.iso $((720*1024)) $(($e+10))
	n=$(($(read32 efi.iso $(($e+2)))+4))
	e=$(entry efi.iso efi/boot/$(basename $2) | tee -a /tmp/log)
	write64 efi.iso $n $(($e+2))
	write64 efi.iso $(stat -c %s $2) $(($e+10))
fi
iso2exe efi.iso
echo rm -rf $TMP
[ -s /usr/share/qemu/ia32/OVMF.fd ] &&
echo qemu -k fr -bios /usr/share/qemu/ia32/OVMF.fd -hda efi.iso
