# Template file for 'efivar'
pkgname=efivar
version=39
revision=0
short_desc="EFI variable filesystem library and tools"
maintainer="finwo <finwo@pm.me>"
license="LGPL-2.1-or-later"
homepage="https://github.com/rhboot/efivar"
distfiles="https://github.com/rhboot/efivar/archive/refs/tags/39.tar.gz"
checksum=c9edd15f2eeeea63232f3e669a48e992c7be9aff57ee22672ac31f5eca1609a6
# glibc + busybox first: the post-install runs /sbin/ldconfig under
# /bin/sh (ordering, not just runtime truth - see libmnl).
depends="glibc busybox"

# Notes:
# - No configure step upstream; plain make with PREFIX/LIBDIR (defaults
#   would land libs in lib64). Docs stay on (host mandoc present).
# - -Werror is upstream default; if a newer host gcc trips it, evaluate
#   the warning before reaching for overrides (grub precedent:
#   --disable-werror exists because vintage code + new gcc).
# - Shared + static both build; the .a files are removed per the
#   shared-only policy. Sysroot seed + .pc rewrites (three .pc files,
#   fixed centrally by the driver) so efibootmgr's build finds them via
#   PKG_CONFIG_PATH.
# - Upstream efivar.pc/efiboot.pc spell Cflags as -I${includedir}/efivar
#   only. That works on a host where /usr/include is an implicit search
#   path (so `#include <efivar/efivar.h>` inside efiboot.h resolves), but
#   in a sysroot build the bare /usr/include is NOT searched and consumers
#   that only `pkg-config efivar efiboot` (e.g. efibootmgr itself) fail.
#   Patch the .pc.in files to expose BOTH -I${includedir} and the subdir.
# - The `efivar` CLI ships too (reading/writing EFI vars from UNOS itself).
# - Every UNOS library package carries the ldconfig post-install.

do_build() {
  cd "${WRKSRC}"
  # Expose the parent include dir too (see Notes above); .pc.in are
  # templated with @@PLACEHOLDERS@@, so patch the source before any
  # generation step writes the final .pc files.
  for pc in src/efivar.pc.in src/efiboot.pc.in src/efisec.pc.in; do
    [ -f "${pc}" ] || continue
    sed -i 's|Cflags: -I${includedir}/efivar|Cflags: -I${includedir} -I${includedir}/efivar|' "${pc}"
  done
  make -j"$(nproc)" PREFIX=/usr LIBDIR=/usr/lib
  [ -f src/libefivar.so ] || die "efivar: shared lib missing after build"
}

do_install() {
  cd "${WRKSRC}"
  make install PREFIX=/usr LIBDIR=/usr/lib DESTDIR="${DESTDIR}"
  make install PREFIX=/usr LIBDIR=/usr/lib DESTDIR="${SYSROOT}"
  rm -f "${DESTDIR}"/usr/lib/*.a "${SYSROOT}"/usr/lib/*.a
  for pc in efivar.pc efiboot.pc efisec.pc; do
    [ -f "${SYSROOT}/usr/lib/pkgconfig/${pc}" ] || die "efivar: no sysroot ${pc}"
  done
}
