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

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6.0
pkgrel=1
pkgdesc="The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openssl-library.org'
msys2_repository_url="https://github.com/openssl/openssl"
msys2_references=(
  'archlinux: openssl'
  "cpe: cpe:/a:openssl:openssl"
)
license=("spdx:Apache-2.0")
optdepends=("${MINGW_PACKAGE_PREFIX}-ca-certificates")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
options=('!buildflags')
source=("https://github.com/openssl/openssl/releases/download/openssl-${pkgver}/openssl-${pkgver}.tar.gz"{,.asc}
        '002-relocation.patch'
        '003-no-set_printf_count_output.patch'
        'pathtools.c'
        'pathtools.h')
sha256sums=('b6a5f44b7eb69e3fa35dbf15524405b44837a481d43d81daddde3ff21fcbb8e9'
            'SKIP'
            'fdc83cb231ed89e9fea70bc82e487427be8eb07417c1bbc3f0aa8561e79de9f3'
            '90ff62d4f25f641d4eb06aa5400c2bc1ed92c3ad3b4040507b6a4323930c621c'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90')
# https://openssl-library.org/source/index.html
validpgpkeys=(
  'BA5473A2B0587B07FB27CF2D216094DFD0CB81EF' # openssl@openssl.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 "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
     002-relocation.patch \
     003-no-set_printf_count_output.patch

  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cp -fHv "${srcdir}"/pathtools.c crypto/
  cp -fHv "${srcdir}"/pathtools.h ./
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # Use mingw cflags instead of hardcoded ones
  sed -i -e '/^"mingw"/ s/-fomit-frame-pointer -O3 -Wall/-O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4/' \
    "${srcdir}"/${_realname}-${pkgver}/Configurations/10-main.conf

  case "${CARCH}" in
    i?86)
      _mingw=mingw
      ;;
    x86_64)
      _mingw=mingw64
      ;;
    aarch64)
      _mingw=mingwarm64
      ;;
  esac

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  /usr/bin/perl ../"${_realname}-${pkgver}"/Configure \
    --prefix="${MINGW_PREFIX}" \
    --libdir=lib \
    --openssldir=etc/ssl \
    ${_mingw} \
    shared \
    zlib-dynamic \
    enable-camellia \
    enable-capieng \
    enable-idea \
    enable-mdc2 \
    enable-rc5  \
    enable-rfc3779 \
    -DOPENSSLBIN=\"\\\"${MINGW_PREFIX}/bin\\\"\"

  MSYS2_ARG_CONV_EXCL="-DENGINESDIR=;-DMODULESDIR=;-DOPENSSLDIR=;-DINSTALLTOP=;-DOPENSSLBIN=" \
  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make VERBOSE=1 test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make -j1 install DESTDIR="${pkgdir}"

  # https://github.com/openssl/openssl/issues/24298
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${MINGW_PREFIX}|\${prefix}|g" -i "${pcfile}"
    sed -i "1s|^|prefix=${MINGW_PREFIX}\\n|" "${pcfile}"
  done

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