# Template file for 'iproute2'
pkgname=iproute2
version=6.19.0
revision=3
short_desc="IP routing and network device configuration tools"
maintainer="finwo <finwo@pm.me>"
license="GPL-2.0-only"
homepage="https://www.kernel.org/pub/linux/utils/net/iproute2/"
distfiles="https://www.kernel.org/pub/linux/utils/net/iproute2/iproute2-6.19.0.tar.xz"
checksum=9332213d35480b647086a70c302de8568de83455a98774d35de216c4ce191006
depends="libmnl libcap"
makedepends="libmnl libcap"
hostmakedepends="bison flex"

# Notes:
# - `ip` + `rtmon` + `ss` ONLY (PLAN section 10): the top-level
#   `install: all` would build every subdir (tc needs flex/bison/xtables/elf),
#   so the template builds SUBDIRS="lib ip misc" and cherry-picks the three
#   binaries plus the iproute2 config files (rt_tables symbolic names live
#   there). tc/bridge/nstat/ifstat/rtacct/lnstat land as separate design
#   decisions, not silent extras. misc needs host bison/flex (build-only
#   code generators, same category as perl for openssl); its libbpf/rpc
#   includes are already gated off by the scrub below.
# - libmnl comes from the sysroot (extended-error-ack decoding) and libcap
#   (drop_cap ambient-capability hygiene); configure finds both via the
#   driver-exported PKG_CONFIG_PATH.
# - Host-independence scrub: configure appends every detected optional to
#   GLOBAL LDLIBS, so any host dev library would silently link into `ip`.
#   BPF/XDP program loading (elf, libbpf), selinux and tirpc go - none of
#   them has consumers in lib/ or ip/. Patterns are anchored; absent lines
#   are no-ops; do_build asserts the final linkage so a rotted scrub fails
#   the build, never the boot.
# - CONF_USR_DIR bakes to /usr/share/iproute2; the config files are
#   installed to match.

do_configure() {
  cd "${WRKSRC}"
  ./configure
  sed -i \
    -e '/^HAVE_ELF:=/d' \
    -e '/^HAVE_LIBBPF.*:=/d' \
    -e '/^HAVE_SELINUX:=/d' \
    -e '/^HAVE_RPC:=/d' \
    -e '/^CFLAGS += -DHAVE_ELF/d' \
    -e '/^CFLAGS += -DHAVE_LIBBPF/d' \
    -e '/^CFLAGS += -DHAVE_SELINUX/d' \
    -e '/^CFLAGS += -DHAVE_RPC/d' \
    -e '/^LDLIBS +=.*-lelf/d' \
    -e '/^LDLIBS +=.*selinux/d' \
    -e '/^LDLIBS +=.*tirpc/d' \
    config.mk
}

do_build() {
  cd "${WRKSRC}"
  # iproute2 has no kbuild-style flag tracking: objects from a previous
  # configure do not rebuild when config.mk flags change. Clean
  # unconditionally (cheap for lib+ip+misc; full-tree cost is why SUBDIRS
  # is limited here too).
  make SUBDIRS="lib ip misc" clean >/dev/null
  make -j"$(nproc)" SUBDIRS="lib ip misc"
  for bin in ip/ip ip/rtmon misc/ss; do
    [ -x "${bin}" ] || die "iproute2: ${bin} missing after build"
    needed=$(readelf -d "${bin}" | sed -n 's/^.*NEEDED.*\[\(.*\)\].*$/\1/p' | sort)
    for lib in ${needed}; do
      case "${lib}" in
        libmnl.so.*|libcap.so.*|libc.so.*) : ;;
        *) die "iproute2: ${bin}: unexpected NEEDED entry: ${lib}" ;;
      esac
    done
  done
}

do_install() {
  cd "${WRKSRC}"
  install -D -m 755 ip/ip "${DESTDIR}/sbin/ip"
  install -D -m 755 ip/rtmon "${DESTDIR}/sbin/rtmon"
  install -D -m 755 misc/ss "${DESTDIR}/sbin/ss"
  for f in etc/iproute2/*; do
    [ -f "${f}" ] || continue
    install -D -m 644 "${f}" "${DESTDIR}/usr/share/iproute2/$(basename "${f}")"
  done
  [ -f "${DESTDIR}/usr/share/iproute2/rt_tables" ] || die "iproute2: rt_tables missing"
}
