# Obarun        : 66 init/supervisor
# Maintainer    : Eric Vidal <eric@obarun.org>
# Maintainer    : Jean-Michel T.Dydak <jean-michel@obarun.org>
# Maintainer    : YianIris <yianiris At disroot Dot org>
# Source        : url="https://git.obarun.org/pkg/obcore/openssh"
#----------------
# Website       : https://www.openssh.org/portable.html/
#-----------------------------------------------------------------------

pkgname=openssh

pkgdesc='Premier connectivity tool for remote login with the SSH protocol'

pkgver=8.8p1
pkgrel=2

url="https://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable"

track=branch
target="${pkgname}-${pkgver}.tar.gz"
source=(
    "${url}/${target}"
    'sshd.pam'
    'sshd.conf'
    'glibc-2.31.patch'
    )

#--BUILD CONFIGURATION--------------------------------------------------

makedepends=(
    'linux-headers' 'git' 'autoconf' 'automake')

#--BUILD CONTROL--------------------------------------------------------

path=(
    --prefix=/usr
    --sbindir=/usr/bin
    --libexecdir=/usr/lib/ssh
    --sysconfdir=/etc/ssh
)

flags=(
    --disable-strip
    --with-ldns
    --with-libedit
    --with-ssl-engine
    --with-pam
    --with-privsep-user=nobody
    --with-kerberos5=/usr
    --with-xauth=/usr/bin/xauth
    --with-md5-passwords
    --with-pid-dir=/run
    --with-default-path=/usr/local/sbin:/usr/local/bin:/usr/bin
    --disable-strip
)

#--BUILD PREPARATION----------------------------------------------------

prepare() {
    cd "${pkgname}-${pkgver}"

    patch -p1 -i "${srcdir}/glibc-2.31.patch"

    autoreconf
}

#--BUILD----------------------------------------------------------------

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

    ./configure "${path[@]}" "${flags[@]}"
    make
}

#--CHECK----------------------------------------------------------------

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

    ## Tests require openssh to be already installed system-wide,
    ## also connectivity tests will fail under makechrootpkg since
    ## it runs as nobody which has /bin/false as login shell.

    if [[ -e /usr/bin/scp && ! -e /.arch-chroot ]]; then
        make tests || true
    fi

}

#--PACKAGE--------------------------------------------------------------

package() {
    cd "${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 -Dm644 ../sshd.conf "${pkgdir}"/usr/lib/tmpfiles.d/sshd.conf

    install -Dm644 ../sshd.pam "${pkgdir}"/etc/pam.d/sshd

    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

    sed \
        -e '/^#ChallengeResponseAuthentication yes$/c ChallengeResponseAuthentication no' \
        -e '/^#PrintMotd yes$/c PrintMotd no # pam does that' \
        -e '/^#UsePAM no$/c UsePAM yes' \
        -i "${pkgdir}"/etc/ssh/sshd_config
}

#--INSTALL CONFIGURATION------------------------------------------------

arch=('x86_64')

checkdepends=('inetutils')

depends=(
    'krb5'
    'openssl'
    'libedit'
    'ldns')

optdepends=(
    'xorg-xauth: X11 forwarding'
    'x11-ssh-askpass: input passphrase in X'
    'libfido2: FIDO/U2F support')

backup=(
    'etc/ssh/ssh_config'
    'etc/ssh/sshd_config'
    'etc/pam.d/sshd')

#--SECURITY AND LICENCE-------------------------------------------------

license=('custom:BSD')

sha512sums=('d44cd04445f9c8963513b0d5a7e8348985114ff2471e119a6e344498719ef40f09c61c354888a3be9dabcb5870e5cbe5d3aafbb861dfa1d82a4952f3d233a8df' #  openssh-8.8p1.tar.gz
            '298e47a21c337101974fa5237b3110aa3c7638b5fa53bd07661413236c8ed3212b431abaeffd875af6c9a72b4f8e1c8512e1e1960cbfff15bfee62b32d305fc3' #  sshd.pam
            '838252ca7bfa69797cba8e31340321ece06a58428b47002cb835678d8e29c6f23f6521269a2b5b17820860a56df308185bc651484db24327cc58191e5f83222a' #  sshd.conf
            'aeee097dbf46aa22f411d78a49b9db7b2451dd0351bb5f57c7b3159c7ddc29aa994cd43046c187033c474f8fb2952165536197139eb7c495db99f6697f41637d') #  glibc-2.31.patch
