# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=libaec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.5
pkgrel=2
pkgdesc="Adaptive Entropy Coding library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gitlab.dkrz.de/k202009/libaec"
license=('BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-crt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
provides=("${MINGW_PACKAGE_PREFIX}-szip")
replaces=("${MINGW_PACKAGE_PREFIX}-szip")
options=('staticlibs' 'strip')
_md5=eb02f9727d538283d1730fdf8fd85665
source=("${url}/uploads/${_md5}/${_realname}-${pkgver}.tar.gz"
        "0002-build-tests-off.patch"
        "0004-mingw-lib-names.patch"
        "0005-cmake-fix-cmake-install.patch")
sha256sums=('2a6b5393c4697b167e81b3c12008fc6334f8778a37587d67faba214640e5ed36'
            '34553ed67ecffe86f295754c6dda0f00ffd6e8e89fc2b4784fd59aff32b2d391'
            '46bc1f198a9085f19195ad87b2ad27f91ae3e9e85a30dbe7e21782197c99126a'
            '9acc599a0f809b1f196c2ff0db6990901e812e659a594041e7540c4980b3fc91')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0002-build-tests-off.patch
  patch -p1 -i ${srcdir}/0004-mingw-lib-names.patch
  patch -p1 -i ${srcdir}/0005-cmake-fix-cmake-install.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_ARCH} ]] && rm -rf ${srcdir}/build-${MINGW_ARCH}
  mkdir -p ${srcdir}/build-${MINGW_ARCH} && cd ${srcdir}/build-${MINGW_ARCH}

  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 \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

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

package() {
  cd ${srcdir}/build-${MINGW_ARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
