# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libebml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.4.1
pkgrel=1
pkgdesc="Extensible Binary Meta Language library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('LGPL')
url="https://matroska.org"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs')
source=("https://dl.matroska.org/downloads/${_realname}/${_realname}-${pkgver}.tar.xz"
        002-mingw-versioned-dll.patch)
sha256sums=('6e94c669405061aa0d25a523b9f1bea8ac73536e37721a110b3372c7f8717032'
            'b9697c3c8cba108cc76df6d943c51c4b2bc30092bd5fd6c60e8beff641361a71')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-mingw-versioned-dll.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/shared-${MINGW_CHOST} && cd ${srcdir}/shared-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/shared-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"

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