# citadel ebuild...<devel@citadel.org>
# $Header$

inherit eutils

DESCRIPTION="Advanced multiprotocol messaging and collaboration server"
HOMEPAGE="http://www.citadel.org/"
SRC_URI="http://my.citadel.org/download/citadel-6.25.tar.gz http://easyinstall.citadel.org/citadel-6.25.tar.gz ftp://ftp.tux.org/pub/net/citadel/citadel-6.25.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86 ~ppc ~sparc"
IUSE="ldap ssl zlib"
S=${WORKDIR}/${PN}

DEPEND=">=dev-libs/libical-0.24_rc4
	>=sys-devel/autoconf-2.53
	>=sys-libs/db-4.1.25_p1
	ldap? ( >=net-nds/openldap-2.0.27 )
	ssl? ( >=dev-libs/openssl-0.9.6 )
	zlib? ( >=sys-libs/zlib-1.1.4 )"

pkg_preinst() {
	enewgroup citadel 175
	enewuser citadel 175 /dev/null /usr/local/citadel citadel
}

src_unpack() {
	unpack ${A}
}

#
# NB:  We do not use the typical approach of installing into /usr
# because for historical reasons Citadel was not designed for this
# type of installation.  This will be corrected in a future release
# of Citadel.
#
# For the moment we install Citadel into the default directory
# /usr/local/citadel for historical and compatibility reasons.
#

src_compile() {
	local myconf

# Currently disabled because chkpwd doesn't like it
#	if use icc
#	then
#		CC=icc
#		CXX=icc
#		export CC CXX
#	fi

	myconf="${myconf} --enable-autologin --with-libical --with-db"

	myconf="${myconf} `use_with pam`"
	myconf="${myconf} `use_with ssl`"
	myconf="${myconf} `use_with zlib`"
	myconf="${myconf} `use_with ldap`"

	./configure --host=${CHOST} --prefix=/usr/local/citadel ${myconf} || die "configure failed"
	emake || die "make failed"
}

src_install() {
	mkdir ${D}/etc/pam.d
	make root=${D} install || die "make install failed"
	dodoc ChangeLog docs/* README.txt techdoc/*

	prepall
	prepalldocs

	keepdir /usr/local/citadel/data
}

pkg_postinst() {
	/usr/local/citadel/setup -q
	if test -f /etc/services
	then
		if ! grep '^citadel' /etc/services >/dev/null
		then
			echo "citadel		504/tcp		# citadel" >> /etc/services
		fi
	fi
	if test -f /etc/inittab
	then
		grep -v 'citserver' /etc/inittab >/etc/inittab.new && \
		echo "cit1:2345:respawn:/usr/local/citadel/citserver -h/usr/local/citadel -x7 -llocal4" >>/etc/inittab.new && \
		mv -f /etc/inittab.new /etc/inittab
		/sbin/init q
	fi

	einfo "Citadel has been started."
# Pending resolution/creation of webcit package
#	if ! has_version ">=net-misc/webcit-5.10"
#	then
#		einfo ""
#		einfo "If you want to access Citadel via the Web, you need to also emerge"
#		einfo "the net-misc/webcit package (it is either not present or too old)."
#	fi
}

pkg_prerm() {
	if test -f /etc/inittab
	then
		grep -v 'citserver' < /etc/inittab > /etc/inittab.new && \
		mv -f /etc/inittab.new /etc/inittab
		/sbin/init q
	fi
}