# Maintainer: Jianfeng Liu <liujianfeng1994@gmail.com>
# Contributor: Olivier Michel <Olivier.Michel@cyberbotics.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=libzip
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.3
pkgrel=1
pkgdesc="A C library for reading, creating, and modifying zip archives (mingw-w64)"
url="https://libzip.org"
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-nettle"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' '!libtool')
source=("https://libzip.org/download/${_realname}-${pkgver}.tar.xz"
        "0001-cmake-disable-_snprintf-check.patch")
sha512sums=('488866348a0c859f6f995c50e956bd51b4105278f07b63fe019cb44ab36c09ef958cc0684fd5444c474d83874b999e8ed1aa9b2da51b74caa37bbe7934b9c870'
            '96401fa51efb23f9ee608e8db1ae0cb47ac5851ab8a94c68023ddd7b4b0e4694d0ff4f76dd00a864c78dc3b21759eee4513bf021a93e39919ea0ff00130f1116')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Nbp1 -i "${srcdir}/0001-cmake-disable-_snprintf-check.patch"
}

build() {
  # Shared Build
  [[ -d ${srcdir}/build-${CARCH}-shared ]] && rm -rf ${srcdir}/build-${CARCH}-shared
  mkdir -p ${srcdir}/build-${CARCH}-shared && cd ${srcdir}/build-${CARCH}-shared

  cd "${srcdir}/build-${CARCH}-shared"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_COMMONCRYPTO=OFF \
    -DENABLE_GNUTLS=ON \
    -DENABLE_OPENSSL=OFF \
    -DENABLE_MBEDTLS=OFF \
    ../${_realname}-${pkgver}

  make
  
  # Static Build
  [[ -d ${srcdir}/build-${CARCH}-static ]] && rm -rf ${srcdir}/build-${CARCH}-static
  mkdir -p ${srcdir}/build-${CARCH}-static && cd ${srcdir}/build-${CARCH}-static

  cd "${srcdir}/build-${CARCH}-static"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_COMMONCRYPTO=OFF \
    -DENABLE_GNUTLS=ON \
    -DENABLE_OPENSSL=OFF \
    -DENABLE_MBEDTLS=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  # Shared Install
  cd "${srcdir}/build-${CARCH}-shared"
  make DESTDIR="${pkgdir}" install
  
  # Static Install
  cd "${srcdir}/build-${CARCH}-static"
  make DESTDIR="${pkgdir}" install

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