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

_realname=qgis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.44.3
pkgrel=3
pkgdesc='Geographic Information System (GIS) that supports vector, raster & database formats (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://qgis.org/'
msys2_repository_url="https://github.com/qgis/QGIS"
msys2_references=(
  'archlinux: qgis'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-draco"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd"
         "${MINGW_PACKAGE_PREFIX}-pdal"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python-gdal"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-owslib"
         "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
         "${MINGW_PACKAGE_PREFIX}-python-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qca-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-3d"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt5-location"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools"
         "${MINGW_PACKAGE_PREFIX}-qtkeychain-qt5"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qwt-qt5"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-spatialindex"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-opencl-clhpp"
             "${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-postgresql")
#optdepends=("${MINGW_PACKAGE_PREFIX}-netcdf: mdal plugin"
#            "${MINGW_PACKAGE_PREFIX}-postgresql: postgres plugins")
source=("https://qgis.org/downloads/${_realname}-$pkgver.tar.bz2"
        "001-fix-building-with-mingw-w64-clang.patch"
        "002-fix-settingstree-init-order-clang.patch"
        "003-fix-build-with-qt5-and-pdal.patch"
        "004-fix-customwidgets-install.patch")
sha256sums=('fb8f1407a0cf2ac776e16bfbc7576217fe1760d1e0789446187c5df8e381b69d'
            '82d8839992f08a26ea7caf5e908e5f4111806df211ae5cec91e2b86e49b43e75'
            '47e6471b92b85d97e58b617da326c15622c01786c19eb7d4d5814483becb75e4'
            'bbd476f619ebd7ed8e4acb0c6a3be9b0356cd37267d7ecbeb53aea372b91207b'
            '216dc67ef707f2e918afb7d190c9ea75c11065dea0d23d11b94cab5d8463bac3')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      001-fix-building-with-mingw-w64-clang.patch \
      002-fix-settingstree-init-order-clang.patch
  fi
  apply_patch_with_msg \
    003-fix-build-with-qt5-and-pdal.patch \
    004-fix-customwidgets-install.patch
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-ignored-attributes"
  else
    CXXFLAGS+=" -Wno-attributes"
  fi
  CXXFLAGS+=" -Wno-deprecated-declarations"

  LDFLAGS+=" -Wl,--stack,8388608" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_CXX_STANDARD=17 \
    "${_extra_config[@]}" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DQGIS_BIN_SUBDIR=bin \
    -DQGIS_DATA_SUBDIR=share \
    -DQGIS_LIBEXEC_SUBDIR=bin \
    -DQGIS_PLUGIN_SUBDIR=lib/qgis/plugins \
    -DQT_PLUGINS_DIR=share/qt5/plugins \
    -DPREFER_INTERNAL_LIBS=OFF \
    -DWITH_INTERNAL_POLY2TRI=ON \
    -DWITH_INTERNAL_MESHOPTIMIZER=ON \
    -DWITH_INTERNAL_NLOHMANN_JSON=OFF \
    -DWITH_3D=ON \
    -DWITH_BINDINGS=ON \
    -DWITH_CUSTOM_WIDGETS=ON \
    -DWITH_EPT=ON \
    -DWITH_PDAL=ON \
    -DWITH_POSTGRESQL=ON \
    -DWITH_QTWEBKIT=OFF \
    -DWITH_QUICK=ON \
    -DWITH_QWTPOLAR=ON \
    -DENABLE_TESTS=OFF \
    -DWITH_GRASS=OFF \
    -DPEDANTIC=OFF \
    ../${_realname}-$pkgver

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

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