# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=minizip-ng
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.10
pkgrel=1
pkgdesc="Fork of the popular zip manipulation library found in the zlib distribution. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: minizip-ng'
  "cpe: cpe:2.3:a:zlib-ng:minizip-ng"
)
url='https://github.com/zlib-ng/minizip-ng/'
license=('spdx:Zlib')
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=(https://github.com/zlib-ng/minizip-ng/archive/${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('c362e35ee973fa7be58cc5e38a4a6c23cc8f7e652555daf4f115a9eb2d3a6be7')

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

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

  LDFLAGS+=" -Wl,--export-all-symbols" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DMZ_BZIP2=ON \
      -DMZ_LZMA=ON \
      -DMZ_OPENSSL=ON \
      -DMZ_PKCRYPT=ON \
      -DMZ_WZAES=ON \
      -DMZ_ZLIB=ON \
      -DMZ_ZSTD=ON \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DMZ_FETCH_LIBS=OFF \
      -DMZ_PROJECT_SUFFIX="-ng" \
      -DMZ_LIB_SUFFIX="-ng" \
      ../${_realname}-${pkgver}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DMZ_BZIP2=ON \
      -DMZ_LZMA=ON \
      -DMZ_OPENSSL=ON \
      -DMZ_PKCRYPT=ON \
      -DMZ_WZAES=ON \
      -DMZ_ZLIB=ON \
      -DMZ_ZSTD=ON \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DMZ_FETCH_LIBS=OFF \
      -DMZ_PROJECT_SUFFIX="-ng" \
      -DMZ_LIB_SUFFIX="-ng" \
      ../${_realname}-${pkgver}

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

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

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm0644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
