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

pkgname=file
pkgver=5.40
pkgrel=2
pkgdesc="File type identification utility"
arch=('i686' 'x86_64')
license=('custom')
groups=('base-devel')
url="https://www.darwinsys.com/file/"
depends=('gcc-libs' 'zlib' 'libbz2')
makedepends=('python' 'zlib-devel' 'libbz2-devel')
options=('!libtool')
source=(#ftp://ftp.astron.com/pub/${pkgname}/${pkgname}-${pkgver}.tar.gz
        ftp://ftp.astron.com/pub/file/${pkgname}-${pkgver}.tar.gz
        file-5.38-msys2.patch)
sha256sums=('167321f43c148a553f68a0ea7f579821ef3b11c27b8cbe158e4df897e4a5dd57'
            'b9ba0c890e7eccbc9913918daa675e48d7211f7b8182a4e2a87657cd5d1c9acf')


prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/file-5.38-msys2.patch

  autoreconf -fiv
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-fsect-man5

  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 'from distutils.sysconfig import * ; print(get_python_lib(0,0));')

  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  cp -f ${srcdir}/${pkgname}-${pkgver}/python/magic.py ${pkgdir}/$PYTHON_SITELIB

  cat ${srcdir}/${pkgname}-${pkgver}/magic/Magdir/* > ${pkgdir}/usr/share/misc/magic
  cp -rf ${pkgdir}/usr/share/misc/magic ${pkgdir}/usr/share/magic
  mkdir -p ${pkgdir}/usr/share/file
  cp -rf ${pkgdir}/usr/share/magic ${pkgdir}/usr/share/file/magic
}
