# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=librasterlite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=1.1.0-beta1
pkgver=1.1.0
pkgrel=9
pkgdesc='An open source library that stores and retrieves huge raster coverages using a SpatiaLite DBMS (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.gaia-gis.it/fossil/librasterlite2/index'
license=('spdx:MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later AND GPL-3.0-or-later') # GPL3 for tools
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-sources/${_realname}-${_pkgver}.tar.gz"
        "001-fix-mingw-detection.patch"
        "002-fix-linking-on-clang.patch")
sha256sums=('f7284cdfc07ad343a314e4878df0300874b0145d9d331b063b096b482e7e44f4'
            'f4a9b63767f8ed0e73085a731ffdd9d4d0d8044a7a1976210c930c06c6bc4ecc'
            '088c975841a8cec6971db5b46194e1d8e627808d4a3034dc27f25a5bffd3ac15')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  patch -p1 -i "${srcdir}"/001-fix-mingw-detection.patch
  patch -p1 -i "${srcdir}"/002-fix-linking-on-clang.patch

  autoreconf -fiv
}

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

  CFLAGS+=" -Wno-implicit-function-declaration -Wno-incompatible-pointer-types"
  if [[ ${CC} == clang ]]; then
    CFLAGS+=" -Wno-incompatible-function-pointer-types"
  fi

  ../${_realname}-${_pkgver}/configure \
    --prefix=${MINGW_PREFIX}

  make
}

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

  make DESTDIR="${pkgdir}" install

  # Fix mod_rasterlite2 location to be possible: SELECT load_extension('mod_rasterlite2');
  mv "${pkgdir}${MINGW_PREFIX}/lib/mod_rasterlite2.dll" "${pkgdir}${MINGW_PREFIX}/bin/"

  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
