# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils

HOMEPAGE="http://www.sympa.org/"
LICENSE="GPL-2"
KEYWORDS="x86"
SLOT="0"

# The package author appears to have an unreliable naming format; falling back
# to manual filename declarations.
SRC_URI="http://www.sympa.org/distribution/old/${P}.tar.gz"

IUSE="ldap mysql postgres ssl"
DEPEND="sys-devel/gettext
	dev-lang/perl"
# Sympa claims to need all of these; perhaps some of them can be pruned anyway?
RDEPEND="
	perl-core/DB_File
	perl-core/Digest-MD5
	dev-perl/MailTools
	dev-perl/IO-stringy
	dev-perl/MIME-tools
	perl-core/MIME-Base64
	perl-core/CGI
	net-www/apache
	dev-perl/FCGI
	net-www/mod_fastcgi
	perl-core/File-Spec
	net-mail/mhonarc
	ldap? (dev-perl/perl-ldap)
	mysql? (dev-db/mysql dev-perl/DBD-mysql)
	postgres? (dev-db/postgresql dev-perl/DBD-Pg)
	!mysql? (
		!postgres? (dev-db/mysql dev-perl/DBD-mysql)
	)
	ssl? (dev-libs/openssl dev-perl/IO-Socket-SSL)"

pkg_setup() {
	if ! useq mysql && ! useq postgres; then
		ewarn
		ewarn "Neither the mysql nor postgres USE flags were enabled; using mysql as"
		ewarn "the default."
		ewarn
	fi
}


src_unpack() {
	unpack ${A} && cd ${S} || die

	# This package tries to be helpful by automatically checking for and
	# installing CPAN modules, however that behavior is not appropriate with a
	# package management system. The feature does, however, provide useful
	# dependency tracking information for deployment purposes. Remove the
	# extraneous_dep_checking patch to view the additional information.
	epatch ${FILESDIR}/all-*
	#sed -e 's/\(ac.*check_perl_modules.pl.*\)/#\1/' <configure \
	#>configure.new && mv configure.new configure && chmod a+rx configure

	sed -e 's/\(.*check_perl_modules.pl.*\)/#\1/' <Makefile.am \
	>Makefile.am.new && mv Makefile.am.new Makefile.am

	automake || die "automake failed"
}


src_compile() {
	local config="--prefix=/usr \
		--with-sbindir=/usr/sbin \
		--with-spooldir=/var/spool/sympa \
		--with-localedir=/usr/share/locale \
		--with-confdir=/etc/sympa \
		--with-cgidir=/var/sympa/www \
		--with-etcdir=/var/sympa \
		--with-iconsdir=/var/sympa/www/icons \
		--with-datadir=/usr/share/sympa \
		--with-expldir=/var/sympa/lists \
		--with-libdir=/usr/lib/sympa \
		--with-docdir=/usr/share/doc/sympa \
		--with-scriptdir=/usr/lib/sympa \
		--with-sampledir=/usr/share/doc/sympa/examples \
		--with-piddir=/var/run/sympa \
		--with-initdir=/etc/init.d \
		--with-mandir=/usr/share/man"
	econf ${config} || die "econf failed"

	emake || die "emake failed."
}


src_install() {
	if ! enewgroup sympa || ! enewuser sympa 1005 -1 -1 sympa; then
		eerror "Unable to add user and group accounts for the application."
		die "User and group addition failed."
	fi

	make DESTDIR=${D} install || die

	rm ${D}/usr/share/sympa/{README,ca-bundle.crt}
	rm -R ${D}/usr/share/sympa/create_list_templates/{html-news-letter,hotline,intranet_list,public_web_forum}

	cp ${FILESDIR}/sympa.init ${D}/etc/init.d/sympa
	cp ${FILESDIR}/*.conf ${D}/etc/sympa
	mkdir -p ${D}/var/sympa/bounces ${D}/var/sympa/www/archives ${D}/var/run/sympa
	chown sympa:sympa ${D}/var/sympa/bounces ${D}/var/sympa/www/archives ${D}/var/run/sympa
	chmod 750 ${D}/var/sympa/bounces ${D}/var/sympa/www/archives

	# This application's installer automatically sets the ownership and
	# permissions of a large number of its files; its choices are borderlining
	# on warranting a patch to completely disable that behavior.
	chown -R root:root ${D}/usr/share
	chown root:root ${D}/etc/sympa ${D}/etc/sympa/wwsympa.conf
	chown root:sympa ${D}/etc/sympa/sympa.conf ${D}/usr/share/doc/sympa/examples/sympa.conf
	chown -R root:root ${D}/usr/lib/sympa
	chown -R root:root ${D}/usr/sbin
	chown -R sympa:sympa ${D}/var/sympa
	find ${D}/usr/share/sympa -type f -exec chmod 644 {} \;
	chmod 640 ${D}/etc/sympa/sympa.conf ${D}/usr/share/doc/sympa/examples/sympa.conf
	chmod 644 ${D}/etc/sympa/wwsympa.conf ${D}/usr/share/doc/sympa/examples/wwsympa.conf
	chmod 644 ${D}/usr/share/man/man8/*
	chmod 750 ${D}/var/sympa/lists
}


pkg_postinst() {
	einfo
	einfo "You need to create a database and database user account for sympa prior to"
	einfo "starting the application; that account will need general access privileges"
	einfo "to the sympa database."
	einfo
	einfo "The Sympa web interface needs to be setup in Apache. Please consult the"
	einfo "Sympa documentation found at <http://www.sympa.org> for more information."
	einfo

	# Why does Apache need read access to this file?
	chmod +r /var/sympa/www/*.fcgi
}