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

pkgname=('openssl' 'libopenssl' 'openssl-devel' 'openssl-docs')
_ver=1.1.1l
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=1
pkgdesc='The Open Source toolkit for Secure Sockets Layer and Transport Layer Security'
arch=('i686' 'x86_64')
url='https://www.openssl.org'
license=('custom:BSD')
depends=('zlib')
makedepends=('gcc' 'tar' 'perl' 'zlib-devel' 'diffutils' 'nasm')
options=('!makeflags' 'staticlibs')
noextract=(${pkgname}-${_ver}.tar.gz)
source=("https://www.openssl.org/source/${pkgname}-${_ver}.tar.gz"{,.asc}
        '0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch'
        '0002-Support-MSYS2.patch'
        '0003-tls13secretstest-work-around-multiple-definition-of.patch'
        '0004-Override-engines-directory.patch')
sha256sums=('0b7a3e5e59c34827fe0c3a74b7ec8baef302b98fa80088d7f9153aa16fa76bd1'
            'SKIP'
            '35f5508bc3e651bb2ef1bf9f4e052f531aeaefbbdeff95b9746fff71137fff31'
            '44038ce01db03b43e90420c229d97a37be81c48183fd8e061e42b41799953acd'
            'a83dcfeec646724f081f8ff89beccee83c962758aa74d724f263138aa21b2438'
            '79765cc10799691ae47880556255287c64b8c1f26ea3907ad2e0add97bc9f1fc')
validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491')

prepare() {
  [[ -d ${srcdir}/${pkgname}-${_ver} ]] || tar -xzvf ${srcdir}/${pkgname}-${_ver}.tar.gz -C ${srcdir}
  cd ${srcdir}/${pkgname}-${_ver}

  patch -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
  patch -p1 -i ${srcdir}/0002-Support-MSYS2.patch
  patch -p1 -i ${srcdir}/0003-tls13secretstest-work-around-multiple-definition-of.patch
  patch -p1 -i ${srcdir}/0004-Override-engines-directory.patch
}

build() {
  cd ${srcdir}/${pkgname}-${_ver}

  if [ "${CARCH}" == 'x86_64' ]; then
    openssltarget='Msys-x86_64'
  elif [ "${CARCH}" == 'i686' ]; then
    openssltarget='Msys'
  fi

  ./Configure \
    --prefix=/usr \
    --openssldir=/usr/ssl \
    --libdir=lib \
    shared \
    zlib \
    no-idea \
    no-rc5 \
    ${openssltarget}

  make depend
  make all
  make DESTDIR=${srcdir}/dest MANDIR=/usr/share/man MANSUFFIX=ssl install
}

check() {
  cd ${srcdir}/${pkgname}-${_ver}

  # the test would write test data into /usr/ssl, which we do not want
  patch -R -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
  make test
  patch -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
}

package_openssl() {
  depends=('libopenssl' 'zlib')
  optdepends=('ca-certificates' 'perl')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/c_rehash ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man1 ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man5 ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man7 ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/ssl ${pkgdir}/usr/
}

package_openssl-docs() {
  mkdir -p ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man3 ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share
}

package_libopenssl() {
  depends=('zlib')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/

  mkdir -p ${pkgdir}/usr/lib/openssl
  cp -rf ${srcdir}/dest/usr/lib/openssl/engines-1.1 ${pkgdir}/usr/lib/openssl/
  chmod -R 755 ${pkgdir}/usr/lib/openssl/engines-1.1

  install -D -m644 ${srcdir}/openssl-${_ver}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_openssl-devel() {
  pkgdesc="Openssl headers and libraries"
  groups=('development')
  depends=("libopenssl=${pkgver}" 'zlib-devel')

  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib
}
