# Maintainer: James Ross-Gowan <rossymiles@gmail.com>

pkgbase=protobuf
pkgname=('protobuf' 'protobuf-devel')
pkgver=21.12
pkgrel=3
pkgdesc="Protocol Buffers - Google's data interchange format"
arch=('i686' 'x86_64')
url='https://developers.google.com/protocol-buffers/'
msys2_repository_url="https://github.com/protocolbuffers/protobuf"
msys2_references=(
  "cpe: cpe:/a:google:google-protobuf"
  "cpe: cpe:/a:google:protobuf"
)
groups=('libraries')
license=('spdx:BSD-3-Clause')
depends=('gcc-libs' 'zlib')
makedepends=('zlib-devel' 'autotools' 'gcc')
source=(protobuf-${pkgver}.tar.gz::https://github.com/protocolbuffers/protobuf/archive/v${pkgver}.tar.gz
        0001-constant-initializer.patch # based on https://github.com/protocolbuffers/protobuf/commit/ddf2c6b6d0f3c73afac13bdfce9a8f626f9fd6d2
        )
sha256sums=('22fdaf641b31655d4b2297f9981fa5203b2866f8332d3c6333f6b0107bb320de'
            '39629ddb18fa890686755515096705ab7d0301f4f8eb05b29a24d5543ea31343')
noextract=("protobuf-${pkgver}.tar.gz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  [[ -d ${srcdir}/protobuf-${pkgver} ]] && rm -rf ${srcdir}/protobuf-${pkgver}
  tar -xzf "${srcdir}/protobuf-${pkgver}.tar.gz"

  cd  ${srcdir}/protobuf-${pkgver}
  apply_patch_with_msg \
    0001-constant-initializer.patch

  ./autogen.sh
}

build() {
  cd "${srcdir}/protobuf-${pkgver}"

  ./configure \
    --host=${CHOST} \
    --prefix=/usr
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}/protobuf-${pkgver}"
  make check
}

package_protobuf() {
  mkdir -p "${pkgdir}/usr/bin"
  cp -f "${srcdir}/dest/usr/bin/"*.dll "${pkgdir}/usr/bin/"
  install -Dm644 "${srcdir}/protobuf-${pkgver}/LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

package_protobuf-devel() {
  pkgdesc="Protobuf headers and libraries"
  groups=('development')
  depends=("protobuf=${pkgver}")

  mkdir -p "${pkgdir}/usr/bin"
  cp -f "${srcdir}/dest/usr/bin/"*.exe "${pkgdir}/usr/bin/"
  cp -rf "${srcdir}/dest/usr/include" "${pkgdir}/usr/"
  cp -rf "${srcdir}/dest/usr/lib" "${pkgdir}/usr/"
}
