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

_realname=zziplib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.13.80
pkgrel=1
pkgdesc="A lightweight library that offers the ability to easily extract data from files archived in a single zip file (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://zziplib.sourceforge.net/"
msys2_repository_url="https://github.com/gdraheim/zziplib"
msys2_references=(
  "cpe: cpe:/a:zziplib:zziplib"
  "cpe: cpe:/a:zziplib_project:zziplib"
)
license=("spdx:MPL-1.1 OR LGPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "zip")
optdepends=("${MINGW_PACKAGE_PREFIX}-SDL: SDL_rwops for ZZipLib")
source=("https://github.com/gdraheim/zziplib/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "010-extern.patch"
        "030-cmake-remove-libs-number.patch")
sha256sums=('21f40d111c0f7a398cfee3b0a30b20c5d92124b08ea4290055fbfe7bdd53a22c'
            '14ab1fd20ab53ddf651dc858d1c6a41c851329eb52578f96e226e60fb2d372bf'
            '79d55dbc9b4f4c9f018b8569e14d52ba94087abfa89b8da624707d957ef73042')

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

  patch -p1 -i "${srcdir}"/010-extern.patch
  patch -p1 -i "${srcdir}"/030-cmake-remove-libs-number.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-implicit-function-declaration"

  echo "Building static library"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DZZIPTEST=OFF \
      -DZZIP_LIBLATEST=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-static

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

  echo "Building shared library"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DZZIP_LIBLATEST=OFF \
      -DZZIPTEST=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-shared

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

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

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

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/docs/COPYING.LIB \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/docs/COPYING.MPL \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MPL
}
