include version.py

DESTDIR=
PROGNAME=sagator
OS=%OS%
SAGATOR_STABILITY=%SAGATOR_STABILITY%
PREFIX=%PREFIX%
DATADIR=%DATADIR%
ETCDIR=$(DESTDIR)/etc
BINDIR=$(DESTDIR)$(PREFIX)/bin
SBINDIR=$(DESTDIR)$(PREFIX)/sbin
MANDIR=$(DESTDIR)$(PREFIX)/share/man
PROGDIR=$(DESTDIR)$(PREFIX)/$(DATADIR)/$(PROGNAME)
DOCDIR=$(DESTDIR)$(PREFIX)/share/doc/$(PROGNAME)
APACHE_ROOT=$(DESTDIR)/%APACHE_ROOT%
MRTG_CFG=$(ETCDIR)/mrtg/$(PROGNAME).cfg
WEBDIR=$(DESTDIR)/%WEBDIR%
LOGWATCH_CONF=$(DESTDIR)%LOGWATCH_CONF%
LOGWATCH_SCRIPTS=$(DESTDIR)%LOGWATCH_SCRIPTS%
CHROOTDIR=/var/spool/vscan
MAKE=make
CP=cp -pPRf
INCLUDES=""
TARDIR=/tmp
RPMOPTS=
PYTHON_PROGRAMS=sagator sgscan \
	sgfilter sqback reporter dbcleanup updatecfg scand stats \
	test/smtptest test/mimeattach avir/libclamav/example \
	doc/gendoc scripts/log/analyzer scripts/log/logwatch
SCANNER_MODULES=\
	interscan/file.py interscan/match.py interscan/actions.py \
	interscan/report.py interscan/header.py interscan/logger.py \
	interscan/parsemail.py interscan/decompress.py \
	interscan/cache.py interscan/policy.py \
	avir/basic.py avir/filesys.py avir/clamav.py \
	avir/avg.py avir/bdc.py avir/nod.py \
	avir/icap.py avir/sophie.py avir/kav.py avir/sanitize.py avir/cmd.py \
	avir/filter.py \
	aspam/dspam.py aspam/spamassassin.py aspam/bogofilter.py aspam/qsf.py
SERVICE_MODULES=\
	srv/collector.py srv/rlimit.py \
	srv/smtpd.py srv/lmtpd.py srv/milter.py srv/filters.py \
	srv/proxy.py srv/reporter.py srv/webq.py srv/daemon.py \
	srv/policy.py

CLAMAV_H=/usr/include/clamav.h
LIBDSPAM_OBJECTS_H=/usr/include/dspam/libdspam_objects.h

# Python
PYTHON=%PYTHON%
PYTHONVER=%PYTHON_VER%
PYTHONPREFIX=%PYTHON_PREFIX%

all:	doc compile
	+make -C po

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

avir/libclamav/const.py: $(CLAMAV_H)
	$(PYTHON) avir/libclamav/getconst.py \
		avir/libclamav/const.py $(CLAMAV_H)

aspam/pydspam/const.py: $(LIBDSPAM_OBJECTS_H)
	$(PYTHON) aspam/pydspam/getconst.py \
		aspam/pydspam/const.py $(LIBDSPAM_OBJECTS_H)

compile: avir/libclamav/const.py
	# 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)/bin/$(PYTHON)|\nw\nq' | ed $$i.py; \
	done || true
	# compile modules
	for i in . aspam avir avir/libclamav aspam/pydspam interscan srv srv/web; do \
	  $(PYTHON) -c \
	    "import compileall;compileall.compile_dir('$$i', 0, '$$i')"; \
	done
	# replace config, if not found
	[ -f etc/sgconf.py ] || \
	  $(CP) etc/sagator.conf-template etc/sgconf.py

install-logwatch:
	# install logwatch scripts
	mkdir -p $(LOGWATCH_SCRIPTS)/services \
	  $(LOGWATCH_CONF)/logfiles $(LOGWATCH_CONF)/services
	[ -f $(LOGWATCH_CONF)/services/sagator.conf ] || \
	  $(CP) scripts/log/logwatch.service $(LOGWATCH_CONF)/services/sagator.conf
	[ -f $(LOGWATCH_CONF)/logfiles/sagator.conf ] || \
	  $(CP) scripts/log/logwatch.logfile $(LOGWATCH_CONF)/logfiles/sagator.conf
	$(CP) scripts/log/logwatch.py $(LOGWATCH_SCRIPTS)/services/sagator

install-systemd:
	if [ -d /usr/lib/systemd/system ]; then \
		mkdir -p $(DESTDIR)/usr/lib/systemd/system; \
		cp scripts/sagator.service $(DESTDIR)/usr/lib/systemd/system/; \
	fi

install-rhfc:	install-logwatch install-systemd
	mkdir -p $(ETCDIR)/sysconfig
	[ -f $(ETCDIR)/sysconfig/sagator ] || \
	  $(CP) scripts/sysconfig-example $(ETCDIR)/sysconfig/sagator
	[ ! -e /usr/share/selinux/devel/Makefile ] \
		|| make -C selinux install INSTDIR=$(PROGDIR)/selinux \

install-suse:	install-logwatch install-systemd
	mkdir -p $(ETCDIR)/sysconfig $(SBINDIR)
	#mkdir -p $(ETCDIR)/init.d
	#$(CP) scripts/sagator.init.suse $(ETCDIR)/init.d/sagator
	#ln -s /etc/init.d/sagator $(SBINDIR)/rcsagator
	#chmod +x $(ETCDIR)/init.d/sagator
	[ -f $(ETCDIR)/sysconfig/sagator ] || \
	  $(CP) scripts/sysconfig-example $(ETCDIR)/sysconfig/sagator

install-gentoo: install-logwatch
	true
	
install-other: install-rhfc
	true

install-debian:	install-logwatch install-systemd
	mkdir -p $(ETCDIR)/default
	[ -f $(ETCDIR)/default/sagator ] || \
	  $(CP) scripts/sysconfig-example $(ETCDIR)/default/sagator
	# make /var directories
	mkdir -p $(DESTDIR)/var/spool/vscan $(DESTDIR)/var/log/sagator
	cp -f scripts/sagator.service debian/sagator-base.service

install: compile install-$(OS)
	mkdir -p $(BINDIR) $(PROGDIR)/etc $(ETCDIR)/cron.d $(DOCDIR) \
	  $(DESTDIR)/$(CHROOTDIR) $(SBINDIR) $(ETCDIR)/logrotate.d
	[ ! -f $(PROGDIR)/etc/sgconf.py ] \
	  || mv $(PROGDIR)/etc/sgconf.py $(PROGDIR)/etc/sgconf.py.save
	$(CP) *.py aspam avir interscan srv $(PROGDIR)
	$(CP) etc/*.py $(PROGDIR)/etc/
	rm -f $(PROGDIR)/etc/sgconf.py
	ln -sf /etc/$(PROGNAME).conf $(PROGDIR)/etc/sgconf.py
	ln -sf ../$(DATADIR)/$(PROGNAME)/sgscan.py $(BINDIR)/sgscan
	ln -sf ../$(DATADIR)/$(PROGNAME)/sgfilter.py $(BINDIR)/sgfilter
	ln -sf ../$(DATADIR)/$(PROGNAME)/sagator.py $(SBINDIR)/sagator
	ln -sf ../$(DATADIR)/$(PROGNAME)/sqback.py $(SBINDIR)/sqback
	[ -f $(ETCDIR)/$(PROGNAME).conf ] || \
	  $(CP) etc/sgconf.py $(ETCDIR)/$(PROGNAME).conf
	chmod 640 $(ETCDIR)/$(PROGNAME).conf
	chmod 750 $(PROGDIR)/etc
	# install logrotate script
	[ -f $(ETCDIR)/logrotate.d/sagator ] || \
	  $(CP) scripts/log/sagator.logrotate $(ETCDIR)/logrotate.d/sagator
	# install mkchroot
	$(CP) scripts/mkchroot.sh $(SBINDIR)/mkchroot.sagator
	# install man pages
	mkdir -p $(MANDIR)/man1 $(MANDIR)/man8
	$(CP) doc/sagator.8 doc/sqback.8 $(MANDIR)/man8/
	$(CP) doc/sgscan.1 doc/sgfilter.1 $(MANDIR)/man1/
	# install sagator's cron scripts
	sed "s|%{CHROOT}|$(CHROOTDIR)|g" \
	  < scripts/cron > $(ETCDIR)/cron.d/sagator
	# install apache configuration
	mkdir -p $(APACHE_ROOT)/conf.d
	$(CP) scripts/apache.conf $(APACHE_ROOT)/conf.d/sagator.conf
	# install mrtg config
	if [ ! -f $(MRTG_CFG) ]; then \
	  mkdir -p `dirname $(MRTG_CFG)`; \
	  sed 's|/usr/share/|$(PREFIX)/$(DATADIR)/|g' \
	  < scripts/graphs/mrtg.cfg > $(MRTG_CFG); \
	fi
	if [ ! -f $(WEBDIR)/index.html ]; then \
	  mkdir -p $(WEBDIR)/tg; \
	  $(CP) scripts/graphs/rrdtool-index.html $(WEBDIR)/index.html; \
	fi
	# install *.repo files for new yum
	#if [ -d /etc/yum.repos.d ]; then \
	#  mkdir -p $(ETCDIR)/yum.repos.d $(ETCDIR)/pki/rpm-gpg; \
	#  $(CP) scripts/sagator.repo $(ETCDIR)/yum.repos.d/; \
	#  $(CP) SAGATOR-GPG-KEY $(ETCDIR)/pki/rpm-gpg/; \
	#fi
	make -C po install \
		PROGNAME=$(PROGNAME) DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)
	echo "Don't forget to set chroot!"

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

clean:
	rm -rf *~ */*~ *.py[co] */*.py[co] */*/*.py[co] \
		__pycache__ */__pycache__ */*/__pycache__ \
		etc/sgconf.py.sagatorsave svn-commit.tmp \
		Makefile scripts/sagator-*.ebuild
	$(MAKE) -C po clean
	[ ! -d test/tmp ] || rm -rf test/tmp
	[ ! -f /usr/share/selinux/devel/Makefile ] || make -C selinux clean

distclean:	clean
	rm -f scripts/sagator.spec scripts/cron scripts/apache.conf \
		scripts/graphs/mrtg.cfg \
		po/Makefile \
		debian/changelog doc/*.txt doc/*.html

check:	all
	for prog in sagator.py sgscan.py sgfilter.py stats.py reporter.py \
		dbcleanup.py scand.py sqback.py; do \
	  pychecker $$prog || true; \
	done

test:
	python3 ./utest.py
	python2 ./utest.py

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

# documentation
doc:	$(SCANNER_MODULES) $(SERVICE_MODULES) stats.py db.py avir/libclamav/const.py
	make aspam/pydspam/const.py || true
	$(PYTHON) doc/gendoc.py doc

pack:	doc clean
	ln -s . sagator-$(VERSION)
	# regenerate const.py before pack
	tar cjf $(TARDIR)/sagator-$(VERSION)%UNSTABLE_RELEASE%.tar.bz2 \
	    --exclude '__pycache__' --exclude='*.pyc' \
	    --exclude '*CVS*' --exclude .git --exclude .svn \
	    --exclude '*~' --exclude rsync \
	    --exclude sagator-$(VERSION)/etc/sgconf.py \
	    --exclude sagator-$(VERSION)/libclamav \
	    --exclude sagator-$(VERSION)/pydspam \
	    --exclude sagator-$(VERSION)/sagator-$(VERSION) \
	    sagator-$(VERSION)/*
	rm -f sagator-$(VERSION)

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

deb:	doc clean
	dpkg-buildpackage -rfakeroot

ubuntu:
	debuild --no-tgz-check -S -sa
	less ../sagator_$(VERSION)-$(RELEASE)_source.changes
	dput -f sagator-ppa ../sagator_$(VERSION)-$(RELEASE)_source.changes
	
ebuild:
	# skip script installation, it will be installed from ebuild
	# update ebuild script
	if [ "$(SAGATOR_STABILITY)" = "stable" ]; then \
		sed "s|%URL%|http://www.salstar.sk/pub/sagator/sagator-$(VERSION).tar.bz2|" \
			scripts/sagator.ebuild.in > scripts/sagator-$(VERSION).ebuild; \
	else \
		sed "s|%URL%|http://www.salstar.sk/pub/sagator/unstable/sagator-$(VERSION)-$(RELEASE).tar.bz2|" \
			scripts/sagator.ebuild.in > scripts/sagator-$(VERSION).ebuild; \
	fi

.PHONY:	info compile install doc makeroot clean distclean pack rpm deb ebuild test
