#!/bin/sh

if [ -r /etc/default/raspberrypi-sys-mods ]; then
  . /etc/default/raspberrypi-sys-mods
fi

if [ -z "$FWLOC" ]; then
  if ! dpkg --print-architecture | grep -q '^arm'; then
    FWLOC=/boot
  else
    for FWLOC in /boot/firmware /boot NOT_FOUND; do
      if ( findmnt --fstab "$FWLOC" || findmnt "$FWLOC" ) > /dev/null; then
        break
      fi
    done
  fi
  if [ "$FWLOC" = "NOT_FOUND" ]; then
    echo "WARNING: Firmware partition not found, searching for config.txt.."  >&2
    for FWLOC in /boot/firmware /boot NOT_FOUND; do
      if [ -f "$FWLOC/config.txt" ]; then
        break
      fi
    done
  fi
fi

echo "$FWLOC"

if [ "$FWLOC" = "NOT_FOUND" ]; then
  exit 1
fi

exit 0
