# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Sébastien Luttringer <seblu@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Andreas Radke <andyrtr@archlinux.org>

pkgname=file
pkgver=5.46
pkgrel=3
pkgdesc='File type identification utility'
arch=(x86_64 powerpc64le powerpc64 powerpc espresso riscv64)
license=('custom')
url='https://www.darwinsys.com/file/'
depends=('glibc' 'zlib' 'xz' 'bzip2' 'libseccomp' 'libseccomp.so' 'zstd' 'libzstd.so')
provides=('libmagic.so')
options=('!emptydirs')
# Warning: Upstream commits update file version and date inside each file!
#          Remove these changes from patches when cherry-picking to avoid
#          conflicts!
source=("https://astron.com/pub/$pkgname/$pkgname-$pkgver.tar.gz"{,.asc}
        '0001-PR-579-net147-Fix-stack-overrun.patch'
        '0002-PR-571-jschleus-Some-zip-files-are-misclassified-as-data.patch')
validpgpkeys=('BE04995BA8F90ED0C0C176C471112AB16CB33B3A') # Christos Zoulas
sha256sums=('c9cc77c7c560c543135edc555af609d5619dbef011997e988ce40a3d75d86088'
            'SKIP'
            '2db041dcadaf8500a8e10d914890963f27a5b716e97f0baf839e01f7c3d8b576'
            'c93c8ae784ba27c2eec02a94db7bc7080d1b116d4c8e4a9191bd67b4288f2b0e')

prepare() {
  cd $pkgname-$pkgver
  # apply patch from the source array (should be a pacman feature)
  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done
}

build() {
  cd $pkgname-$pkgver

  # Fix linking libmagic (vfork needs libpthread)
  CFLAGS+=" -pthread"

  case "${CARCH}" in
    powerpc) _configure_flags=(--disable-libseccomp) ;;
    *) _configure_flags=(--enable-libseccomp) ;;
  esac

  ./configure \
    --prefix=/usr \
    --datadir=/usr/share/file \
    --enable-fsect-man5 \
    --enable-libseccomp ${_configure_flags[@]}
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  cd $pkgname-$pkgver
  make check
}

package() {
  cd $pkgname-$pkgver
  make DESTDIR="$pkgdir" install
  install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"
}

# vim:set ts=2 sw=2 et:
