# Makefile for amavis-stats
#
# Author: Mark Lawrence
#
# Copyright (C) 2003,2004 Mark Lawrence (nomad@null.net)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License with
#  the Debian GNU/Linux distribution in file /usr/share/common-licenses/GPL;
#  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
#  Suite 330, Boston, MA  02111-1307  USA
#
# On Debian systems, the complete text of the GNU General Public
# License, version 2, can be found in /usr/share/common-licenses/GPL-2.

SHELL   = /bin/sh
PACKAGE = amavis-stats
VERSION := $(shell ./version)
DESC    := $(shell ./version -d)
#SOURCE  = http://chili.nar.com.au/~mark/amavis-stats/$(DIST)
SOURCE  = $(dist)

DISTFILES = Makefile version README amavis-stats amavis-stats.1 \
			amavis-stats.lsm amavis-stats.php apache.conf debian redhat

TAR = tar
GZIP_ENV = --best

top_srcdir  = .

distdir     = $(PACKAGE)-$(VERSION)
dist	    = $(PACKAGE)-$(VERSION).tar.gz

debarchdir  = debarchive-$(VERSION)
debarch     = as-debarchive-$(VERSION).tar.gz

rhspec      = $(PACKAGE)-$(VERSION)-1.spec

etcdir      = $(DESTDIR)/etc/$(PACKAGE)
sbindir     = $(DESTDIR)/usr/sbin
libdir      = $(DESTDIR)/var/lib/$(PACKAGE)
imgdir      = $(DESTDIR)/var/lib/$(PACKAGE)/img
man1dir     = $(DESTDIR)/usr/share/man/man1
phpdir      = $(DESTDIR)/usr/share/$(PACKAGE)


all:

version: amavis-stats.php
	sed -e 's/^\(\$$asVersion \+= \+\"\).*\"/\1$(VERSION)\"/' amavis-stats.php > VERSION.php
	cp VERSION.php amavis-stats.php
	rm -f VERSION.php
	sed -e 's/^\(\$$version \+= \+\"\).*\"/\1$(VERSION)\"/' amavis-stats > VERSION.pl
	cp VERSION.pl amavis-stats
	rm -f VERSION.pl

install:
	install -d $(etcdir)
	install -m644 apache.conf $(etcdir)
	install -d $(sbindir)
	install -m755 amavis-stats $(sbindir)
	install -d $(phpdir)
	install -m644 amavis-stats.php $(phpdir)
	install -d $(libdir)
	install -d $(imgdir)
	cd $(phpdir) && rm -rf img # braindead rpm build system
	cd $(phpdir) && ln -s ../../../var/lib/$(PACKAGE)/img img
	cd $(phpdir) && rm -f index.php # braindead rpm build system
	cd $(phpdir) && ln -s amavis-stats.php index.php

installman:
	install -d $(man1dir)
	install -m644 amavis-stats.1 $(man1dir)

installapache:
	echo "Include /etc/$(PACKAGE)/apache.conf" >> /etc/apache/httpd.conf

localinstall:
	install -d /var/lib/$(PACKAGE)
	install -d /usr/local/sbin
	install -m755 amavis-stats /usr/local/sbin
	install -d /usr/local/share/$(PACKAGE)
	install -m644 amavis-stats.php /usr/local/share/$(PACKAGE)
	install -d $(man1dir)
	install -m644 amavis-stats.1 $(man1dir)

distdir: $(DISTFILES)
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)

dist: version distdir
	cp -pr $(DISTFILES) $(distdir)
	-chmod -R a+r $(distdir)
	GZIP=$(GZIP_ENV) $(TAR) chozf $(dist) $(distdir)
	-rm -rf $(distdir)

deb: version
	dpkg-buildpackage -b -rfakeroot -tc -us -uc

rpm: dist
	cd redhat && $(MAKE) \
		VERSION="$(VERSION)" \
		PACKAGE="$(PACKAGE)" \
		DESC="$(DESC)" \
		SOURCE="$(SOURCE)" spec
	cp $(dist) /usr/src/rpm/SOURCES
	rpm -ba redhat/$(PACKAGE)-$(VERSION)-1.spec
	cp /usr/src/rpm/RPMS/i386/amavis-stats-$(VERSION)-1.i386.rpm .

archive: deb dist rpm
	mkdir -p $(debarchdir)/contrib/binary-i386
	cp ../$(PACKAGE)_$(VERSION)_*.deb $(debarchdir)/contrib/binary-i386
	cp ../php4-rrdtool*.deb $(debarchdir)/contrib/binary-i386
	cd $(debarchdir) && dpkg-scanpackages contrib/binary-i386 /dev/null | \
	gzip -c > Packages.gz
	cd ..
	GZIP=$(GZIP_ENV) $(TAR) chozf $(debarch) $(debarchdir) $(dist) \
	  amavis-stats-$(VERSION)-1.i386.rpm
	-rm -rf $(debarchdir)

clean:
	-rm -f *.tar.gz
	-rm -f *.rpm
	cd redhat && $(MAKE) clean

sysclean:
	sudo rm -f /var/lib/amavis-stats/*rrd
	sudo rm -f /var/lib/amavis-stats/amavis-stats*

# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
