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

inherit eutils depend.apache multilib autotools

DESCRIPTION="a feature rich open source mailing list software"
HOMEPAGE="http://www.sympa.org/features.html"
SRC_URI="http://www.sympa.org/distribution/${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86"
IUSE="mysql postgres ldap fcgi apache2 ssl soap"

DEPEND="
	sys-devel/gettext
	dev-lang/perl
	"
# see http://www.sympa.org/doc/html/node4.html#SECTION00422000000000000000
RDEPEND="
	!mysql? ( !postgres? ( dev-perl/DBD-mysql ) )
	${DEPEND_APACHE}
	dev-perl/Archive-Zip
	dev-perl/DBI
	dev-perl/IO-stringy
	dev-perl/MIME-tools
	dev-perl/MailTools
	dev-perl/Template-Toolkit
	dev-perl/XML-LibXML
	dev-perl/libintl-perl
	dev-perl/libnet
	dev-perl/libwww-perl
	dev-perl/regexp-common
	fcgi? ( dev-perl/FCGI )
	ldap? ( dev-perl/perl-ldap )
	mysql? ( dev-perl/DBD-mysql )
	net-mail/mhonarc
	perl-core/CGI
	perl-core/DB_File
	perl-core/Digest-MD5
	perl-core/File-Spec
	perl-core/MIME-Base64
	postgres? ( dev-perl/DBD-Pg )
	soap? ( dev-perl/SOAP-Lite )
	ssl? ( dev-perl/IO-Socket-SSL dev-perl/Net-SSLeay )
	virtual/mta
	"
# TODO
# perl suid
# apache2? conf
# DBD-Oracle
# DBD-Sybase
# Crypt-CipherSaber
# clamav? ( app-antivirus/clamav )
# >=sys-libs/db-4.1 ?
# include version in R?DEPEND
# suid_wrapper

# BUG
# DEPEND_APACHE is wrong, any CGI-enabled web server is fine
# config files are need to be modified

MY_SYMPAUSER="sympa"
MY_SYMPAGROUP="sympa"
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
	enewgroup ${MY_SYMPAGROUP}
	enewuser ${MY_SYMPAUSER} -1 -1 -1 ${MY_SYMPAGROUP}
}

src_unpack() {
	unpack ${A}
	cd ${S}
	# ignore interactive scripts
	epatch ${FILESDIR}/${P}.Makefile.patch
	epatch ${FILESDIR}/${P}.no_lazy_bindings.patch
	# modify default values
	epatch ${FILESDIR}/${P}.gentoo_default_config.patch
	if ! use fcgi ; then
		epatch ${FILESDIR}/${P}.gentoo_default_config_use_fcgi.patch
	fi
	# FIXED:
	# respect LOCALEDIR macro
	epatch ${FILESDIR}/${P}.fix_localedir_Language.pm.patch
	# FIXED:
	# wrong template charset in cjk languages (at least in Japanese)
	# UTF8 must be default
	# BUG:
	# still some texts are not displayed properly (e.g. Connexion expiration
	# period)
	epatch ${FILESDIR}/${P}.utf8_text_encoding.patch
	# TODO:
	# notify the upstream
	# FIXED:
	# honor LOCALEDIR
	epatch ${FILESDIR}/${P}.fix_localedir.patch
	eautoreconf
}
src_compile() {
	# BUG:
	# --without-foodir doesn't work, thus neither "doc" nor "nls" work
	# --with-piddir=/var/run ?
	# FIXED:
	# use multilib
	local MY_CONF="
		--prefix=/usr \
		--with-sbindir=/usr/sbin \
		--with-spooldir=/var/spool/sympa \
		--with-confdir=/etc/sympa \
		--with-etcdir=/etc/sympa \
		--with-cgidir=/usr/libexec/sympa \
		--with-iconsdir=/usr/share/sympa/icons \
		--with-datadir=/usr/share/sympa \
		--with-expldir=/var/lib/sympa/lists \
		--with-libdir=/usr/$(get_libdir)/sympa \
		--with-scriptdir=/usr/share/sympa/scripts \
		--with-sampledir=/usr/share/doc/sympa/example \
		--with-piddir=/var/run/sympa \
		--without-initdir \
		--with-mandir=/usr/share/man \
		--with-docdir=/usr/share/doc/sympa \
		--with-localedir=/usr/share/locale \
		"
	econf ${MY_CONF} || die "econf failed"
	emake || die "emake failed."
}

src_install() {
	# BUG should be in pkg_setup
	#--------------------------------------------------
	# if ! enewgroup sympa || ! enewuser sympa -1 -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
	mv "${D}etc/sympa/sympa.conf" "${D}etc/sympa/sympa.conf-dist" \
		|| die "mv failed"
	mv "${D}etc/sympa/wwsympa.conf" "${D}etc/sympa/wwsympa.conf-dist" \
		|| die "mv failed"

	#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}

	# BUG:
	# broken
	newinitd ${FILESDIR}/${P}.initd ${PN}
	#cp ${FILESDIR}/sympa.init ${D}/etc/init.d/sympa
	# use *-dist
	#cp ${FILESDIR}/*.conf ${D}/etc/sympa
	# BUG use keepdir

	# FIXED:
	# patching files accordingly
	#
	# sympa writes pid files *AFTER* dropping priv
	keepdir /var/run/sympa
	fowners ${MY_SYMPAUSER}:${MY_SYMPAGROUP} /var/run/sympa
	# queue dirs
	keepdir /var/spool/sympa
	local MY_DIRS="arc auth bounce digest expire moderation msg outgoing qbounce task tmp"
	local MY_DIR
	for MY_DIR in ${MY_DIRS}; do
		keepdir /var/spool/sympa/${MY_DIR}
		fowners ${MY_SYMPAUSER}:${MY_SYMPAGROUP} /var/spool/sympa/${MY_DIR}
		fperms  750 /var/spool/sympa/${MY_DIR}
	done
	keepdir /var/lib/sympa
	keepdir /var/lib/sympa/lists
	fowners ${MY_SYMPAUSER}:${MY_SYMPAGROUP} /var/lib/sympa/lists

	#--------------------------------------------------
	# 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
	#--------------------------------------------------
	#--------------------------------------------------
	# # BUG:
	# # APACHE2?_MODULES_CONFDIR is for apache modules
	if use soap ; then
		docinto /usr/share/doc/${PN}
		newdoc ${FILESDIR}/${P}.apache_soap.conf soap.conf
	fi
	docinto /usr/share/doc/${PN}
	newdoc ${FILESDIR}/${P}.apache.conf apache.conf || die "newdoc failed"
	#
	# if use apache2; then
	# 	if use soap ; then
	# 		insinto ${APACHE2_MODULES_CONFDIR}
	# 		newins ${FILESDIR}/${P}.apache_soap.conf sympa_soap.conf
	# 	fi
	# 	insinto ${APACHE2_MODULES_CONFDIR}
	# 	newins ${FILESDIR}/${P}.apache.conf sympa.conf
	# else
	# 	if use soap ; then
	# 		insinto ${APACHE1_MODULES_CONFDIR}
	# 		newins ${FILESDIR}/${P}.apache_soap.conf sympa_soap.conf
	# 	fi
	# 	insinto ${APACHE1_MODULES_CONFDIR}
	# 	newins ${FILESDIR}/${P}.apache.conf sympa.conf
	# fi
	#-------------------------------------------------- 
	#--------------------------------------------------
	# BUG:
	# should be fixed in the upstream, if it's really a problem
	# patching Makefile.am is preferred
	#
	# # 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 "Sample configs are installed in /usr/share/doc/${P}."
	einfo

	# BUG:
	# virtual/libgc
	if built_with_use sys-libs/glibc userlocale ; then
		ewarn "glibc needs to be built without userlocale USE flag"
	fi

	# Why does Apache need read access to this file?
	# chmod +r /var/sympa/www/*.fcgi
	# use soap && \
	chmod o+r /usr/libexec/sympa/sympa_soap_server.fcgi
	chmod o+r /usr/libexec/sympa/wwsympa.fcgi
}