APP=rpi-userland
VERSION=20200603
BUILD=1sml
HOMEPAGE='https://github.com/raspberrypi/userland'
DESC="common raspberry-pi userland utilities"
REQUIRES="musl"

build() {
  compileonlyfor aarch64
  mkandenterbuilddir
  rm -rf $APP-$VERSION

  tar xf $SRCDIR/$APP-$VERSION.tar.?z*
  cd $APP-$VERSION 
  fixbuilddirpermissions

  # this package does not like execinfo header, and will fail to link against the execinfo
  # library, so we temporarily rename the header
  [ -e /include/execinfo.h ] && mv /include/execinfo.h /include/execinfo.h.bak

  sed -i -e 's/sudo //' buildme
  ./buildme --aarch64 "$PKG"

  # The build script does not exit with a status 0, so we do a couple of file checks ourselves
  if ! [ -e $PKG/opt/vc/bin/dtmerge ] ; then
    echo "Oops, compilation failed!"
    exit 1
  elif ! [ -e $PKG/opt/vc/bin/tvservice ]; then
    echo "Oops, compilation failed!"
    exit 1
  fi
  
  install -Dm 644 $SRCDIR/10-vchiq.rules $PKG/lib/udev/rules.d/10-vchiq.rules
  install -Dm 755 $SRCDIR/rpi-userland.sh $PKG/etc/profile.d/rpi-userland.sh

  # restore the execinfo header to its original name
  [ -e /include/execinfo.h.bak ] && mv /include/execinfo.h.bak /include/execinfo.h

  cp LICENCE $PKGDOCS/

  mkfinalpkg
}

SHA512SUMS="
dbfe0e6bc50eeb745e66bbfdb5bc6f4774ed51d99f334a3604f73df997bf1a4be4ee8f3e703022f128640a28fac8b2cf7366ad563e07d2afdef67ea4de747f2c  rpi-userland-20200603.tar.lz
"
