include version.py

DESTDIR=
PROGNAME=sagator
ETCDIR=$(DESTDIR)/etc
BINDIR=$(DESTDIR)/usr/bin
SBINDIR=$(BINDIR)/../sbin
LIBDIR=$(DESTDIR)/usr/lib/$(PROGNAME)
DOCDIR=$(DESTDIR)/usr/share/doc/$(PROGNAME)
MRTG_CFG=$(ETCDIR)/mrtg/$(PROGNAME).cfg
MRTG_DIR=$(DESTDIR)/var/www/mrtg/$(PROGNAME)
CHROOTDIR=/var/spool/vscan
MAKE=make
INCLUDES=""
CLAMAV=yes
TARDIR=.
PYTHON_PROGRAMS=sagator sgscan sqback updatecfg
SCANNER_MODULES=\
	interscan/file.py interscan/match.py interscan/actions.py \
	interscan/report.py interscan/header.py interscan/logger.py \
	interscan/parsemail.py interscan/mimeparse.py interscan/decompress.py \
	interscan/store.py \
	av/basic.py av/dazuko.py av/clamav.py av/avg.py av/bdc.py av/nod.py \
	av/icap.py av/sophie.py av/kav.py av/sanitize.py \
	as/spamassassin.py as/bogofilter.py as/qsf.py
SERVICE_MODULES=\
	srv/collector.py srv/smtpd.py srv/milter.py srv/avfilter.py

# Python
PYTHON=$(shell for v in 2 2.2 2.3 ""; do python$$v -c "print 'python$$v'" 2>/dev/null && break; done)
PYTHONVER=$(shell $(PYTHON) pythonver.py 2>/dev/null)
PYTHONPREFIX=/usr
PYTHONPREFIX_SLASHED=$(shell echo $(PYTHONPREFIX) | sed 's/\//\\\//g')

all:	README.scanners README.services compile

info:
	@echo "PYTHON=$(PYTHON)"
	@echo "PYTHONVER=$(PYTHONVER)"

libclamav/libclamav.so: libclamav/libclamav.pyx
	[ $(CLAMAV) = yes ] && $(MAKE) -C libclamav PYTHONVER=$(PYTHONVER) PYTHONPREFIX=$(PYTHONPREFIX)

compile: libclamav/libclamav.so
	# replace interpreter for sagator's programs
	for i in $(PYTHON_PROGRAMS); do \
	  grep '^#!$(PYTHONPREFIX)/bin/$(PYTHON)$$' $$i.py > /dev/null 2>&1 || \
	    echo -e ',s/^#!\/.*$$/#!$(PYTHONPREFIX_SLASHED)\/bin\/$(PYTHON)/\nw\nq' | ed $$i.py; \
	done || true
	# compile modules
	for i in . as av interscan srv; do \
	  $(PYTHON) $(PYTHONPREFIX)/lib/$(PYTHONVER)/compileall.py -l -d $$i $$i; \
	done
	# replace config, if not found
	[ -f config.py ] || cp config.py-template config.py

install: compile
	mkdir -p $(BINDIR) $(LIBDIR) $(ETCDIR)/cron.daily $(DOCDIR) \
	  $(DESTDIR)/$(CHROOTDIR) $(SBINDIR) $(ETCDIR)/logrotate.d
	[ -f $(LIBDIR)/config.py ] && mv $(LIBDIR)/config.py $(LIBDIR)/config.py.save || true
	cp -f *.py *.py[co] $(LIBDIR)
	cp -rf as av interscan srv $(LIBDIR)
	ln -sf ../lib/$(PROGNAME)/sgscan.py $(BINDIR)/sgscan
	ln -sf ../lib/$(PROGNAME)/sagator.py $(SBINDIR)/sagator
	ln -sf ../lib/$(PROGNAME)/sqback.py $(SBINDIR)/sqback
	[ -f $(ETCDIR)/$(PROGNAME).conf ] || \
	  cp -f config.py $(ETCDIR)/$(PROGNAME).conf
	rm -f $(LIBDIR)/config.py
	ln -sf ../../../etc/$(PROGNAME).conf $(LIBDIR)/config.py
	# install logrotate script
	[ -f $(ETCDIR)/logrotate.d/sagator ] || \
	  cp -f scripts/sagator.logrotate $(ETCDIR)/logrotate.d/sagator
	# install logwatch scripts
	mkdir -p $(ETCDIR)/log.d/scripts/services \
	  $(ETCDIR)/log.d/conf/logfiles $(ETCDIR)/log.d/conf/services
	[ -f $(ETCDIR)/log.d/conf/services/sagator.conf ] || \
	  cp -f scripts/logwatch.service $(ETCDIR)/log.d/conf/services/sagator.conf
	[ -f $(ETCDIR)/log.d/conf/logfiles/sagator.conf ] || \
	  cp -f scripts/logwatch.logfile $(ETCDIR)/log.d/conf/logfiles/sagator.conf
	cp -f scripts/logwatch.py $(ETCDIR)/log.d/scripts/services/sagator
	# install libclamav
	cp -f libclamav/libclamav.so $(LIBDIR) || true
	# install init script
	if [ ! -f /etc/debian_version ]; then \
	  mkdir -p $(ETCDIR)/rc.d/init.d $(ETCDIR)/sysconfig; \
	  cp scripts/sagator.init $(ETCDIR)/rc.d/init.d/sagator; \
	  chmod +x $(ETCDIR)/rc.d/init.d/sagator; \
	  [ -f $(ETCDIR)/sysconfig/sagator ] || \
	    cp -f scripts/sysconfig-example $(ETCDIR)/sysconfig/sagator; \
	fi
	# install mkchroot
	cp scripts/mkchroot.sh $(SBINDIR)/mkchroot.sagator
	# install sagator's cron script
	sed "s/%{CHROOT}/`echo $(CHROOTDIR) | sed 's/\\//\\\\\\//g'`/" \
	  < scripts/cron > $(ETCDIR)/cron.daily/sagator
	chmod +x $(ETCDIR)/cron.daily/sagator
	# install mrtg config
	if [ ! -f $(MRTG_CFG) ]; then \
	  mkdir -p `dirname $(MRTG_CFG)`; \
	  cp -f scripts/mrtg.cfg $(MRTG_CFG); \
	fi
	if [ ! -f $(MRTG_DIR)/index.html ]; then \
	  mkdir -p $(MRTG_DIR); \
	  cp -f scripts/mrtg-index.html $(MRTG_DIR)/index.html; \
	fi
	echo "Don't forget to set chroot!"

makeroot:
	./scripts/mkchroot.sh $(DESTDIR)$(CHROOTDIR)

clean:
	$(MAKE) -C libclamav clean
	rm -rf *~ */*~ *.py[co] */*.py[co] scripts/sagator.spec \
		config.py.sagatorsave

mrproper:	clean
	rm -f libclamav/libclamav.c scripts/sagator.spec

kill:
	kill `cat /var/run/sagator.pid`

# documentation
README.scanners:	libclamav/libclamav.so $(SCANNER_MODULES) stats.py
	# Helpyfying scanners ...
	@( $(PYTHONPREFIX)/lib/$(PYTHONVER)/pydoc.py $(SCANNER_MODULES); \
	$(PYTHON) -c 'import stats; help(stats.status)' ) \
	| awk 'BEGIN {class=1} \
	     / *class .*\(/ {class=1} \
	     /^Help on.* module/ {class=1} \
	     /^NAME/ {class=0} \
	     /-------/ {if (class==1) {print ""};class=0} \
	     /Method resolution order:/ {if (class==1) {print ""};class=0} \
	     /Methods defined here:/ {if (class==1) {print ""};class=0} \
	     /__doc__/ {if (class==1) {print ""};class=0} \
	     {if (class==1) print}' \
	| sed 's/^ *//g' > README.scanners

README.services:	$(SERVICE_MODULES)
	# Helpyfying services ...
	@$(PYTHONPREFIX)/lib/$(PYTHONVER)/pydoc.py $(SERVICE_MODULES) \
	| awk 'BEGIN {class=1} \
	     / *class .*service\)/ {class=1} \
	     /^Help on.* module/ {class=1} \
	     /^NAME/ {class=0} \
	     /-------/ {if (class==1) {print ""};class=0} \
	     /Method resolution order:/ {if (class==1) {print ""};class=0} \
	     /Methods defined here:/ {if (class==1) {print ""};class=0} \
	     /__doc__/ {if (class==1) {print ""};class=0} \
	     {if (class==1) print}' \
	| sed 's/^ *//g' > README.services

version:
	echo "%define version $(VERSION)" > scripts/sagator.spec
	echo "%define rel $(RELEASE)" >> scripts/sagator.spec
	cat scripts/sagator.spec.in >> scripts/sagator.spec
	sed -e "s/%VERSION%/$(VERSION)-$(RELEASE)/" \
	  -e "s/%DATE%/`date -R`/" debian/changelog.in \
	  > debian/changelog

pack:	README.scanners README.services clean version
	(cd ..; \
	 tar cvjf $(TARDIR)/sagator-$(VERSION).tar.bz2 \
	     --exclude '*CVS*' --exclude 'sagator/config.py' sagator/*\
	)

rpm:	pack
	rpmbuild -ta ../$(TARDIR)/sagator-$(VERSION).tar.bz2

deb:	README.scanners README.services clean version
	dpkg-buildpackage -rfakeroot

.PHONY:	info compile install makeroot clean mrproper version pack rpm deb
