#!/bin/bash
set -x
# Build a custom bootable image for BIOS machines
## define releases for tools
. conf
VERSIONINFO=`git describe --dirty`
BUILDTYPE=$1
BUILDIMG=LINUXPBA${1}-${VERSIONINFO}.img
if [ "$BUILDTYPE" == "Debug" ] || [ "$BUILDTYPE" == "Release" ] ; then
            echo "Building " $BUILDTYPE "image"
	else
	    echo "You must select Debug|Release"
	    exit		
fi

# Start fresh
sudo rm -rf LINUXPBA${BUILDTYPE} ; mkdir LINUXPBA${BUILDTYPE} ; cd LINUXPBA${BUILDTYPE}
## check that everything is available
[ -f ../scratch/${SYSLINUX}/bios/mbr/mbr.bin -a \
     -x ../scratch/${SYSLINUX}/bios/extlinux/extlinux -a \
     -f ../../LinuxPBA/dist/${BUILDTYPE}/GNU-Linux/linuxpba -a \
     -f ../../linux/CLI/dist/${BUILDTYPE}_i686/GNU-Linux/sedutil-cli -a \
     -f ../scratch/buildroot/PBA32/images/bzImage  \
  ] || { echo " prereqs are not available "; exit 1; }
#
# recreate the initrd file with the latest PBA
mkdir  ../scratch/buildroot/PBA32/overlay/sbin
cp ../../LinuxPBA/dist/${BUILDTYPE}/GNU-Linux/linuxpba  ../scratch/buildroot/PBA32/overlay/sbin/linuxpba
cp ../../linux/CLI/dist/${BUILDTYPE}_i686/GNU-Linux/sedutil-cli  ../scratch/buildroot/PBA32/overlay/sbin/sedutil-cli
cd ../scratch/buildroot
make all O=PBA32 2>&1 >PBA32.log
cd ../../LINUXPBA${BUILDTYPE}
dd if=/dev/zero of=${BUILDIMG} bs=1M count=8
(echo o;echo n;echo p;echo 1;echo"";echo "";echo "";echo a;echo 1;echo w) | fdisk ${BUILDIMG}
dd if=../scratch/${SYSLINUX}/bios/mbr/mbr.bin of=${BUILDIMG} count=1 conv=notrunc bs=512
LOOPDEV=`sudo losetup --show -f -o 1048576 ${BUILDIMG}`
sudo mkfs.ext4 $LOOPDEV -L ${BUILDTYPE}-${VERSIONINFO}
mkdir image
sudo mount $LOOPDEV image
sudo chmod 777 image
sudo mkdir -p image/boot/extlinux
sudo ../scratch/${SYSLINUX}/bios/extlinux/extlinux --install image/boot/extlinux
sudo cp ../scratch/buildroot/PBA32/images/bzImage  image/boot/extlinux/
sudo cp ../scratch/buildroot/PBA32/images/rootfs.cpio.xz image/boot/extlinux/
sudo cp ../buildroot/syslinux.cfg image/boot/extlinux/extlinux.conf
sudo umount image
sudo losetup -d $LOOPDEV
gzip ${BUILDIMG}
