# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libwebm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.0.32
pkgrel=1
pkgdesc="WebM video file parser (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://chromium.googlesource.com/webm/libwebm"
msys2_references=(
  "cpe: cpe:/a:webmproject:libwebm"
)
msys2_ignore_vulnerabilities=(
  "CVE-2018-6406"  # already fixed: https://github.com/webmproject/libwebm/commit/8e88e04b07352f2ca
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://github.com/webmproject/${_realname}/archive/${_realname}-${pkgver}.tar.gz
        libwebm.pc)
sha256sums=('7fd5e085bda9f8031cf2ad2a1e52d9b7b29cba9c0b96ad2ce794ce89e4249eb8'
            '082aea8e45bf42faba18e00d00561d4b5aba307261df74c18238fcecc792cc1f')

prepare() {
  cd "${srcdir}"/${_realname}-${_realname}-${pkgver}
}

build() {
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_WEBM_PARSER=ON \
    ../${_realname}-${_realname}-${pkgver}

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

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_WEBM_PARSER=ON \
    ../${_realname}-${_realname}-${pkgver}

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

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

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

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

  cd "${srcdir}"
  sed -i "s|@PREFIX@|${MINGW_PREFIX}|g" libwebm.pc
  sed -i "s|@VERSION@|${pkgver}|g" libwebm.pc
  install -Dm644 libwebm.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libwebm.pc"
}
