#!/bin/sh

board=
partition=/dev/disk/by-partlabel/BOOT
verbose=
dryrun=
imagedir=/usr/share/u-boot/
skip_delay=

get_board() {
	if [ -z "$board" ] && [ -e /sys/firmware/devicetree/base/compatible ]; then
		case "$(cat /sys/firmware/devicetree/base/compatible 2>/dev/null)" in
		samsung,n1awifi*) board="n1awifi";;
		samsung,n2awifi*) board="n2awifi";;
		samsung,v1a-wifi*) board="v1a-wifi";;
		esac
	fi
}

die() {
	echo "ERROR: $@"
	exit 1
}

usage() {
	get_board

	cat <<EOF
usage: $0 [-n,--dry-run] [-i,--imagedir <imagedir>] [-b|--board <board-type>] [-p|--partition <partition>] [-s|--skip-delay]

options:

 -b,--board <board>       Specify the board type: n1awifi, n2awifi, v1awifi
                          (current default: ${board:-none})

 -p, --partition <partition> Specify partition to flash u-boot to
						  (current default: ${partition:-none}) 

 -i,--imagedir <imagedir> Specify u-boot image directory
                          (current default: ${imagedir:-none})

 -n,--dry-run             Print commands but don't execute them

 -s,--skip-delay          Skip delay and flash the image immediately 

EOF
}

validate_checksum() {
	file="$1"
	file_sha512="$file.sha512"
	file_size=$(stat -c%s $file)
	partition="$2"
	bs="$3"

	checksum0=$(cat $file_sha512 | awk {'print $1'})
	checksum1=$(dd if=$partition bs=1 count=$file_size status=none | sha512sum | awk {'print $1'})
	if [ "$checksum0" != "$checksum1" ]; then
		echo "File: $checksum0"
		echo "Part: $checksum1"
		die "Checksum failed"
	fi
	echo "Successful U-Boot image checksum verification on $partition :"
	echo -e "\t$checksum1"
}

while [ $# -gt 0 ]; do
	opt="$1"
	shift
	case "$opt" in
	-b|--board)
		case "$1" in
		n1awifi) board="n1awifi" ;;
		n2awifi) board="n2awifi" ;;
		v1awifi) board="v1a-wifi" ;;
		*) usage; exit 1;;
		esac
		shift
		;;
	-p|--partition)
		partition="$1"
		shift
		;;
	-i|--imagedir)
		imagedir="$1"
		shift
		;;
	-n|--dry-run)
		dryrun="echo"
		;;
	-s|--skip-delay)
		skip_delay="yes"
		;;
        --)
                break
                ;;
        -*)
                usage
                exit 1
                ;;
        esac
done

get_board

echo "/!\ WARNING:"
echo "This upgrade may not work reliably, postmarketOS might not boot anymore!"
echo
read -p "Continue? (y/n) [n]: " -n 1 -r
echo
case "$REPLY" in
	y|Y) ;;
	*) exit 1 ;;
esac

if [ -z "$dryrun" ]; then
	if [ -z "$skip_delay" ]; then
		echo "Updating $board u-boot in $device in 3 seconds..."
		sleep 3
	else
		echo "Updating $board u-boot in $device"
	fi
fi

(
set -e
[ -e "$imagedir/samsung/u-boot-$board.bin" ] || die "$board images not installed, apk add u-boot-exynos5420"
$dryrun echo "Generating android boot image..."
$dryrun mkbootimg --kernel "$imagedir"/samsung/u-boot-$board.bin --base "0x10000000" --kernel_offset "0x00008000" --ramdisk_offset "0x01000000" --tags_offset "0x00000100" --second_offset "0x00f00000" --pagesize "2048" -o /tmp/aboot.img
[ -z "$dryrun" ] && printf "SEANDROIDENFORCE" >> /tmp/aboot.img
[ -z "$dryrun" ] && sha512sum /tmp/aboot.img > /tmp/aboot.img.sha512
$dryrun dd if=/tmp/aboot.img of=$partition bs=512 oflag=direct status=none
[ -z "$dryrun" ] && validate_checksum /tmp/aboot.img $partition 512
$dryrun rm -f /tmp/aboot.img /tmp/aboot.img.sha512
$dryrun sync
) || die "U-Boot installation in $partition failed"

[ -z "$dryrun" ] && echo "Completed successfully."
