#!/bin/sh

#
# Script used by hostapps updater to flash bootloader onto internal media
#

set -o errexit

# machine specific data
uboot_file="u-boot-sunxi-with-spl.bin"
uboot_block_size=1024
uboot_seek_blocks=8

device="/dev/"$(findmnt --noheadings --canonicalize --output SOURCE /mnt/boot/ | xargs lsblk -no pkname)

update_files="uboot"

for i in $update_files; do
	current_update_file=$(eval echo \$${i}_file)
	block_size=$(eval echo \$${i}_block_size)
	seek_blocks=$(eval echo \$${i}_seek_blocks)

	# calculate size and md5sum of the binary to update from the update bundle
	update_size=$(ls -al /resin-boot/$current_update_file | awk '{print $5}')
	update_md5sum=$(md5sum /resin-boot/$current_update_file | awk '{print $1'})

	# calculate number of bytes to skip when computing the checksum of the data we want to update (i.e. the data already written to $device)
	let skip_bytes=$block_size*$seek_blocks

	# calculate md5sum of the data already written to $device, using $update_size bytes and skipping $skip_bytes from $device
	existing_md5sum=$(dd if=$device skip=$skip_bytes bs=1 count=$update_size status=none | md5sum | awk '{print $1}')

	if [ ! "$existing_md5sum" = "$update_md5sum" ]; then
		dd if=/resin-boot/$current_update_file of=$device conv=fdatasync seek=$seek_blocks bs=$block_size
	fi
done

