# Maintainer: Norbert Pfeiler <norbert.pfeiler@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=snappy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.2
pkgrel=2
pkgdesc="A fast compressor/decompressor library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/google/snappy"
msys2_references=(
  "cpe: cpe:2.3:a:google:snappy"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-zlib"
              "${MINGW_PACKAGE_PREFIX}-lzo2")
source=("https://github.com/google/snappy/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-add-pkgconfig.patch"
        "0002-remove-Werror-flag.patch"
        "snappy.pc.in"
        "cmake-dual-libs.patch")
sha256sums=('90f74bc1fbf78a6c56b3c4a082a05103b3a56bb17bca1a27e052ea11723292dc'
            '2a204d7d35509ffc290d80c641fcb7e8488c67ca550aa1695493cff12c53156b'
            'a687b1a6e07e508bf43b0cce8059c1ddba74195b4b7b822643a0d06d3083c46d'
            '134f06ca0584a1026538d0fb972fc141c008390ecae1806184b721eca1abbc75'
            '37caf9737f58cdce95cdeb2c2c69a4292211750efe366a5fa4b3b61c5cb6e035')

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

  # upstream dropped the pkgconfig file and isn't interested in adding it back.
  # https://github.com/google/snappy/pull/55.patch
  cp -v "${srcdir}/snappy.pc.in" ./
  patch -p1 -i "${srcdir}/0001-cmake-add-pkgconfig.patch"

  patch -p1 -i "${srcdir}/0002-remove-Werror-flag.patch"
  patch -p1 -i "${srcdir}/cmake-dual-libs.patch"
}

build() {
  # Shared Build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DSNAPPY_BUILD_TESTS=OFF \
      -DSNAPPY_BUILD_BENCHMARKS=OFF \
      ../${_realname}-${pkgver}

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

  # Static Build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DSNAPPY_BUILD_TESTS=OFF \
      -DSNAPPY_BUILD_BENCHMARKS=OFF \
      ../${_realname}-${pkgver}

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

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

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