# Contributor: Elizabeth Jennifer Myers <elizabeth@sporksirc.net>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Valery Kartel <valery.kartel@gmail.com>
pkgname=proftpd
pkgver=1.3.6
pkgrel=1
_pkgver=${pkgver/_rc/rc}
pkgdesc="Highly configurable FTP server software"
url="http://www.proftpd.org/"
pkgusers=$pkgname
pkggroups=$pkgname
arch="all"
license="GPL"
options="!check"
depends=""
depends_dev="perl"
makedepends="$depends_dev bash file pcre-dev libmemcached-dev libressl-dev sqlite-dev mariadb-dev
	hiredis-dev unixodbc-dev postgresql-dev openldap-dev geoip-dev net-snmp-dev"
install="$pkgname.pre-install"
_modules="auth_file auth_otp:sftp ban copy ctrls_admin deflate delay dnsbl dynmasq exec geoip
	facl facts ident ifsession:=zz ifversion lang ldap load log_forensic memcache qos
	quotatab:-- quotatab_file:quotatab quotatab_ldap:quotatab quotatab_radius:quotatab
	quotatab_sql:quotatab,sql radius ratio readme redis rewrite sftp_sql:sftp,sql site_misc
	shaper sql:-- sql_passwd:sql sql_sqlite:sql sql_mysql:sql sql_odbc:sql sql_postgres:sql
	statcache tls tls_fscache:tls tls_memcache:tls tls_redis:tls tls_shmcache:tls unique_id
	wrap2:-- wrap2_file:wrap2 wrap2_redis:wrap2,redis wrap2_sql:wrap2,sql"
for _module in $_modules; do
	[ -z "${_module##*:*}" ] && eval _load_mod_${_module%:*}=${_module#*:}
	subpackages="$subpackages $pkgname-mod_${_module%:*}:_module"
	_shared_mods="${_shared_mods:+$_shared_mods:}mod_${_module%:*}"
done
subpackages="$pkgname-dev $pkgname-doc $pkgname-utils
	$pkgname-mod_sftp $pkgname-mod_snmp $subpackages
	"
source="ftp://ftp.proftpd.org/distrib/source/$pkgname-$_pkgver.tar.gz
	$pkgname.logrotate
	$pkgname.initd
	mod_delay.conf
	mod_load.conf
	mod_sftp.conf
	mod_tls_shmcache.conf
	libressl.patch
	"
builddir="$srcdir/$pkgname-$_pkgver"
prepare() {
	default_prepare
	update_config_guess
}

build() {
	cd "$builddir"
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/$pkgname \
		--localstatedir=/run/$pkgname \
		--libexecdir=/usr/lib/$pkgname \
		--disable-strip \
		--disable-pam \
		--enable-dso \
		--enable-facl \
		--enable-pcre \
		--enable-ipv6 \
		--enable-ctrls \
		--enable-openssl \
		--with-shared=mod_sftp:mod_snmp:$_shared_mods \
		|| return 1
	make || return 1
}

package() {
	make -C "$builddir" DESTDIR="$pkgdir" install || return 1
	sed -i	-e "5 a Include /etc/$pkgname/modules.d/*.conf" \
		-e "5 a Include /etc/$pkgname/conf.d/*.conf\n" \
		-e "s/nobody/$pkgname/" \
		-e "s/nogroup/$pkgname/" \
		"$pkgdir"/etc/$pkgname/$pkgname.conf || return 1
	mkdir -p "$pkgdir"/var/log/$pkgname \
		"$pkgdir"/etc/$pkgname/conf.d \
		"$pkgdir"/etc/$pkgname/modules.d || return 1
	install -dm750 -o ftp -g ftp "$pkgdir"/var/lib/ftp || return 1
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname || return 1
	install -Dm644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname || return 1
	rm -fr "$pkgdir"/run "$pkgdir"/usr/share/locale \
		"$pkgdir"/usr/sbin/in.$pkgname
}

dev() {
	default_dev || return 1
	mkdir -p "$subpkgdir"/usr/bin || return 1
	mv "$pkgdir"/usr/bin/prxs "$subpkgdir"/usr/bin
}

doc() {
	default_doc || return 1
	mkdir -p "$subpkgdir"/usr/share/doc/$pkgname || return 1
	cd "$builddir"
	cp -r RELEASE_NOTES doc/* contrib/README* \
		contrib/mod_quotatab_ldap.ldif \
		contrib/mod_quotatab_ldap.schema \
		"$subpkgdir"/usr/share/doc/$pkgname
}

utils() {
	pkgdesc="$pkgdesc (utilities)"
	depends="$pkgname perl perl-mail-sendmail"
	mkdir -p "$subpkgdir"/usr/sbin || return 1
	mv "$pkgdir"/usr/sbin/ftpscrub \
		"$pkgdir"/usr/sbin/ftpshut \
		"$subpkgdir"/usr/sbin || return 1
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr
}

_module() {
	local name=${subpkgname#$pkgname-}
	local load=$(eval echo \$_load_$name)
	pkgdesc="ProFTPd module: $name"
	depends="$pkgname"
	mkdir -p "$subpkgdir"/usr/lib/$pkgname || return 1
	mv "$pkgdir"/usr/lib/$pkgname/$name.so \
		"$subpkgdir"/usr/lib/$pkgname || return 1
	[ -f "$srcdir"/$name.conf ] && install -Dm644 "$srcdir"/$name.conf \
		"$subpkgdir"/etc/$pkgname/conf.d/${name#mod_}.conf
	if [ "$load" != "--" ]; then
		local file="$subpkgdir"/etc/$pkgname/modules.d/${name#mod_}.conf
		if [ -z "${load##=*}" ]; then
			file="$subpkgdir"/etc/$pkgname/modules.d/${load#=}${name#mod_}.conf
			load=""
		fi
		mkdir -p ${file%/*} || return 1
		echo "<IfModule !$name.c>" > $file
		local dep; for dep in $(echo $load | tr "," " "); do
			depends="$depends $pkgname-mod_$dep"
			echo "  <IfModule !mod_$dep.c>" >> $file
			echo "    LoadModule mod_$dep.c" >> $file
			echo "  </IfModule>" >> $file
		done
		echo "  LoadModule $name.c" >> $file
		echo "</IfModule>" >> $file
	fi
}

mod_snmp() {
	_module || return 1
	mkdir -p "$subpkgdir"/usr/share/snmp/mibs || return 1
	mv "$pkgdir"/etc/$pkgname/PROFTPD-MIB.txt \
		"$subpkgdir"/usr/share/snmp/mibs
}

mod_sftp() {
	_module || return 1
	mkdir -p "$subpkgdir"/etc/$pkgname/authorized_keys || return 1
	mv "$pkgdir"/etc/$pkgname/dhparams.pem \
		"$pkgdir"/etc/$pkgname/blacklist.dat \
		"$subpkgdir"/etc/$pkgname/
}

sha512sums="2a3ca76a0c35ba31e9d79f7f652f4f35768262f5039c5dc04ef83ac9218f624645ac6cee445af4ec6a8c59a9bdad1e7b48e0e90cd13934cbe7c3e77a2f6013c0  proftpd-1.3.6.tar.gz
142f8ba096844eb48883cdd3890fa3c0c4c8f73f7ce4f2acd5fb742053f8a3605b3f6ca5d5ab6c62c673d76eb60885d796af55bd7b53950794c4e7e2837973b8  proftpd.logrotate
a03124a88c0c9f3981762804c62aa479f36227f24408a60437bf737c1e783c539a2d2c0aed0b1d95e56a587bdb5c372f6d670c298cdb7433ecd1f64d4bef7c42  proftpd.initd
e918e9fdf42e46953faeb55f5a8e7e7afc8951b861059182993f45b7ca870b9bdfa261fc893923772060bf500c44f98503d44878473dce7534f0825a7ff87831  mod_delay.conf
06c149f4c1b8e0deb8102a0ddbe5314354d53cb70934c9315968ab69b9d2b1e16c33bf652aba05472eac4ca44137ce18c93214bb1257fad6c6e2e9b0728cf556  mod_load.conf
557cbf2c1ca3a724216837f98c0f86b7d14df5f91564e1aceef721389c4774562edbd584d3ce2611ea4b2853c50acd569c33302f534e6e60452cf12b4b997ece  mod_sftp.conf
768ce1e63b44eafb9033212655c9a5a9aa65a6f7e1b97b11210ccc0e49caf5c32dd2514dba10a098a4ab2c8bd5f3ad5167f337f5fa0a1cdd5efa4e37c980f6e5  mod_tls_shmcache.conf
abee13da067175c1f3dd1c02f8055b551e2d1370cfc430d22e204a174a4518ccf0b55cb8415676362508f4504d28d1d711f4d27001e69b6b6ce7bd7ea3f521ef  libressl.patch"