# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>

pkgname=u-boot-rm
pkgver=2020.09
_rm1tag=97b35fdddf0077abd2e0f0409b94ef20adbe8565
_rm2tag=47c91918aa7724c16b6eaf87cf5dfbf4548eafc1
pkgrel=9
pkgdesc="u-boot bootloader common files"
url="https://www.denx.de/wiki/U-Boot/"
arch="armv7"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11"
options="!check" # no tests
makedepends="
	bc
	bison
	dtc
	flex
	gnutls-dev
	linux-headers
	openssl-dev>3
	py3-elftools
	py3-setuptools
	python3-dev
	swig
	util-linux-dev
	"
source="
	u-boot-rm1-$_rm1tag.tar.gz::https://github.com/reMarkable/uboot/archive/$_rm1tag.tar.gz
	u-boot-rm2-$_rm2tag.tar.gz::https://github.com/reMarkable/uboot/archive/$_rm2tag.tar.gz
	update-u-boot
	README.txt
	rm1-uboot-config-patch.diff
	rm2-uboot-config-patch.diff
	"
builddir="$srcdir"

#rm2:zero-sugar broken
case "$CARCH" in
arm*) board_configs="
	zerogravitas:zero-gravitas
	";;
esac

for board_config in $board_configs; do
	_allboards="$_allboards $pkgname-${board_config%%:*}"
done

subpackages="$pkgname-all:_all $_allboards"
prepare() {
	default_prepare

	cd "$builddir"/uboot-$_rm1tag
	patch -p1 -i "$srcdir"/rm1-uboot-config-patch.diff
	# patch -p1 -i "$srcdir"/rm1-ttygs0-serial.diff
	cd "$builddir"/uboot-$_rm2tag
	patch -p1 -i "$srcdir"/rm2-uboot-config-patch.diff
}

build() {
	local board_config board
	for board_config in $board_configs; do
		local configs="${board_config#*:}"
		for board in ${configs//,/ }; do
			msg "Building u-boot for $board"
			case $board in
				zero-gravitas) cd "$builddir"/uboot-$_rm1tag;;
				zero-sugar) cd "$builddir"/uboot-$_rm2tag;;
			esac

			touch include/config.h
			LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
			LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h

			export BUILD_DIR="$builddir"/build/$board
			mkdir -p "$BUILD_DIR"
			make O="$BUILD_DIR" ${board}_config
			make O="$BUILD_DIR" all
		done
	done
}

package() {
	mkdir -p "$pkgdir"/usr/share/$pkgname "$pkgdir"/usr/sbin
	install "$srcdir"/README.txt "$pkgdir"/usr/share/$pkgname/README.txt
	install "$srcdir"/update-u-boot "$pkgdir"/usr/sbin
}

_all() {
	pkgdesc="u-boot for all boards (meta package)"
	depends="$_allboards"

	mkdir -p "$subpkgdir"/
}

_split_boards() {
	cd "$builddir"/build
	pkgdesc="u-boot for $1"
	depends="u-boot-rm"
	shift
	local board
	for board; do
		msg "Including board $board"
		mkdir -p "$subpkgdir"/usr/share/$pkgname/$board
		export BUILD_DIR="$builddir"/build/$board

		cp "$BUILD_DIR"/u-boot.imx "$subpkgdir"/usr/share/$pkgname/$board/u-boot.bin
	done
}

for board_config in $board_configs; do
	_board="${board_config%%:*}"
	_configs="${board_config#*:}"
	eval "$_board() { _split_boards $_board ${_configs//,/ }; }"
done

sha512sums="
7cc8d513cac1d63ee5119529caa056164ee3bc3633da5d880fbfb344b770c7c53c41475d42dd129b505390dcd5449e14524afc06f7cdba9354af7e70a7035ef9  u-boot-rm1-97b35fdddf0077abd2e0f0409b94ef20adbe8565.tar.gz
d0d18b123c4281285a8b3c1afd9ef623b48a33506399c8e0dbc393f22d967ee3e5df7030aab26c4fe8cad614f3f22f1ea5d6b5bb4ef5cc46fffe56b7ac7c3c28  u-boot-rm2-47c91918aa7724c16b6eaf87cf5dfbf4548eafc1.tar.gz
a905e4dcdb2704e0b1d4f0ab4a6333d3f1ffcec5c3a8359f5789800eb2114dad673cf0b5f24cae3a590f27333dff2e96b6f1efae5a466f9281a3d65efe403a1e  update-u-boot
f8c9bb6e84d6f0620c976ac7ad5dd7ec7ff9dfdd4b1d03d2bf6653e7beccf80bdf2debfc92fb1f696dba92fb40287d3c45897e0078951451d0835cb61a5f16d1  README.txt
673cb917cc565ae245d4a7d659227bc240473112bc9c73ae184a64ce74be7994273d6dc4db17194d348941f83a42b3b67d3140f6f23c37c13d6dc93f7d76cc39  rm1-uboot-config-patch.diff
0e98404ca6d20eb01bd9c129b05e2eb10c60a692a56adc221ea283b4bfe7b0bcc2851121d4c3fe51e07a9784c339f7ccb221e0cc3f3603144fec46be66d0cbcc  rm2-uboot-config-patch.diff
"
