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

_realname=libmatroska
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.6.2
pkgrel=1
pkgdesc="Matroska library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('LGPL')
url="https://matroska.org"
depends=("${MINGW_PACKAGE_PREFIX}-libebml")
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=('bc4479aa8422ab07643df6a1fa5a19e4bed4badfd41ca77e081628620d1e1990'
            '982c95e45e4b4b4b6dbf7ab447893a14e8f473e6475dd306442f030fbf3376ff')

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
}
