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

_realname=libssh2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-wincng")
pkgver=1.11.1
pkgrel=1
pkgdesc="A library implementing the SSH2 protocol as defined by Internet Drafts (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libssh2.org/"
msys2_references=(
  'cpe: cpe:/a:libssh2:libssh2'
)
msys2_repository_url='https://github.com/libssh2/libssh2'
license=("spdx:BSD-3-Clause")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://libssh2.org/download/${_realname}-${pkgver}.tar.gz"{,.asc})
sha256sums=('d9ec76cbe34db98eec3539fe2c899d26b0c837cb3eb466a56b0f109cabf658f7'
            'SKIP')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2'   # Daniel Stenberg
              '914C533DF9B2ADA2204F586D78E11C6B279D5C91')  # Daniel Stenberg (old key)

prepare() {
  cd ${_realname}-${pkgver}
  autoreconf -fi
}

_do_build() {
  builddir="${srcdir}/build-${MSYSTEM}-$1"
  mkdir -p "${builddir}" && cd "${builddir}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-rpath \
    --with-crypto=$1

  make
}

build() {
  _do_build openssl
  _do_build wincng
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-openssl"
  make check || true

  cd "${srcdir}/build-${MSYSTEM}-wincng"
  make check || true
}

_do_package() {
  cd "${srcdir}/build-${MSYSTEM}-$1"
  make DESTDIR="${pkgdir}" install

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

package_libssh2() {
  depends=("${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-zlib")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-wincng")
  _do_package openssl
}

package_libssh2-wincng() {
  depends=("${MINGW_PACKAGE_PREFIX}-zlib")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  _do_package wincng
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
