# Maintainer: Ilya Rakhlin <i.rakhlin@gmail.com>

_realname=libressl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.0
pkgrel=1
pkgdesc="Free version of the TLS/crypto stack forked from OpenSSL (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.libressl.org'
msys2_references=(
  "cpe: cpe:/a:openbsd:libressl"
)
msys2_repository_url="https://github.com/libressl/portable"
license=('spdx:ISC AND OpenSSL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
optdepends=("${MINGW_PACKAGE_PREFIX}-ca-certificates")
options=('strip')
source=("https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/${_realname}-${pkgver}.tar.gz"{,.asc}
        "0001-relocation.patch"
        "0002-add-prefix.patch"
        "pathtools."{c,h})
sha256sums=('0f71c16bd34bdaaccdcb96a5d94a4921bfb612ec6e0eba7a80d8854eefd8bb61'
            'SKIP'
            'cb104e1463d47f5347d7980b090bb123f1b3c52038cf58d9b6030a8fb9e037d7'
            'e17ede3e4ccb4fd990cc5375834c91117e5cf1e483b63eb7ceea2110a30b6bfb'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90')
validpgpkeys=('A1EB079B8D3EB92B4EBD3139663AF51BD5E4D8D5') # Brent Cook <bcook@openbsd.org>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"; do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}
# =========================================== #

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

  apply_patch_with_msg \
    0001-relocation.patch \
    0002-add-prefix.patch
  echo crypto tls | xargs -n 1 cp "${srcdir}/pathtools."{c,h} -fHvt
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DOPENSSLDIR=" \
    cmake -G "Ninja" \
      -B "build-${MSYSTEM}" \
      -S "${_realname}-${pkgver}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_INCLUDEDIR=include/${_realname} \
      -DCMAKE_INSTALL_LIBDIR=lib/${_realname} \
      -DCMAKE_INSTALL_LIBEXECDIR=libexec/${_realname} \
      -DBUILD_SHARED_LIBS=ON \
      -DLIBRESSL_INSTALL_CMAKEDIR=lib/cmake/LibreSSL \
      -DOPENSSLDIR=${MINGW_PREFIX}/etc/${_realname} \
      "${extra_config[@]}"

  MSYS2_ARG_CONV_EXCL="-DOPENSSLBIN=;-DOPENSSLDIR=;-DTLS_DEFAULT_CA_FILE=" \
    cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"
  install -Dm644 "${_realname}-${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  # Resolve naming conflicts with OpenSSL
  find "${pkgdir}${MINGW_PREFIX}/share/man" -type f -print0 | xargs -0 rename -v '' "${_realname}-"
}
