#!/bin/sh
if grep -wq "CONFIG_SELF_EXTRACTING" include/generated/autoconf.h;
then
cd ram_init
echo "compressed uboot version"
cp ../decompress/decompress.bin ./
filesize=`stat -c %s decompress.bin`

echo "compressed u-boot filesize is $filesize"
initsize=0x2000
raminitsize=6144
touch init.S spi_nand.S
make -f makefile_boot BOOT_SIZE=2048 RAMINIT_SIZE=$raminitsize
make -f makefile_raminit RAM_VERSION_SIZE=$filesize \
FLASH_VERSION_SIZE=$initsize DECOMPRESS=$do_decompress

dd bs=1 count=$raminitsize if=init.bin of=sysboot.bin skip=0 seek=2048

mv sysboot.bin init.bin

#dd bs=1 count=$filesize if=u-boot.bin of=init.bin skip=0 seek=20480
dd bs=1 count=$filesize if=decompress.bin of=init.bin skip=0 seek=8192

else
echo "uncompress uboot version"
cd ram_init
cp ../u-boot.bin ./
filesize=`stat -c %s u-boot.bin`
#filesize=262144
initsize=0x2000
touch init.S spi_nand.S
echo "ram version filesize is $filesize"
#ram uboot limit is 376K
file_limit=385024

if [ "$filesize" -gt "$file_limit" ];then
echo "uboot exceed maximum size"
exit 1
fi
make -f makefile_raminit RAM_VERSION_SIZE=$filesize SYSTEM_INIT_SIZE=$initsize
raminitsize=`stat -c %s init.bin`

make -f makefile_boot BOOT_SIZE=2048 RAMINIT_SIZE=$raminitsize
dd bs=1 count=$raminitsize if=init.bin of=sysboot.bin skip=0 seek=2048

mv sysboot.bin init.bin
cp u-boot.bin u-boot_s2.bin
cp init.bin u-boot_s1.bin
dd bs=1 count=$filesize if=u-boot.bin of=init.bin skip=0 seek=8192
fi
cp init.bin ../u-boot.bin
if grep -wq "UBOOT_AS_BL33" ../include/generated/autoconf.h;
then
echo "uboot as bl33"
cp u-boot_s2.bin ../u-boot.bin
fi
#./merge.sh
