#!/bin/sh
# $1: create efiboot-`date`tar.xz

#grub2-efi
#=========
#  https://packages.debian.org/sid/grub-efi-ia32-signed
#  https://packages.debian.org/sid/grub-efi-amd64-signed
#  MOD: https://packages.debian.org/sid/i386/grub-efi-ia32-bin/download
#  MOD: https://packages.debian.org/sid/amd64/grub-efi-amd64-bin/download
#
#  https://packages.debian.org/sid/shim-signed
#
#grub4dos
#========
#  https://github.com/chenall/grub4dos/releases
#	current: grub4dos-0.4.6a-2019-10-28

IA32_URL='http://ftp.us.debian.org/debian/pool/main/g/grub-efi-ia32-signed/grub-efi-ia32-signed_1+2.04+4_i386.deb'
X64_URL='http://ftp.us.debian.org/debian/pool/main/g/grub-efi-amd64-signed/grub-efi-amd64-signed_1+2.04+4_amd64.deb'
MOD_URL='http://ftp.us.debian.org/debian/pool/main/g/grub2/grub-efi-ia32-bin_2.04-4_i386.deb
http://ftp.us.debian.org/debian/pool/main/g/grub2/grub-efi-amd64-bin_2.04-4_amd64.deb'

SHIM32_URL='http://ftp.us.debian.org/debian/pool/main/s/shim-signed/shim-signed_1.33+15+1533136590.3beb971-7_i386.deb'
SHIM64_URL='http://ftp.us.debian.org/debian/pool/main/s/shim-signed/shim-signed_1.33+15+1533136590.3beb971-7_amd64.deb'

mkdir -p tmp
cd tmp || exit
for i in ${IA32_URL} ${X64_URL} ${SHIM32_URL} ${SHIM64_URL} ${MOD_URL}
do
	if [ ! -f ${i##*/} ] ; then
		wget $i || exit 1
	fi
done

mkdir -p deb
for i in ${IA32_URL} ${X64_URL} ${SHIM32_URL} ${SHIM64_URL} ${MOD_URL}
do
	echo "dpkg-deb -x ${i##*/} deb"
	dpkg-deb -x ${i##*/} deb
done

rm -rf grub
mkdir -p grub
cp -af deb/usr/lib/grub/x86_64-efi grub/
rm -rf grub/x86_64-efi/monolithic

cp -af deb/usr/lib/grub/i386-efi grub/
rm -rf grub/i386-efi/monolithic

#if [ -f deb/usr/lib/grub/i386-efi-signed/grubia32.efi.signed ] ; then
#	GRUBIA32_SOURCE='deb/usr/lib/grub/i386-efi-signed/grubia32.efi.signed'
if [ -f deb/usr/lib/grub/i386-efi-signed/gcdia32.efi.signed ] ; then
	GRUBIA32_SOURCE='deb/usr/lib/grub/i386-efi-signed/gcdia32.efi.signed'
elif [ -f deb/usr/lib/grub/i386-efi/monolithic/gcdia32.efi ] ; then
	GRUBIA32_SOURCE='deb/usr/lib/grub/i386-efi/monolithic/gcdia32.efi'
elif [ -f deb/usr/lib/grub/i386-efi/monolithic/grubia32.efi ] ; then
	# grubia32.efi requires /EFI/debian/grub.cfg
	GRUBIA32_SOURCE='deb/usr/lib/grub/i386-efi/monolithic/grubia32.efi'
fi

#if [ -f deb/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed ] ; then
#	GRUBX64_SOURCE='deb/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed'
if [ -f deb/usr/lib/grub/x86_64-efi-signed/gcdx64.efi.signed ] ; then
	GRUBX64_SOURCE='deb/usr/lib/grub/x86_64-efi-signed/gcdx64.efi.signed'
elif [ -f deb/usr/lib/grub/x86_64-efi/monolithic/gcdx64.efi ] ; then
	GRUBX64_SOURCE='deb/usr/lib/grub/x86_64-efi/monolithic/gcdx64.efi'
elif [ -f deb/usr/lib/grub/x86_64-efi/monolithic/grubx64.efi ] ; then
	# grubx64.efi requires /EFI/debian/grub.cfg
	GRUBX64_SOURCE='deb/usr/lib/grub/x86_64-efi/monolithic/grubx64.efi'
else
	echo "Need grubx64.efi"
	exit
fi

if [ -f deb/usr/lib/shim/shimia32.efi.signed ] ; then
	BOOTIA32_SOURCE='deb/usr/lib/shim/shimia32.efi.signed'
else
	BOOTIA32_SOURCE=${GRUBIA32_SOURCE}
	GRUBIA32_SOURCE=''
fi

if [ -f deb/usr/lib/shim/shimx64.efi.signed ] ; then
	BOOTX64_SOURCE='deb/usr/lib/shim/shimx64.efi.signed'
else
	BOOTX64_SOURCE=${GRUBX64_SOURCE}
	GRUBX64_SOURCE=''
fi

X="$GRUBIA32_SOURCE $GRUBX64_SOURCE"
case $X in
	*/gcd*) GRUBCFG='boot/grub/grub.cfg' ;;
	*/grubia32*|*/grubx64*) GRUBCFG='EFI/debian/grub.cfg' ;;
esac

#=======================================================

#if [ "$1" ] ; then #efiboot-`date`-tar.xz
	pkg="efiboot-`date "+%Y%m%d"`"
	rm -rf ${pkg}
	mkdir -p ${pkg}

	mkdir -p ${pkg}/EFI/boot
	cp -fv $BOOTX64_SOURCE  ${pkg}/EFI/boot/bootx64.efi
	
	if [ -f "$GRUBIA32_SOURCE" ] ; then
		cp -fv $GRUBIA32_SOURCE ${pkg}/EFI/boot/grubia32.efi
	fi
	if [ -f "$GRUBX64_SOURCE" ] ; then
		cp -fv $GRUBX64_SOURCE  ${pkg}/EFI/boot/grubx64.efi
	fi
	if [ -f "$BOOTIA32_SOURCE" ] ; then
		cp -fv $BOOTIA32_SOURCE ${pkg}/EFI/boot/bootia32.efi
	fi

	#mkdir -p ${pkg}/$(dirname $GRUBCFG)
	#cp ../grub/grub.cfg ${pkg}/$(dirname $GRUBCFG)

	mkdir ${pkg}/boot
	rm -rf ${pkg}/boot/grub
	cp -af grub ${pkg}/boot/
	cp -f ../*.jpg ../*.png ${pkg}/boot/
	cp -f ../grub/*.cfg ${pkg}/boot/grub/

	cd $pkg
	tar -Jcf ../${pkg}.tar.xz *
	cd ..
	md5sum ${pkg}.tar.xz > ${pkg}.tar.xz.md5.txt

	echo
	echo ${pkg}.tar.xz
	echo
#fi

#=======================================================

# make a grub2 efi image
mk_efi_img() {
	#output_img=${1}/boot/efi.img
	output_img=efi.img
	mkdir -p /tmp/efi_img # mount point
	echo "making $output_img"
	size=0
	x=$(stat -c %s "$BOOTX64_SOURCE")
	size=$((size + x))
	if [ -f "$BOOTIA32_SOURCE" ] ; then
		x=$(stat -c %s "$BOOTIA32_SOURCE")
		size=$((size + x))
	fi
	if [ -f "$GRUBIA32_SOURCE" ] ; then
		x=$(stat -c %s "$GRUBIA32_SOURCE")
		size=$((size + x))
	fi
	if [ -f "$GRUBX64_SOURCE" ] ; then
		x=$(stat -c %s "$GRUBX64_SOURCE")
		size=$((size + x))
	fi
	size=$((size + 524288)) # add 512k
	size=$((size / 512))
	dd if=/dev/zero of=${output_img} bs=512 count=${size} || return 1
	echo "formatting ${output_img} - vfat"
	mkdosfs ${output_img}
	FREE_DEV=`losetup -f`
	echo "mounting ${output_img} on /tmp/efi_img"
	losetup $FREE_DEV ${output_img} || return 2
	mount -t vfat $FREE_DEV /tmp/efi_img || \
		( losetup -d $FREE_DEV ; return 3 )
	sync
	echo "copying files"
	mkdir -p /tmp/efi_img/EFI/boot/ || return 4
	cp -fv "$BOOTX64_SOURCE" /tmp/efi_img/EFI/boot/bootx64.efi
	if [ -f "$BOOTIA32_SOURCE" ] ; then
		cp -fv "$BOOTIA32_SOURCE" /tmp/efi_img/EFI/boot/bootia32.efi
	fi
	if [ -f "$GRUBIA32_SOURCE" ] ; then
		cp -fv "$GRUBIA32_SOURCE" /tmp/efi_img/EFI/boot/grubia32.efi
	fi
	if [ -f "$GRUBX64_SOURCE" ] ; then
		cp -fv "$GRUBX64_SOURCE" /tmp/efi_img/EFI/boot/grubx64.efi
	fi
	sync
	echo "unmounting /tmp/efi_img"
	umount /tmp/efi_img || return 7
	losetup -d $FREE_DEV 2>/dev/null #precaution
	rm -r /tmp/efi_img
	return 0
}

mk_efi_img
if [ $? -eq 0 ] ; then
	echo - OK -
	echo
	#cp -afv efi.img ../
	#echo 'efi.img updated !!'
	echo tmp/efi.img is ready
	echo tmp/grub is ready
	echo
	exit
fi

### END ###