# Template file for 'openssh'
_desc="The OpenSSH implementation of SSH protocol"

pkgname=openssh
version=6.6p1
revision=11
build_style=gnu-configure
configure_args="--without-tcp-wrappers --datadir=/usr/share/openssh
 --sysconfdir=/etc/ssh --without-selinux --with-privsep-user=nobody
 --with-mantype=man --without-rpath --with-xauth=/usr/bin/xauth
 --with-ssl-engine --disable-strip --with-privsep-path=/var/chroot/ssh
 --with-pid-dir=/run --with-pam --with-pie --without-kerberos5 LD=$CC"
hostmakedepends="automake libtool perl"
makedepends="zlib-devel libressl-devel pam-devel"
short_desc="${_desc} - client"
replaces="openssh-client<6.1p1"
conf_files="/etc/ssh/moduli /etc/ssh/ssh_config"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://www.openssh.org"
license="BSD"
distfiles="ftp://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/$pkgname-$version.tar.gz"
checksum=48c1f0664b4534875038004cc4f3555b8329c2a81c1df48db5c517800de203bb

# Package build options
build_options="systemd openssh_identitypersist"
desc_option_openssh_identitypersist="add key to ssh-agent(1) upon connecting"

pre_configure() {
	if [ "$build_option_openssh_identitypersist" ]; then
		$XBPS_FETCH_CMD "https://bugzilla.mindrot.org/attachment.cgi?id=2396>identpersist.patch"
		patch -sNp1 -i identpersist.patch
		msg_normal "$pkgver: applied identitypersist patch.\n"
	fi
	autoreconf -fi
}

post_install() {
	vinstall contrib/sshd.pam.generic 644 etc/pam.d sshd
	vinstall contrib/ssh-copy-id 755 usr/bin
	vinstall contrib/ssh-copy-id.1 644 usr/share/man/man1
	vinstall LICENCE 644 usr/share/licenses/${pkgname}

	sed -i \
		-e 's|^#\(ListenAddress 0.0.0.0\)|\1|g' \
		-e 's|^#\(UsePAM\) no|\1 yes|g' \
		-e 's|^#\(ChallengeResponseAuthentication\) yes|\1 no|g' \
		-e 's|^#\(PrintMotd\) yes|\1 no|g' \
		${DESTDIR}/etc/ssh/sshd_config

	# Disable pam_cracklib
	sed '/pam_cracklib.*/d' -i ${DESTDIR}/etc/pam.d/sshd
	# Enable pam_lastlog(8) by default.
	echo -e "session\trequired\tpam_lastlog.so\tnever\tsilent\n" >> ${DESTDIR}/etc/pam.d/sshd
	# Enable pam_systemd by default.
	echo -e "-session\toptional\tpam_systemd.so\n" >> ${DESTDIR}/etc/pam.d/sshd
}

openssh-server_package() {
	conf_files="/etc/ssh/sshd_config /etc/pam.d/sshd"
	make_dirs="/var/chroot/ssh 0755 root root"
	depends="openssh>=$version"
	short_desc="${_desc} - server"
	pkg_install() {
		vmove usr/libexec/sftp-server
		vmove etc/ssh/sshd_config
		vmove usr/sbin
		vmove usr/share/man/man5/sshd_config.5
		for f in sftp-server sshd; do
			vmove usr/share/man/man8/${f}.8
		done
		vmove etc/pam.d
		if [ "$build_option_systemd" ]; then
			vmkdir usr/lib/systemd/system
			install -m644 ${FILESDIR}/*.{socket,service} \
				${PKGDESTDIR}/usr/lib/systemd/system
		fi
	}
}
