#!/bin/sh
set -e

src=/usr/share/m1n1/
uboot=/usr/share/u-boot-asahi

for i in /boot/efi/m1n1 /boot/m1n1; do
	if [ -d "$i" ]; then
		m1n1_dir="$i"
		break
	fi
done

if [ -z "$m1n1_dir" ]; then
	echo "$m1n1_dir does not exist, is /boot/efi mounted?" 1>&2
	exit 1
fi

target="${1:-$m1n1_dir/boot.bin}"
DTBS=/boot/dtbs-asahi/apple/*

cat "$src/m1n1.bin" $DTBS \
    <(gzip -c "$uboot/u-boot-nodtb.bin") \
    >"${target}.new"
mv -f "${target}.new" "$target"

echo "m1n1 updated at ${target}"
