# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=raptor
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.16
pkgrel=7
pkgdesc="RDF Syntax Library. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://librdf.org/raptor"
msys2_repository_url="https://github.com/dajobe/raptor"
msys2_references=(
  'archlinux: raptor'
  "cpe: cpe:2.3:a:librdf:raptor_rdf_syntax_library"
)
license=('spdx:Apache-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
source=("http://download.librdf.org/source/raptor2-${pkgver}.tar.gz"
        "001-fix-build-with-mingw.patch"
        "002-fix-build-with-lixml2-2.11.patch::https://github.com/dajobe/raptor/commit/4dbc4c1d.patch")
sha256sums=('089db78d7ac982354bdbf39d973baf09581e6904ac4c92a98c5caadb3de44680'
            '4d63ddb36d5131ad6a540e50af0591ef903c40fab5788ac4fe183f74f836b0bc'
            '5059dcfb1b4694584f2c8fbf75a02b5cd2bfe4f92b0b15e1cc2bd1ff023e4244')

prepare() {
  cd "${srcdir}/${_realname}2-${pkgver}"
  patch -p1 -i "${srcdir}"/001-fix-build-with-mingw.patch
  patch -p1 -i "${srcdir}"/002-fix-build-with-lixml2-2.11.patch

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--enable-release")
  else
    _extra_config+=("--enable-debug")
  fi

  ../${_realname}2-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-static \
    "${_extra_config[@]}"

  make
}

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

  make check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" make install

  PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${PREFIX_WIN}|\$\{prefix\}|g" -e "10s|${MINGW_PREFIX}|\$\{prefix\}|g" {} \;

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}/${_realname}2-${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  install -Dm644 "${srcdir}/${_realname}2-${pkgver}/COPYING.LIB" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  install -Dm644 "${srcdir}/${_realname}2-${pkgver}/LICENSE-2.0.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
