# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Laurent Carlier <lordheavym@gmail.com>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>
# Contributor: Andrej Gelenberg <andrej.gelenberg@udo.edu>

pkgbase=elfutils
pkgname=('debuginfod' 'elfutils' 'libelf')
pkgver=0.185
pkgrel=1
pkgdesc='Handle ELF object files and DWARF debugging information'
arch=('x86_64')
url="https://sourceware.org/elfutils/"
license=('LGPL3' 'GPL3')
makedepends=('bzip2' 'curl' 'gcc-libs' 'libarchive' 'libmicrohttpd' 'sqlite'
'xz' 'zlib' 'zstd')
options=('staticlibs')
source=("https://sourceware.org/${pkgbase}/ftp/${pkgver}/${pkgbase}-${pkgver}.tar.bz2"{,.sig})
sha512sums=('34de0de1355b11740e036e0fc64f2fc063587c8eb121b19216ee5548d3f0f268d8fc3995176c47190466b9d881007cfa11a9d01e9a50e38af6119492bf8bb47f'
            'SKIP')
b2sums=('57cfa7e4060975c4bf6170dbd354687a19a92f7069c060db1ac2fa4da5a9d34e8bddd07c7ce376b5c28b5670ad5b8c1df795164cbcfb600a44cf1db60c20c4af'
        'SKIP')
validpgpkeys=(
  '47CC0331081B8BC6D0FD4DA08370665B57816A6A'  # Mark J. Wielaard <mark@klomp.org>
  'EC3CFE88F6CA0788774F5C1D1AA44BE649DE760A'  # Mark Wielaard <mjw@gnu.org>
)

prepare() {
  cd "$pkgbase-$pkgver"
  autoreconf -fiv
}

build() {
  cd "$pkgbase-$pkgver"
  CFLAGS+=" -g"  # required for test-suite success
  ./configure --prefix=/usr \
              --sysconfdir=/etc \
              --program-prefix="eu-" \
              --enable-deterministic-archives
  make
}

check() {
  cd "$pkgbase-$pkgver"
  # Some tests fail due to limited capabilities of build chroot
  make check || echo "Known problem: The build environment might lack enough resources."
}

package_libelf() {
  pkgdesc+=" (libraries)"
  depends=('gcc-libs' 'libbz2.so' 'libcurl.so' 'xz' 'zlib' 'zstd')
  # NOTE: the shared objects can not be added to provides as they are not versioned

  cd "$pkgbase-$pkgver"
  make DESTDIR="$pkgdir" install
  # remove files provided by elfutils or debuginfod
  rm -rvf "${pkgdir}"{/etc,/usr/{bin,share}}
  install -vDm 644 {AUTHORS,ChangeLog,NEWS,NOTES,README} \
    -t "${pkgdir}/usr/share/doc/${pkgname}"
}

package_elfutils() {
  pkgdesc+=" (utilities)"
  depends=('gcc-libs' 'glibc' "libelf=${pkgver}-${pkgrel}")

  cd "$pkgbase-$pkgver"
  make DESTDIR="$pkgdir" install
  install -vDm 644 {AUTHORS,ChangeLog,NEWS,NOTES,README} \
    -t "${pkgdir}/usr/share/doc/${pkgname}"
  # remove files provided by libelf
  rm -rvf "${pkgdir}/usr/"{include,lib}
  # remove files provided by debuginfod
  rm -rvf "${pkgdir}/usr/bin/debuginfod"* \
    "${pkgdir}/usr/share/man/man1/debuginfod"* \
    "${pkgdir}/usr/share/man/man3/debuginfod"* \
    "${pkgdir}/usr/share/man/man8/" \
    "${pkgdir}/etc"
}

package_debuginfod() {
  pkgdesc+=" (debuginfod)"
  depends=('gcc-libs' 'glibc' 'libarchive.so' "libelf=${pkgver}-${pkgrel}"
  'libmicrohttpd.so' 'sqlite')
  optdepends=("elfutils=${pkgver}-${pkgrel}: for translations" )

  cd "$pkgbase-$pkgver"
  make DESTDIR="$pkgdir" install
  install -vDm 644 {AUTHORS,ChangeLog,NEWS,NOTES,README} \
    -t "${pkgdir}/usr/share/doc/${pkgname}"
  # remove files provided by libelf
  rm -rvf "${pkgdir}/usr/"{include,lib}
  # remove files provided by elfutils
  rm -rvf "${pkgdir}/usr/bin/eu-"* \
    "${pkgdir}/usr/share/man/man1/eu-"* \
    "${pkgdir}/usr/share/man/man3/elf_"* \
    "${pkgdir}/usr/share/locale"
}
