# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: Brian Galey <bkgaley at gmail dot com>
# Contributor: Pietro Zambelli <peter.zamb at gmail dot com>

_realname=libspatialite
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.0.1
pkgrel=2
pkgdesc="SQLite extension to support spatial data types and operations (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gaia-gis.it/fossil/libspatialite/index"
license=('MPL' 'GPL' 'LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "automake" "autoconf")
depends=("${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-librttopo"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-minizip"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
options=('strip')
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-${pkgver}.tar.gz"
        "01-fix-pkgconfig.patch")
sha256sums=('eecbc94311c78012d059ebc0fae86ea5ef6eecb13303e6e82b3753c1b3409e98'
            '6635724e88dc5f840e3b3df2a236ae8f8a61321f5c78fd7f382008009c9a2a65')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  sed -i configure.ac -e "s|mingw32|${MINGW_CHOST}|g"
  patch -p1 -i ${srcdir}/01-fix-pkgconfig.patch
  autoreconf
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-rttopo \
    --enable-libxml2

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
  # Fix mod_spatialite location to be possible: SELECT load_extension('mod_spatialite');
  mv "${pkgdir}${MINGW_PREFIX}/lib/mod_spatialite.dll" "${pkgdir}${MINGW_PREFIX}/bin/"
}
