# Maintainer: Bruno Pagani <archange@archlinux.org>

pkgname=liblas
pkgver=1.8.1.r128+gded46373
pkgrel=17
pkgdesc="C/C++ library for reading and writing the very common LAS LiDAR format"
arch=(x86_64 aarch64)
url="https://liblas.org/"
license=(BSD)
depends=(boost-libs gdal laszip2 libgeotiff proj)
makedepends=(cmake boost git)
#source=(https://download.osgeo.org/liblas/libLAS-${pkgver}.tar.bz2)
#md5sums=('2e6a975dafdf57f59a385ccb87eb5919')
#sha256sums=('9adb4a98c63b461ed2bc82e214ae522cbd809cff578f28511122efe6c7ea4e76')
source=(git+https://github.com/libLAS/libLAS.git#commit=ded463732db1f9baf461be6f3fe5b8bb683c41cd
        fix-gdal-2.5.patch
        liblas-gcc15.patch
        boost-1.89.patch)
sha256sums=('699e26fcbbb781e357512538fce425e4a66c5dd2d5c03697008183c78ede9d78'
            'c492dd229664c0f1be1a7ca7a7e2df0414a04e2ac89dc5b5ce61463fbe84a6af'
            'b058d31eb27ad1fd454468337cb52c18e53761b6b64b4be86df9d5d21c76a03f'
            'ca933a36fc91487da51e9368d6d766b37d619b1741b540952155c974293b0239')

pkgver() {
  cd libLAS
  git describe --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./;s/-/+/'
}

prepare() {
  cd libLAS
  # Fix build with gdal 3.12
  git cherry-pick -n 9b175e067cebaac8d012d1d37c9cea4e8d8de1d9
  # https://github.com/libLAS/libLAS/issues/164
  patch -Np1 -i ../fix-gdal-2.5.patch
  # Fix build with GCC 15 (Fedora)
  patch -p1 -i ../liblas-gcc15.patch
  # Find our laszip 2.x version
  sed -i 's@laszip/@laszip2/@g' cmake/modules/FindLASzip.cmake
  sed -i 's@laszip/@laszip2/@g' src/header.cpp
  sed -i 's@laszip/@laszip2/@g' src/version.cpp
  sed -i 's@laszip/@laszip2/@g' src/detail/zippoint.cpp
  sed -i 's@laszip/@laszip2/@g' src/detail/reader/zipreader.cpp
  sed -i 's@laszip/@laszip2/@g' src/detail/writer/zipwriter.cpp
  # Fix build with boost 1.89
  patch -p1 -i ../boost-1.89.patch
}

build() {
  #cmake -B build -S libLAS-${pkgver} \
  cmake -B build -S libLAS \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DWITH_GDAL=ON \
    -DWITH_GEOTIFF=ON \
    -DWITH_LASZIP=ON \
    -DWITH_UTILITIES=ON \
    -DCMAKE_INSTALL_RPATH=/usr/lib/laszip2 \
    -DLASZIP_INCLUDE_DIR=/usr/include \
    -DLASZIP_LIBRARY=/usr/lib/laszip2/liblaszip.so \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -G "Unix Makefiles"
  make -C build
}

package() {
  make -C build DESTDIR="${pkgdir}" install
  #install -Dm644 libLAS-${pkgver}/LICENSE.txt -t "${pkgdir}"/usr/share/licenses/${pkgname}
  install -Dm644 libLAS/LICENSE.txt -t "${pkgdir}"/usr/share/licenses/${pkgname}
}
