# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=file
pkgver=5.46
pkgrel=2
pkgdesc="File type identification utility"
arch=('i686' 'x86_64')
license=('custom')
url="https://www.darwinsys.com/file/"
msys2_repository_url="https://github.com/file/file"
msys2_references=(
  "cpe: cpe:/a:file_project:file"
)
depends=('gcc-libs' 'zlib' 'libbz2' 'liblzma' 'libzstd')
makedepends=('python' 'zlib-devel' 'libbz2-devel' 'liblzma-devel' 'libzstd-devel' 'autotools' 'gcc')
options=('!libtool')
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")
sha256sums=('c9cc77c7c560c543135edc555af609d5619dbef011997e988ce40a3d75d86088'
            'SKIP'
            '2db041dcadaf8500a8e10d914890963f27a5b716e97f0baf839e01f7c3d8b576'
            'c93c8ae784ba27c2eec02a94db7bc7080d1b116d4c8e4a9191bd67b4288f2b0e')
validpgpkeys=('BE04995BA8F90ED0C0C176C471112AB16CB33B3A') # Christos Zoulas <christos@zoulas.com>

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  # https://gitlab.archlinux.org/archlinux/packaging/packages/file/-/issues/2
  patch -Np1 -i "${srcdir}/0001-PR-579-net147-Fix-stack-overrun.patch"

  # https://gitlab.archlinux.org/archlinux/packaging/packages/file/-/issues/3
  patch -Np1 -i "${srcdir}/0002-PR-571-jschleus-Some-zip-files-are-misclassified-as-data.patch"

  autoreconf -fiv
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-fsect-man5 \
    --enable-zlib \
    --enable-bzlib \
    --enable-xzlib \
    --enable-zstdlib

  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make DESTDIR=${pkgdir} install

  install -Dm644 COPYING ${pkgdir}/usr/share/licenses/${pkgname}/COPYING

  PYTHON_SITELIB=$(/usr/bin/python -c "import sysconfig; print(sysconfig.get_path('platlib'))")
  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  cp -f ${srcdir}/${pkgname}-${pkgver}/python/magic.py "${pkgdir}/$PYTHON_SITELIB"
  python3 -m compileall -o 0 -o 1 -s "${pkgdir}" "${pkgdir}/$PYTHON_SITELIB"
}
