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

inherit eutils flag-o-matic python

DESCRIPTION="Sagator is an email/web antivirus/antispam gateway."
HOMEPAGE="http://www.salstar.sk/sagator/"

SRC_URI="%URL%"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="alpha amd64 hppa ia64 ppc ppc64 sparc x86"
IUSE="apache2 chroot doc mrtg logwatch logrotate nls sqlite"

DEPEND="dev-lang/python
	virtual/mta
	app-antivirus/clamav
	apache2? ( >=net-www/apache-2.0.54 >=dev-python/genshi-0.4.0 )
	logwatch? ( sys-apps/logwatch )
	logrotate? ( app-admin/logrotate )
	mrtg? ( net-analyzer/mrtg )
	sqlite? ( >=dev-python/pysqlite-2.0.0 )"

RESTRICT="nomirror"

#S="${WORKDIR}/${PN}"


# Updates Sagator's config file, which might be modified by the user, in order
# to reflect possible syntactic changes between Sagator major releases.
# This is actually an ugly hack,to assure consitent handling of config protected
# files updates via Portage's etc-update script.
update_sagator_config() {
	if [ -f /etc/mail/sagator/sagator.conf ]; then
		ebegin "Previous Sagator configuration found, trying automatic update"
		cp /etc/mail/sagator/sagator.conf ${S}/etc/sgconf.py
		${S}/updatecfg.py 2>&1 > /dev/null
		eend $?
	fi
}

pkg_setup() {
	enewgroup vscan
	enewuser vscan -1 -1 /var/spool/vscan vscan
}


src_compile() {
	local conf="--prefix /usr"
	conf="${conf} --logwatch-conf /etc/log.d/conf"
	conf="${conf} --logwatch-scripts /etc/log.d/scripts"
	conf="${conf} --apache-root /etc/apache2"

	./configure ${conf}
	
	# Remove some exotic CFLAGS.
	strip-flags
	
	emake || die "emake died"
}

src_install() {
	update_sagator_config
	
	make DESTDIR=${D} install || die "make install died"

	#
	# Because of the decision not to patch the toplevel Makefile.in, the left
	# over generic stuff will be replaced with Gentoo specific scripts and / or
	# config files.
	#

	# Remove compiled python files (.pyc). These must be handled via portage, in
	# order to uninstall them properly.
	find ${D}/usr/share/sagator/ -name '*.pyc' -print0 | xargs -0 rm -f 

	# Remove generic logrotate.d stuff.
	rm -r ${D}/etc/logrotate.d
	# Remove generic cron.daily stuff.
	rm -r ${D}/etc/cron.daily
	# Remove generic /usr/share/doc/sagator stuff.
	rm -r ${D}/usr/share/doc/sagator
	
	# Create Sagator's Gentoo specific configuration directory.
	dodir /etc/mail/sagator
	# Move Sagator's configuration file into a configuration directory.
	mv ${D}/etc/sagator.conf ${D}/etc/mail/sagator/sagator.conf
	# Set up appropriate permissions for configuration directory and file.
	fperms 0700 /etc/mail/sagator
	fperms 0600 /etc/mail/sagator/sagator.conf
	# Make a symlink.
	dosym /etc/mail/sagator/sagator.conf /usr/share/sagator/etc/sgconf.py

	# Install Gentoo specific init script.
	newinitd "${S}/scripts/sagator.init.gentoo" "sagator"
	
	# Install Gentoo specific conf.d file.
	newconfd "${S}/scripts/gentoo.conf" "sagator"

	# Add keepdir file to /var/spool/vscan.
	keepdir /var/spool/vscan

	# Create sagator's log directory.
	dodir /var/log/sagator
	keepdir /var/log/sagator
	fowners vscan:root /var/log/sagator
	fperms 0700 /var/log/sagator

	# Create sagator's stats dir.
	dodir /var/share/sagator
	keepdir /var/lib/sagator
	fowners vscan:root /var/lib/sagator
	fperms 0700 /var/lib/sagator
	
	
	# 
	# Documentation specific stuff.
	#

	# Install the very basic documentation.
	dodoc TODO ${S}/doc/README ${S}/doc/FAQ

	
	#
	# Useflag specific stuff.
	#
	
	# If apache2 useflag is set, rename apache configuration files and
	# directories to reflect Gentoo Apache configuration layout.
	if use apache2; then
		mv ${D}/etc/apache2/conf.d ${D}/etc/apache2/modules.d
		mv ${D}/etc/apache2/modules.d/sagator.conf \
			${D}/etc/apache2/modules.d/99sagator
	# The apache2 useflag is not set, so remove apache config directory.
	else
		rm -r ${D}/etc/apache2
		#rm -f ${D}/usr/share/sagator/html.py*
		#rm -rf ${D}/usr/share/sagator/srv/web
	fi
	
	# Install additional documentation.
	use doc &&  { \
		dohtml ${S}/doc/Databases.html \
			${S}/doc/Index.html \
			${S}/doc/Scanners.html \
			${S}/doc/Services.html \
			${S}/doc/E2I.html; \
		docinto txt; \
		dodoc ${S}/doc/*.txt; \
	}
	
	# Remove log rotation script unless logrotate useflag is set.
	use logrotate || rm -r ${D}/etc/logrotate.d

	# Remove logwatch stuff unless logwatch useflag is set.
	use logwatch || rm -r ${D}/etc/log.d
	
	# Remove MRTG stuff unless mrtg useflag is set.
	use mrtg || rm -r ${D}/etc/mrtg ${D}/var/www

	# Remove NLS stuff unless nls flag is set.
	use nls || rm -r ${D}/usr/share/locale

	# Create chroot content if chroot useflag is set.
	use chroot && ${S}/scripts/mkchroot.sh ${D}/var/spool/vscan --auto
}

pkg_postinst() {

	# Compile python files.
	python_version
	python_mod_optimize /usr/share/sagator
	
	# You must configure Sagator warning.
	ewarn
	ewarn "You must configure Sagator manually!"
	ewarn "Consult Sagator's documentation and edit"
	ewarn "/etc/mail/sagator/sagator.conf to suit your needs."
	ewarn "Then set the SAGATOR_CONFIGURED variable in /etc/conf.d/sagator"
	ewarn "to 1 (one), otherwise Sagator will refuse to start."
	ewarn
}

pkg_postrm() {
	python_version
	python_mod_cleanup /usr/share/sagator
}
