#!/bin/bash -e

# We are using same name as the debian's raspi-firmware script, but we
# only need to run this script on ubuntu. Debian's package has a postinst
# section capable of copying files, but Ubuntu relies on flash-kernel
[[ -d /usr/lib/linux-firmware-raspi ]] || exit 0

if grep -q '^ID=debian' /etc/os-release; then
   exit 0
fi

# Play nice when run under debconf.
exec </dev/null >&2

# Ensure the target directory exists.
mkdir -p /boot/firmware

for file in /usr/lib/linux-firmware-raspi/*
do
file=$( basename "$file" )
cp "/usr/lib/linux-firmware-raspi/$file" "/boot/firmware/$file"
sync -f "/boot/firmware/$file" || true
done

exit 0
