# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=liblas
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.1
pkgrel=6
pkgdesc="ASPRS LiDAR data translation toolset (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://liblas.org/"
msys2_repository_url="https://github.com/libLAS/libLAS"
msys2_references=(
  'cpe: cpe:/a:liblas:liblas'
)
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         #"${MINGW_PACKAGE_PREFIX}-laszip"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff")
source=("https://github.com/libLAS/libLAS/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-1.8.1.patch"
        "liblas-upstream-fixes.patch"
        "support-aarch64.patch")
sha256sums=('31cff4fd235f9397df8c3086b0aefc50a047d2be0e9ce39f0cddfc662633fa93'
            '1a04a04ee064e54a86b229826336d34239ec6e178c23b67b57bba01933df8592'
            'cbcb261f4bf4b2f9f2c82810f66791ebcb4f11008333becf7de7219c57ca521b'
            '278d743d52ec062dd6e03d403876c6c1c229ba56ceb311474c6828b9c8f8387b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-1.8.1.patch"
  patch -Np1 -i "${srcdir}/liblas-upstream-fixes.patch"
  patch -Np1 -i "${srcdir}/support-aarch64.patch"
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_OSGEO4W=ON \
    -DWITH_LASZIP=ON \
    -DWITH_PKGCONFIG=ON \
    -DWITH_TESTS=OFF \
    "../${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake ../${_realname}-${pkgver} -DWITH_TESTS=ON
  ${MINGW_PREFIX}/bin/cmake --build .
  GDAL_DATA="${MINGW_PREFIX}/share/gdal" ${MINGW_PREFIX}/bin/ctest
}

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

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