# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

pkgbase=libpsl
pkgname=("${pkgbase}" "${pkgbase}-devel")
pkgver=0.21.5
pkgrel=2
pkgdesc='Public Suffix List library'
url='https://github.com/rockdaboot/libpsl'
arch=('i686' 'x86_64')
license=('spdx:MIT')
makedepends=('libxslt' 'python' 'libidn2-devel' 'publicsuffix-list' 'libunistring-devel' 'autotools' 'gcc' 'gettext-devel' 'gtk-doc')
source=(https://github.com/rockdaboot/libpsl/releases/download/${pkgver}/${pkgname}-${pkgver}.tar.gz
        0.16.0-install-dafsa.patch 
        0.16.1-make-dafsa-setlocale.patch
        019.1-implement-private-libs.patch)
sha512sums=('c14d575cecc0f1693894dd79565b6b9220084ddfa43b908a1cefe16d147cdd5ec47796eb0c2135e2f829a951abaf39d8a371ab5c1352f57b36e610e25adf91f5'
            '42cf2d5ff3bd48fbe00d1bf5bc4e7acb0554b9fae65b941c27abf8bd8a21d2c678b78b1e356c7596fc20d17e226dc4014e36b95eb785e9beb41e43d074ef5483'
            '36f4ae0bee8d25a55fb38b64525fa855698830310f5b7e5b3c5c96fa6799622225eab67ccfdb0e20d2092f1398563231c02aecd4bc8c042796cb471ca905fd10'
            '93290b524b7ea0b4aa5abd50a1cdcad4eeb8d135d7c9d6da02a4751065a7a93fd3c12a5b0be399baf00584d5830f7fb59f4414d56cfd139ec493a799ffbee1d6')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgbase}-${pkgver}
  apply_patch_with_msg \
    0.16.0-install-dafsa.patch \
    0.16.1-make-dafsa-setlocale.patch \
    019.1-implement-private-libs.patch

  rm -frv list
  autoreconf -fiv
}

build() {
  cd ${pkgbase}-${pkgver}
  
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  PYTHON=/usr/bin/python.exe ../${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --disable-dependency-tracking \
    --enable-shared \
    --enable-static \
    --enable-man \
    --enable-builtin=libidn2 \
    --enable-runtime=libidn2 \
    --with-psl-file=/usr/share/publicsuffix/public_suffix_list.dat \
    --with-psl-testfile=/usr/share/publicsuffix/test_psl.txt
  LC_CTYPE=en_US.UTF-8 make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check
}

package_libpsl() {
 pkgdesc="Public Suffix List library (runtime)"
  depends=('libidn2' 'libunistring')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/* ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man1 ${pkgdir}/usr/share/man/
  install -Dm 644 ${srcdir}/${pkgbase}-${pkgver}/COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"

}

package_libpsl-devel() {
  pkgdesc="libpsl headers and libraries"
  options=('staticlibs')
  depends=("libpsl=${pkgver}" 'libidn2-devel' 'libunistring')
  groups=('development')

  mkdir -p ${pkgdir}/usr/share/man/man3
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

}
