#!/bin/sh
 
uboot_file=baserock/u-boot.bin
system_image=$1
 
if [[ $(id -u) != 0 ]]; then
    echo "Script must be run as root"
    exit
fi;
 
if [ ! -f "${uboot_file}" ]; then
    echo "Error: missing u-boot file $uboot_file"
    exit
fi;
 
if [ ! -f "${system_image}" ]; then
    echo "Error: missing system image"
    exit
fi;
 
if [ -f "bootloader/system.img" ]; then
    echo "Backup existing system image"
    cp bootloader/system.img bootloader/system.img.bak
fi;
 
ln -s $1 bootloader/system.img
echo "Flashing u-boot and baserock image to emmc"
ROOTFS_SIZE=8589934592 ./flash.sh -L baserock/u-boot.bin -r jetson-tk1 mmcblk0p1
echo "Finished flashing...."
rm bootloader/system.img
if [ -f "bootloader/system.img.bak" ]; then
    mv bootloader/system.img.bak bootloader/system.img
fi;
