# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=flatbuffers
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.12.0
pkgrel=2
pkgdesc='Memory Efficient Serialization Library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://google.github.io/flatbuffers/'
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/${_realname}/archive/v${pkgver}.tar.gz"
        "${_realname}-1.9.0.patch")
sha256sums=('62f2223fb9181d1d6338451375628975775f7522185266cd5296571ac152bc45'
            '67bf9306dfff8652a81695f96dec8ec0eb6b6e5a57e2b29b4dc0fe5caba16545')
options=('strip' '!libtool' 'staticlibs')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  cd "${srcdir}"
  tar -xvf ${_realname}-${pkgver}.tar.gz --exclude='CONTRIBUTING.md'
  cd "${_realname}-${pkgver}"
  patch -Np1 -i "../${_realname}-1.9.0.patch"

  # Remove Werror flag
  sed -i 's/-Werror=/-W/g;s/-Werror//g' CMakeLists.txt
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DFLATBUFFERS_BUILD_SHAREDLIB=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"
  install -Dm755 flatc.exe -t "${pkgdir}${MINGW_PREFIX}"/bin

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