# 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=5
pkgdesc='File type identification utility'
arch=('x86_64' 'aarch64')
license=('custom')
url='https://www.darwinsys.com/file/'
depends=('glibc'
         'bzip2' 'libbz2.so'
         'libseccomp' 'libseccomp.so'
         'xz' 'liblzma.so'
         'zlib' 'libz.so'
         'zstd' 'libzstd.so')
makedepends=('git')
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=("git+https://github.com/file/file.git#tag=FILE${pkgver//./_}"
        '0001-PR-579-net147-Fix-stack-overrun.patch'
        '0002-PR-571-jschleus-Some-zip-files-are-misclassified-as-data.patch'
        '0003-Fix-termios-handling.patch')
validpgpkeys=('BE04995BA8F90ED0C0C176C471112AB16CB33B3A') # Christos Zoulas
sha256sums=('c0e07f4456de8ca4c7c8225f0675bc2709e9ed376db8d6e44c799884b1053cf2'
            'fd618e04f3c52a77a74883ec81e918576b4c326b1d42be0dc5427033b181f7b2'
            'c93c8ae784ba27c2eec02a94db7bc7080d1b116d4c8e4a9191bd67b4288f2b0e'
            'c0b4e40b7f7782c3a4622d040a3fb66bd644458e86847705e9fba3949b3fdbd0')

prepare() {
  cd file

  # 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

  autoreconf -fiv
}

build() {
  cd file

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

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

check() {
  cd file

  make check
}

package() {
  cd file

  make DESTDIR="$pkgdir" install
  install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"
}

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