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

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver=1.1.1k
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates" "${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' '!buildflags' 'staticlibs')
license=('custom:BSD')
url="https://www.openssl.org"
noextract=(${_realname}-${_ver}.tar.gz)
source=(https://www.openssl.org/source/${_realname}-${_ver}.tar.gz{,.asc}
        'openssl-1.1.1-relocation.patch'
        'openssl-1.1.1-mingw-arm.patch')
sha256sums=('892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5'
            'SKIP'
            '798d1b4dec28429d6cf2081c5b5b23b4344590a9b812e8089842389dbd175b77'
            'd41fad88631e7b8d2a56662f2166ea97ecbc6369f2ad3eac415182bc9ac9f308')

validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491'
              '7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C')

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

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  # Clean up old sources so re-patching doesn't fail.
  [[ -d ${srcdir}/${_realname}-${_ver} ]] && rm -rf ${srcdir}/${_realname}-${_ver}
  tar -xzvf ${_realname}-${_ver}.tar.gz -C ${srcdir} || true
  cd ${srcdir}/${_realname}-${_ver}

  apply_patch_with_msg \
     openssl-1.1.1-relocation.patch \
     openssl-1.1.1-mingw-arm.patch
}

build() {
  rm -rf ${srcdir}/build-${CARCH}

  # No support for out-of-source builds
  mkdir -p ${srcdir}/build-${CARCH}
#  cp -a ${srcdir}/${_realname}-${_ver}/* ${srcdir}/build-${CARCH}

  # 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}-${_ver}/Configure
#     ${srcdir}/build-${CARCH}/Configure

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

  cd "${srcdir}/build-${CARCH}"
  export MSYS2_ARG_CONV_EXCL="--prefix="
  ${srcdir}/${_realname}-${_ver}/Configure \
    --prefix=${MINGW_PREFIX} \
    --openssldir=ssl \
    ${_mingw} \
    shared \
    zlib-dynamic \
    enable-camellia \
    enable-capieng \
    enable-idea \
    enable-mdc2 \
    enable-rc5  \
    enable-rfc3779 \
    -D__MINGW_USE_VC2005_COMPAT \
    -DOPENSSLBIN=\"\\\"${MINGW_PREFIX}/bin\\\"\"

  make ZLIB_INCLUDE=-I"${MINGW_PREFIX}"/include depend all
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make VERBOSE=1 test
}

package() {
  cd "${srcdir}/build-${CARCH}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/bin
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/include/openssl
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}

  make -j1 DESTDIR="${pkgdir}" MANDIR="${MINGW_PREFIX}/share/man" MANSUFFIX=ssl install
  install -D -m644 "${srcdir}/${_realname}-${_ver}/LICENSE" \
         "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/bin
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1

  install -m644 "${srcdir}/${_realname}-${_ver}/ms/applink.c" "${pkgdir}${MINGW_PREFIX}"/include/openssl/
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/c_rehash
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{ssl/misc,ssl/man}
}
