# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.7.2
pkgrel=2
_rj_ver=1.1.0
_lerc_commit=19542a00b9a8b5c1089f74239e5859e02e403212
arch=('any')
pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://www.pelicanmapping.com/home-1/opensource"
msys2_repository_url="https://github.com/gwaldron/osgearth"
msys2_references=(
  'aur: osgearth'
)
license=('spdx:LGPL-3.0-or-later WITH LGPL-3.0-linking-exception')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         #"${MINGW_PACKAGE_PREFIX}-blosc"
         #"${MINGW_PACKAGE_PREFIX}-draco"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-glew"
         #"${MINGW_PACKAGE_PREFIX}-leveldb"
         #"${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-spdlog"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             "${MINGW_PACKAGE_PREFIX}-rocksdb")
optdepends=("${MINGW_PACKAGE_PREFIX}-libwebp"
            "${MINGW_PACKAGE_PREFIX}-rocksdb")
source=("https://github.com/gwaldron/osgearth/archive/${_realname}-${pkgver}.tar.gz"
        "https://github.com/Tencent/rapidjson/archive/v${_rj_ver}/rapidjson-v${_rj_ver}.tar.gz"
        "https://github.com/Esri/lerc/archive/${_lerc_commit}/lerc-${_lerc_commit}.tar.gz"
        "0002-cxx-standard-17.patch"
        "0003-osg-plugin-prefix.patch"
        "0101-rj-remove-non-compiling-assignment-operator.patch::https://patch-diff.githubusercontent.com/raw/Tencent/rapidjson/pull/719.patch")
sha256sums=('8bf241fa76e98ef3e072edb5da21ca213f7ef6be448905233e17eded60ccd71b'
            'bf7ced29704a1e696fbccf2a2b4ea068e7774fa37f6d7dd4039d0787f8bed98e'
            '11988ad44f4b63df7dde62bccde9b194cff468bf389912e3f3f7ebf06246c370'
            'ae80d6a24c6115a893373df572a0c47c7aedfe3913ffe2d247d510b9abc73f78'
            'b317d5e6cf9a57a34fa5cf2559de41b9498535cb10e15844c7c24a8bae487f56'
            '8abc5c5ef033801ea99a9f289126e473ccf4ebdb9ab33fa8157f957fdeb03a2f')

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

prepare() {
  cd "${srcdir}"/rapidjson-${_rj_ver}
  apply_patch_with_msg \
    0101-rj-remove-non-compiling-assignment-operator.patch
  
  cd "${srcdir}"/${_realname}-${_realname}-${pkgver}
  apply_patch_with_msg \
    0002-cxx-standard-17.patch \
    0003-osg-plugin-prefix.patch

  mkdir -p src/third_party/rapidjson/
  mv "${srcdir}"/rapidjson-${_rj_ver}/* src/third_party/rapidjson/
  mv "${srcdir}"/lerc-${_lerc_commit}/* src/third_party/lerc/
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/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 [[ ${CARCH} == aarch64 ]]; then
    CXXFLAGS+=(" -march=armv8.2-a")
  fi

  CXXFLAGS+=" -w -Wno-register" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DOSG_DIR="${MINGW_PREFIX}" \
    -DOSGEARTH_BUILD_EXAMPLES=OFF \
    -DOSGEARTH_BUILD_TESTS=OFF \
    -DOSGEARTH_BUILD_LEGACY_SPLAT_NODEKIT=ON \
    -DOSGEARTH_BUILD_ZIP_PLUGIN=OFF \
    -DOSGEARTH_PROTOBUF_FORCE_CONFIG_MODE=ON \
    -DCURL_NO_CURL_CMAKE=ON \
    -DMY_LIBRARIES="dbghelp" \
    ../${_realname}-${_realname}-${pkgver}

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

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

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/osgearth
  cp -rf "${srcdir}"/${_realname}-${_realname}-${pkgver}/data \
    "${pkgdir}${MINGW_PREFIX}"/share/osgearth

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/osgearth/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

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