#!/bin/bash
#
# gita = git address for the bootstrap seeds
# gitu = UID for this version
# gits = source for wanted architecture
#
gita="https://github.com/oriansj/bootstrap-seeds/blob/"
gitu="4715ae5292a6551a7d6e3ba2a2f1586a6bc5cb7d"
gits="/POSIX/AMD64/hex0_AMD64.hex0?raw=true"

step1="hex0-x86"
step2="builder-hex0-x86-stage1"
step3="builder-hex0-x86-stage2"

# Place a # at the beginning of the wget line after the first run
# rm -f so it doesn't complain if the build subdirectory doesn't exist
#
echo " 1a. Download $step1.hex0 from git repository"
wget -O $step1.hex0 -q $gita$gitu$gits
rm -rf build/
mkdir build
cd build
cp -a ../$step1.hex0 ../$step2.hex0 ../$step3.hex0 .

echo " 1b. Use sed and xxd to create $step1.seed from $step1.hex0"
sed -re 's/^([^#;]*).*/\1/' $step1.hex0 | xxd -r -p > $step1.seed
chmod +x $step1.seed

echo " 1c. Use $step1.seed to create $step1.tmp from $step1.hex0" 
./$step1.seed $step1.hex0 $step1.tmp
chmod +x $step1.tmp

echo " 1d. Use $step1.tmp to create $step1 from $step1.hex0" 
./$step1.tmp $step1.hex0 $step1
chmod +x $step1

# uniq -c reduces the three checksums to one line if they are all identical
#
echo " 1e. Check if $step1.seed, $step1.tmp and $step1 are identical:"
wcl=$(md5sum $step1{,.seed,.tmp} | sed -re 's/^([^ ]*) .*$/\1/' | uniq -c | wc -l)
if [ "$wcl" -ne 1 ]; then
  echo "       => Something has gone wrong! Please check every step manually"
  exit 1
fi
echo "       => Yes!"

echo
echo
echo " 2a. Use $step1 to create $step2.tmp from $step2.hex0"
./$step1 $step2.hex0 $step2.tmp

# Create _HK version to see if the source is really build!
#
step2b="builder-hex0-x86-stage1_HK"
sed -re 's/^00 00 00 00 00 00 00 00$/00 00 00 00 00 00 48 4B/' builder-hex0-x86-stage1.hex0 > $step2b.hex0
echo " 2b. Use $step1 to create $step2b.tmp from $step2b.hex0"
./$step1 $step2b.hex0 $step2b.tmp

echo " 2c. Create a $step2.src script from from $step2.hex0"
filesize=$(stat -c %s $step2.hex0)
( echo "src 0 /dev"
  echo "src $filesize ./$step2.hex0"
  cat $step2.hex0
  echo "hex0 ./$step2.hex0 /dev/hda" ) > $step2.src

# We need minimal 26k extra empty space after the source (why?)
# Use status=none in dd to surpress summary on stdout
#
echo " 2d. Create a virtual harddisk with $step2b.tmp, $step3.hex0 and $step2.src"
filesize=$(stat -c %s $step3.hex0)
fillsize=$(( 512 - $filesize % 512 ))
( cat $step2b.tmp
  cat $step3.hex0
  dd if=/dev/zero bs=1 count=$fillsize status=none ) > $step2.img
filesize=$(stat -c %s $step2.src)
fillsize=$(( 512 - $filesize % 512 ))
( cat $step2.src
  dd if=/dev/zero bs=1 count=$fillsize status=none
  dd if=/dev/zero bs=26k count=1 status=none ) >> $step2.img

echo " 2e. Start virtual harddisk $step2.img with QEMU/KVM"
qemu-system-x86_64 \
  -m 2G \
  -nographic \
  -drive file=$step2.img,format=raw \
  --no-reboot > qemu_step2.log
hex=$(tail -1 qemu_step2.log)
bytes=$(printf "%d" $((16#${hex:0:8})))
if [ "$bytes" -ne 512 ]; then
  echo "       => Something has gone wrong! Please check every step manually"
  exit 1
fi

# The diff exit code is 0 if both files are identical
#
echo " 2f. Extract $step2.mbr and check if it is identical with $step2.tmp"
dd if=$step2.img of=$step2.mbr bs=$bytes count=1 status=none
diff $step2.tmp $step2.mbr
if [ "$?" -ne 0 ]; then
  echo "       => Something has gone wrong! Please check every step manually"
  exit 1
fi
echo "       => Yes!"

echo
echo
echo " 3a. Use $step1 to create $step3.tmp from $step3.hex0"
./$step1 $step3.hex0 $step3.tmp

echo " 3b. Create a $step3.src script from from $step3.hex0"
filesize=$(stat -c %s $step3.hex0)
( echo "src 0 /dev"
  echo "src $filesize ./$step3.hex0"
  cat $step3.hex0
  echo "hex0 ./$step3.hex0 /dev/hda" ) > $step3.src

# We need minimal 9k extra empty space after the source (why?)
#
echo " 3c. Create a virtual harddisk with $step2.mbr, $step3.hex0 and $step3.src"
filesize=$(stat -c %s $step3.hex0)
fillsize=$(( 512 - $filesize % 512 ))
( cat $step2.mbr
  cat $step3.hex0
  dd if=/dev/zero bs=1 count=$fillsize status=none ) > $step3.img
filesize=$(stat -c %s $step3.src)
fillsize=$(( 512 - $filesize % 512 ))
( cat $step3.src
  dd if=/dev/zero bs=1 count=$fillsize status=none
  dd if=/dev/zero bs=9k count=1 status=none ) >> $step3.img

echo " 3d. Start virtual harddisk $step3.img with QEMU/KVM"
qemu-system-x86_64 \
  -m 2G \
  -nographic \
  -drive file=$step3.img,format=raw \
  --no-reboot > qemu_step3.log
hex=$(tail -1 qemu_step3.log)
bytes=$(printf "%d" $((16#${hex:0:8})))
if [ "$bytes" -ne 4096 ]; then
  echo "       => Something has gone wrong! Please check every step manually"
  exit 1
fi

echo " 3e. Extract $step3.kernel and check if it is identical with $step3.tmp"
dd if=$step3.img of=$step3.kernel bs=$bytes count=1 status=none
diff $step3.tmp $step3.kernel
if [ "$?" -ne 0 ]; then
  echo "       => Something has gone wrong! Please check every step manually"
  exit 1
fi
echo "       => Yes!"
