# Maintainer: Jeremy Drake <github@jdrake.com>

pkgname=(pacutils pacutils-devel)
pkgver=0.11.0
pkgrel=1
pkgdesc="Utility library for libalpm front-ends (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://github.com/andrewgregory/pacutils"
license=('MIT')
depends=('gcc-libs'
         'libarchive'
         'libcurl'
         'libgpgme'
         'libiconv'
         'libopenssl'
         'pacman')
makedepends=('libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'perl')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/andrewgregory/${pkgname}/archive/refs/tags/v${pkgver}.tar.gz"
        "0001-cast-isspace-parameters-to-unsigned-char.patch"
        "0002-cast-time_t-to-size_t-for-printf.patch"
        "0003-msysize.patch")
sha256sums=('7a7f3aba619c95b23afbd24f64334d2cb20e659a6c03d1d5543f3f2da66f75ae'
            '56346ae17c32a336ed44ac79e3c2c6734aa118f056ac614d6b43fb3c2c447e71'
            '8cadaac06c6261c21014ad9083b791db70dcc48522a75ed3ff8e215d7b4a3ce2'
            'ad5f1aceee99a56f6ff39aaa03fabf99df7d5f6cdca60d842054f07754c44ab5')

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

  # https://github.com/andrewgregory/pacutils/pull/47
  patch -Np1 -i "${srcdir}/0001-cast-isspace-parameters-to-unsigned-char.patch"
  # https://github.com/andrewgregory/pacutils/pull/48
  patch -Np1 -i "${srcdir}/0002-cast-time_t-to-size_t-for-printf.patch"
  patch -Np1 -i "${srcdir}/0003-msysize.patch"
}

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

  make all doc PREFIX=/usr LOCALSTATEDIR=/var SYSCONFDIR=/etc SOPREFIX=msys- SOEXT=dll
  make -j1 DESTDIR="${srcdir}/prefix-${CARCH}" install PREFIX=/usr LOCALSTATEDIR=/var SYSCONFDIR=/etc SOPREFIX=msys- SOEXT=dll
}

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

  make check PREFIX=/usr LOCALSTATEDIR=/var SYSCONFDIR=/etc SOPREFIX=msys- SOEXT=dll
}

package_pacutils() {
  cd "${srcdir}"

  cp -fR "${srcdir}/prefix-${CARCH}/"* "${pkgdir}"
  rm -rf "${pkgdir}/usr/"{include,lib,share/man/man3}
}

package_pacutils-devel() {
  cd "${srcdir}"

  mkdir -p "${pkgdir}/usr/share/man"
  cp -fR "${srcdir}/prefix-${CARCH}/usr/"{include,lib} "${pkgdir}/usr/"
  cp -fR "${srcdir}/prefix-${CARCH}/usr/share/man/man3" "${pkgdir}/usr/share/man/"
}
