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

_realname=librasterlite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=1.1.0-beta1
pkgver=1.1.0
pkgrel=1
pkgdesc='librasterlite2 is an open source library that stores and retrieves huge raster coverages using a SpatiaLite DBMS (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.gaia-gis.it/fossil/librasterlite2/index'
license=('GPLv3+')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libspatialite")
options=('strip' 'staticlibs')
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-sources/${_realname}-${_pkgver}.tar.gz")
sha256sums=('f7284cdfc07ad343a314e4878df0300874b0145d9d331b063b096b482e7e44f4')

prepare() {
  cd ${_realname}-${_pkgver}
  autoreconf -fiv
}

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=mingw32 \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}

  make
}

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

  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"
}
