#!/bin/bash
set -x
# Build a custom UEFI linux based PBA image
## define releases for tools
. conf
VERSIONINFO=`git describe --dirty` || VERSIONINFO=downloaded
BUILDTYPE=UEFI64
BUILDIMG=${BUILDTYPE}-${VERSIONINFO}.img

## check that everything is available
[  -f scratch/${SYSLINUX}/efi64/efi/syslinux.efi ] || { echo " prereqs are not available "; exit 1; }
[  -f scratch/${SYSLINUX}/efi64/com32/elflink/ldlinux/ldlinux.e64 ] || { echo " prereqs are not available "; exit 1; }
[  -f scratch/buildroot/64bit/images/bzImage ] || { echo " prereqs are not available "; exit 1; }
[  -f scratch/buildroot/64bit/images/rootfs.cpio.xz ] || { echo " prereqs are not available "; exit 1; }
[  -f scratch/buildroot/64bit/target/sbin/linuxpba  ] || { echo " prereqs are not available "; exit 1; }
[  -f scratch/buildroot/64bit/target/sbin/sedutil-cli  ] || { echo " prereqs are not available "; exit 1; }
[  -f buildroot/syslinux.cfg ] || { echo " prereqs are not available "; exit 1; }
  
# Start fresh
sudo rm -rf ${BUILDTYPE} ; mkdir ${BUILDTYPE} ; cd ${BUILDTYPE}
# make an image file
dd if=/dev/zero of=${BUILDIMG} bs=1M count=32
(echo "n";echo "";echo "";echo "";echo "ef00";echo w;echo Y) | gdisk ${BUILDIMG}
LOOPDEV=`sudo losetup --show -f -o 1048576 ${BUILDIMG}`
sudo mkfs.vfat $LOOPDEV -n ${BUILDTYPE}
sudo mkdir image
sudo mount $LOOPDEV image
sudo chmod 777 image
sudo mkdir -p image/EFI/boot
sudo cp ../scratch/${SYSLINUX}/efi64/efi/syslinux.efi image/EFI/boot/bootx64.efi
sudo cp ../scratch/${SYSLINUX}/efi64/com32/elflink/ldlinux/ldlinux.e64 image/EFI/boot/
sudo cp ../scratch/buildroot/64bit/images/bzImage image/EFI/boot/
sudo cp ../scratch/buildroot/64bit/images/rootfs.cpio.xz image/EFI/boot/
sudo cp ../buildroot/syslinux.cfg image/EFI/boot/
sudo umount image
sudo losetup -d $LOOPDEV
gzip ${BUILDIMG}