#!/bin/sh
# SPDX-License-Identifier: MIT

set -e

echo "Downloading m1n1.bin..."
curl --no-progress-meter -L -o /tmp/m1n1.bin https://files2.tobhe.de/ubuntu/m1n1.bin

echo "Find EFI partition..."
disk=$(diskutil list | grep "EFI -" | awk '{print $(NF)}' | head -n1)
echo "\tusing $disk."

echo "Mounting EFI partition..."
diskutil mount "/dev/$disk"

echo "Find mount path"
mount_path="$(df | grep $disk | awk '{ print substr($0, index($0,$9)) }')"
echo "\tusing: $mount_path"

echo "Copying m1n1.bin to ESP..."
cp "$mount_path/m1n1/boot.bin" "$mount_path/m1n1/boot.bin.backup"
cp "/tmp/m1n1.bin" "$mount_path/m1n1/boot.bin"

diskutil umount "$disk"
echo "Done!"
