# 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.190
pkgrel=1
pkgdesc="Handle ELF object files and DWARF debugging information"
arch=(x86_64)
url="https://sourceware.org/elfutils/"
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=('9c4f5328097e028286c42f29e39dc3d80914b656cdfbbe05b639e91bc787ae8ae64dd4d69a6e317ce30c01648ded10281b86a51e718295f4c589df1225a48102'
            'SKIP')
b2sums=('9934aff08f1898377708b28595fb52130ef9f80408132ac7d306845b10672ba45cf1ac69783da07b3eea9fd647741d44da45b8b2626c45a768cb2844c6186513'
        'SKIP')
validpgpkeys=(
  '47CC0331081B8BC6D0FD4DA08370665B57816A6A'  # Mark J. Wielaard <mark@klomp.org>
  'EC3CFE88F6CA0788774F5C1D1AA44BE649DE760A'  # Mark Wielaard <mjw@gnu.org>
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # remove failing test due to missing glibc debug package during test: https://bugs.archlinux.org/task/74875
  sed -e 's/run-backtrace-native.sh//g' -i $pkgbase-$pkgver/tests/Makefile.am
  (
    cd $pkgbase-$pkgver
    autoreconf -fiv
  )

  cp -av $pkgbase-$pkgver $pkgbase-test-$pkgver
}


build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc
    --program-prefix="eu-"
    --enable-deterministic-archives
  )

  (
    # fat-lto-objects is required for non-mangled .a files in libelf
    CFLAGS+=" -ffat-lto-objects"
    cd $pkgbase-$pkgver
    ./configure "${configure_options[@]}"
    make
  )
  (
    cd $pkgbase-test-$pkgver
    # debugging information is required for test-suite
    CFLAGS+=" -g"
    # fat-lto-objects is required for non-mangled .a files in libelf
    CFLAGS+=" -ffat-lto-objects"
    ./configure "${configure_options[@]}"
    make
  )
}

check() {
  make check -C $pkgbase-test-$pkgver
}

package_debuginfod() {
  pkgdesc+=" (debuginfod)"
  license=(
    GPL-3.0-or-later
  )
  depends=(
    gcc-libs
    glibc
    libarchive libarchive.so
    libelf=$pkgver
    libmicrohttpd libmicrohttpd.so
    sqlite libsqlite3.so
  )
  optdepends=("elfutils=$pkgver: for translations")

  make DESTDIR="$pkgdir" install -C $pkgbase-$pkgver
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,ChangeLog,NEWS,NOTES,README} -t "$pkgdir/usr/share/doc/$pkgname/"

  # set the default DEBUGINFOD_URLS environment variable to the distribution's debuginfod URL
  printf "https://debuginfod.archlinux.org\n" > "$pkgdir/etc/debuginfod/archlinux.urls"

  (
    cd "$pkgdir"

    _pick libelf etc/debuginfod/*
    _pick libelf etc/profile.d/*
    _pick libelf usr/{include,lib}
    _pick elfutils usr/bin/eu-*
    _pick elfutils usr/share/locale
    _pick elfutils usr/share/man/man1/eu-*
    _pick elfutils usr/share/man/man3/elf_*
  )

}

package_elfutils() {
  pkgdesc+=" (utilities)"
  license=(
    GPL-3.0-or-later
  )
  depends=(
    gcc-libs
    glibc
    libelf=$pkgver
    sh
  )

  mv -v elfutils/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,ChangeLog,NEWS,NOTES,README} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_libelf() {
  pkgdesc+=" (libraries)"
  license=(
    'GPL-2.0-or-later OR LGPL-3.0-or-later'
  )
  depends=(
    bzip2 libbz2.so
    curl libcurl.so
    gcc-libs
    glibc
    xz liblzma.so
    zlib
    zstd libzstd.so
  )
  # NOTE: the shared objects can not be added to provides as they are not versioned

  mv -v libelf/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,ChangeLog,NEWS,NOTES,README} -t "$pkgdir/usr/share/doc/$pkgname/"
}
