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

pkgname=openssh
pkgver=10.5p1
pkgrel=1
pkgdesc='Free version of the SSH connectivity tools'
url='http://www.openssh.org/portable.html'
license=('custom:BSD')
arch=('i686' 'x86_64')
groups=('net-utils')
depends=('libfido2' 'heimdal' 'libedit' 'libxcrypt' 'openssl')
makedepends=('libfido2-devel' 'libcbor-devel' 'heimdal-devel' 'libedit-devel' 'libxcrypt-devel' 'openssl-devel')
source=("https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/${pkgname}-${pkgver}.tar.gz"{,.asc}
        0001-Forward-port-MSys2-patches.patch
        0002-openssh-work-around-Cygwin-declaring-setkey.patch
        0003-openssh-skip-privilege-separation-tests.patch
        0004-Allow-scp-to-copy-files-that-start-with-a-Windows-dr.patch
        0005-Disallow-interop-tests-and-extra-tests-to-run-concur.patch)
sha256sums=('d44d28a839ea9daf969cc69150fde59910b2b39361dad81a3bd6cbd19218db11'
            'SKIP'
            'ca1e4a8e6248cd4e36ece97005faff1cbd8bdfdda197be7194303a9e5209f492'
            '5d9a69b4e37dba1ecd87fa71a80743ba9563dd64b91698b1d667b6bcdac752ed'
            'bacb61023f5afb4c58fe19001e97572e8f8a2509d2654b100538ded28df7d2ee'
            'da2d432be272f21b7436c172b4197553300fbfb5863a032ece90acf44e24a80a'
            '101a92a25fd447e6f1efb0bd3e3dae96af90cdc7a239943500645d612bdf860a')
validpgpkeys=('59C2118ED206D927E667EBE3D3E5F56B6D920D30'
              '7168B983815A5EEF59A4ADFD2A3F414E736060BA')

backup=('etc/ssh/ssh_config' 'etc/ssh/sshd_config')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-Forward-port-MSys2-patches.patch
  patch -p1 -i ${srcdir}/0002-openssh-work-around-Cygwin-declaring-setkey.patch
  patch -p1 -i ${srcdir}/0003-openssh-skip-privilege-separation-tests.patch
  patch -p1 -i ${srcdir}/0004-Allow-scp-to-copy-files-that-start-with-a-Windows-dr.patch
  patch -p1 -i ${srcdir}/0005-Disallow-interop-tests-and-extra-tests-to-run-concur.patch
  autoreconf -fvi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  # The 32-bit MSYS2 runtime (3.3.x) exports arc4random_stir() in its
  # import library but does not declare it in <stdlib.h>, causing a build
  # failure with -Wimplicit-function-declaration. Tell configure the
  # function is absent so the existing no-op macro in defines.h kicks in.
  if [[ "$CARCH" == "i686" ]]; then
    export ac_cv_func_arc4random_stir=no
  fi

  TEST_SSH_UTF8=no \
  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib/ssh \
    --sysconfdir=/etc/ssh \
    --localstatedir=/var \
    --with-libedit \
    --with-kerberos5=/usr \
    --disable-strip \
    --with-security-key-builtin

  make
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make tests ||
  grep $USER /etc/passwd | grep -q /bin/false
  # connect.sh fails when run with stupid login shell
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  #ln -sf ssh.1.gz "${pkgdir}"/usr/share/man/man1/slogin.1.gz
  install -Dm644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENCE"

  install -Dm755 contrib/findssl.sh "${pkgdir}"/usr/bin/findssl.sh
  install -Dm755 contrib/ssh-copy-id "${pkgdir}"/usr/bin/ssh-copy-id
  install -Dm644 contrib/ssh-copy-id.1 "${pkgdir}"/usr/share/man/man1/ssh-copy-id.1

  #rm -f "${pkgdir}"/etc/ssh/sshd_config
  #rm -f "${pkgdir}"/usr/bin/sshd.exe
  #rm -f "${pkgdir}"/usr/sbin/sshd.exe
  #rm -f "${pkgdir}"/usr/lib/ssh/sftp-server.exe
}
