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

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
	"${MINGW_PACKAGE_PREFIX}-${_realname}-pdb")
pkgver=3.5.9
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' 'clang32' 'clangarm64')
url='https://www.openssl.org/'
license=("spdx:Apache-2.0")
optdepends=("${MINGW_PACKAGE_PREFIX}-ca-certificates")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
if [[ "$MSYSTEM" != CLANG* ]]; then
  makedepends+=("${MINGW_PACKAGE_PREFIX}-cv2pdb")
fi
options=('!strip' '!buildflags')
source=("https://github.com/openssl/openssl/releases/download/openssl-${pkgver}/openssl-${pkgver}.tar.gz"{,.asc}
        '0001-support-aarch64.patch'
        '0002-relocation.patch'
        '0003-test_rand-use-the-better-chomp.patch'
        '0004-mingw32-broken-test.patch'
        '0005-Fix-Text-comparison.patch'
        '0006-Mangle-Absolute-path.patch'
        '0007-Fix-OS-detection.patch'
        '0008-Define-SIO_UDP_NETRESET-if-necessary.patch'
        '0009-Also-treat-Cygwin-Perl-like-MSYS-Perl-in-test-skip-g.patch'
        'pathtools.c'
        'pathtools.h')
sha256sums=('603f5602e2eef00d77fbd429d34dcd5822bb301757a1bc9cdb24c670f1eb859a'
            'SKIP'
            '8503b2d481e8392a67152d529cfbb01cc9b9c4a8d7a529b202f3a1a385058c34'
            'e64661d67c47c1f8b5a409a230fd5a7a97c5b9070ceeefdc469788cf85068c36'
            '49f3855724820b3a543fe48ddd3fe3a3ad4a6e425e43934d46cce745a2b087d9'
            'd226be89fdd2d9b576d6e6167858973ca2c6f4c9f606e7685e4451483a7ee5e0'
            '7b536140d138d2af264ac23f3f9d2fae4c8226854c8b6a02b724b34c523ac5cb'
            '2785fc1ee7c908c8a9b0b5e18371ce1baf75f5684877bf388004b74009e6ecb6'
            '9a5c55ae51a8411034339ff9fc028cfb11337511957554b1681c1163160d82c8'
            '32a166a50fe916fb872be441bf7db1f56d67222b7b38ccfe5da394802f79eeac'
            '4f7c427058e5edc533c5c653ef5189e2b38998f4fb2e300f5db2675ed4bef5fb'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90')

# https://www.openssl.org/community/otc.html
validpgpkeys=(
  '8657ABB260F056B1E5190839D9C4D26D0E604491' # Matt Caswell <matt@openssl.org>
  '7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C' # Richard Levitte <richard@levitte.org>
  'A21FAB74B0088AA361152586B8EF1A6BA9DA2D5C' # Tomáš Mráz <tm@t8m.info>
  'B7C1C14360F353A36862E4D5231C84CDDCC69C45' # Paul Dale <pauli@openssl.org>
  'EFC0A467D613CB83C7ED6D30D894E2CE8B3D79F5' # OpenSSL Management Committee <openssl-omc@openssl.org>
  'BA5473A2B0587B07FB27CF2D216094DFD0CB81EF' # OpenSSL <openssl@openssl.org>
  'B146647E45A7B33947AB226B2A2C87D161692D40' # OpenSSL <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 \
     0001-support-aarch64.patch \
     0002-relocation.patch \
     0003-test_rand-use-the-better-chomp.patch \
     0005-Fix-Text-comparison.patch \
     0006-Mangle-Absolute-path.patch \
     0007-Fix-OS-detection.patch \
     0008-Define-SIO_UDP_NETRESET-if-necessary.patch \
     0009-Also-treat-Cygwin-Perl-like-MSYS-Perl-in-test-skip-g.patch

  if [[ "$MSYSTEM" == MINGW32 ]]
  then
    # MINGW32 gmtime() doesn't handle negative values particularly well, which breaks a test that expects a
    # negative value after converting an invalid ASN1 string to time_t but can't compare the expected result
    # with the actual result
    apply_patch_with_msg 0004-mingw32-broken-test.patch
  fi

  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 ./

  msg2 "Using sed to force makepkg-mingw CFLAGS instead of hardcoded ones"
  cd "${srcdir}/${_realname}-${pkgver}/Configurations"
  sed -i -e "/^[[:space:]]*\"mingw-common\"/,/release/ s/-O3/${CFLAGS}/" 10-main.conf
  grep -q --fixed-strings -- "${CFLAGS}" 10-main.conf  || { echo "...sed failed"; return 1; }
}

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

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

  STRIP_OPTS=

  if [[ "$MSYSTEM" == CLANG* ]]
  then
    export LDFLAGS="-Wl,-pdb=" # Ensures that Clang generates PDB files
    STRIP=llvm-strip
    STRIP_OPTS=--strip-debug
  else
    STRIP=cv2pdb
    sed -i -e '/^[[:space:]]*\"mingw-common\"/,/release/ s/-O/-g &/' \
      ${srcdir}/${_realname}-${pkgver}/Configurations/10-main.conf
  fi
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ../"${_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

  case "${pkgname[@]}" in
  *-pdb|*-pdb\ *)
    for file in apps/*.exe *.dll engines/*.dll
    do
      $STRIP $STRIP_OPTS "$file" || exit
    done

    # copy cv2pdb'ed versions of libeay32.dll and ssleay32.dll to apps/
    cp -f *.dll *.pdb apps/;;
  esac
}

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

  HARNESS_JOBS=${HARNESS_JOBS:-8} make VERBOSE_FAILURE=1 TESTS=-test_symbol_presence test
}

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

  test pdb != "$1" || {
    install -d -m 755 "${pkgdir}${MINGW_PREFIX}/bin"
    install -d -m 755 "${pkgdir}${MINGW_PREFIX}/lib/engines"
    install -m 755 *.pdb "${pkgdir}${MINGW_PREFIX}/bin"
    install -m 755 apps/openssl.pdb "${pkgdir}${MINGW_PREFIX}/bin"
    install -m 755 engines/*.pdb "${pkgdir}${MINGW_PREFIX}/lib/engines"
    return
  }

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

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

package_mingw-w64-i686-openssl() {
  _package
}

package_mingw-w64-x86_64-openssl() {
  _package
}

package_mingw-w64-ucrt-x86_64-openssl() {
  _package
}

package_mingw-w64-clang-aarch64-openssl() {
  _package
}

package_mingw-w64-i686-openssl-pdb() {
  _package pdb
}

package_mingw-w64-x86_64-openssl-pdb() {
  _package pdb
}

package_mingw-w64-ucrt-x86_64-openssl-pdb() {
  _package pdb
}

package_mingw-w64-clang-aarch64-openssl-pdb() {
  _package pdb
}
