#!/bin/bash

if [ ! $1 ]; then
  echo "  Usage:  
      ./makeuboot <target> 
  Supported targets:
      ./makeuboot zynq_microzed_config
      ./makeuboot zynq_zc706_config
      ./makeuboot elphel393_config
      ./makeuboot zynq_zed_config
  "
  exit -1
fi

. ./initenv
make clean
make $1
make include/autoconf.mk
echo "Running ezynqcfg.py for the first time - u-boot length is not known yet, generating arch/arm/mach-zynq/ezynq.c"
ezynq/ezynqcfg.py -c include/autoconf.mk --html u-boot.html -o boot_head.bin --html-mask 0x3ff --lowlevel arch/arm/mach-zynq/ezynq.c

make

echo "Running ezynqcfg.py for the second time - u-boot length is known and will be used in the RBL header"
echo "Other files are already created, repeating it here just to remind their paths"

if [ $(cat .config | grep "CONFIG_SPL=y") ]; then
  UBOOT_FILE="spl/u-boot-spl.bin"
else
  UBOOT_FILE="u-boot.bin"
fi

ezynq/ezynqcfg.py -c include/autoconf.mk -o boot_head.bin --uboot $UBOOT_FILE --html u-boot.html --html-mask 0x3ff --lowlevel arch/arm/mach-zynq/ezynq.c
cat boot_head.bin $UBOOT_FILE > boot.bin