#!/bin/bash
set -x
## define releases for tools
. conf
VERSIONINFO=`git describe --dirty`  || VERSIONINFO=tarball
BUILDTYPE=RESCUE32
ROOTDIR=32bit

if [ x${1} == "xRescue32" ] ; then
	BUILDTYPE="RESCUE32"
	ROOTDIR="32bit"
elif [ x${1} == "xRescue64" ] ; then
	BUILDTYPE="RESCUE64"
	ROOTDIR="64bit"
else 
	echo "NO BUILDTYPE specified defalted to Rescue32"
fi
BUILDIMG=${BUILDTYPE}-${VERSIONINFO}.img
echo "Building " $BUILDTYPE "image"
[ -f scratch/${SYSLINUX}/bios/mbr/mbr.bin ] || { echo " prereqs are not available "; exit 1; }
[ -x scratch/${SYSLINUX}/bios/extlinux/extlinux ] || { echo " prereqs are not available "; exit 1; }
[ -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/${ROOTDIR}/images/bzImage ] || { echo " prereqs are not available "; exit 1; }
[ -f scratch/buildroot/${ROOTDIR}/images/rootfs.cpio.xz  ] || { echo " prereqs are not available "; exit 1; }
[ -x scratch/buildroot/${ROOTDIR}/target/sbin/linuxpba  ] || { echo " prereqs are not available "; exit 1; }
[ -x scratch/buildroot/${ROOTDIR}/target/sbin/sedutil-cli  ] || { echo " prereqs are not available "; exit 1; }
[ -f buildroot/syslinux.cfg ] || { echo " prereqs are not available "; exit 1; }
[ -f UEFI64/UEFI64-*.img.gz ] || { echo " prereqs are not available "; exit 1; }
[ -f BIOS32/BIOS32-*.img.gz ] || { echo " prereqs are not available "; exit 1; }
#
# Remaster the root file system
sudo rm -rf scratch/rescuefs
sudo rm -f scratch/buildroot/${ROOTDIR}/images/rescuefs.cpio.xz 
mkdir scratch/rescuefs
cd scratch/rescuefs
xz --decompress --stdout ../buildroot/${ROOTDIR}/images/rootfs.cpio.xz | sudo cpio -i -H newc -d
#weird permission denied on etc/issue 
echo "* ***********************************" > /tmp/issue
echo "* DTA sedutil rescue image ${BUILDIMG}" >> /tmp/issue
echo "*" >> /tmp/issue
echo "* Login as root, there is no password" >> /tmp/issue
echo "*" >> /tmp/issue
echo "* ***********************************" >> /tmp/issue
sudo mv /tmp/issue etc/issue
sudo rm etc/init.d/S99*
sudo mkdir -p usr/sedutil
sudo cp ../../UEFI64/UEFI64-*.img.gz usr/sedutil/
sudo cp ../../BIOS32/BIOS32-*.img.gz usr/sedutil/
sudo find . | sudo cpio -o -H newc | xz -9 -C crc32 -c > ../buildroot/${ROOTDIR}/images/rescuefs.cpio.xz
cd ../..
#
sudo rm -rf ${BUILDTYPE} ; mkdir ${BUILDTYPE} ; cd ${BUILDTYPE}
dd if=/dev/zero of=${BUILDIMG} bs=1M count=128
if [ ${BUILDTYPE} == "RESCUE32" ] ; then
	(echo o;echo n;echo p;echo 1;echo "";echo "";echo a;echo 1;echo w) | fdisk -C 100 ${BUILDIMG}
	dd if=../scratch/${SYSLINUX}/bios/mbr/mbr.bin of=${BUILDIMG} count=1 conv=notrunc bs=512
else
	(echo "n";echo "";echo "";echo "";echo "ef00";echo w;echo Y) | gdisk ${BUILDIMG}
fi
LOOPDEV=`sudo losetup --show -f -o 1048576 ${BUILDIMG}`
if [ ${BUILDTYPE} == "RESCUE32" ] ; then
	sudo mkfs.ext4 $LOOPDEV -L ${BUILDTYPE}
else
	sudo mkfs.vfat $LOOPDEV -n ${BUILDTYPE}
fi
mkdir image
sudo mount $LOOPDEV image
sudo chmod 777 image
if [ ${BUILDTYPE} == "RESCUE32" ] ; then
	sudo mkdir -p image/boot/extlinux
	sudo ../scratch/${SYSLINUX}/bios/extlinux/extlinux --install image/boot/extlinux
	sudo cp ../scratch/buildroot/32bit/images/bzImage  image/boot/extlinux/
	sudo cp ../scratch/buildroot/32bit/images/rescuefs.cpio.xz image/boot/extlinux/rootfs.cpio.xz
	sudo cp ../buildroot/syslinux.cfg image/boot/extlinux/extlinux.conf
else
	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/rescuefs.cpio.xz image/EFI/boot/rootfs.cpio.xz
	sudo cp ../buildroot/syslinux.cfg image/EFI/boot/
fi	
sudo umount image
sudo losetup -d $LOOPDEV
gzip ${BUILDIMG}
