# P3Scan 1.0
# 
# (C) 2003 by Jack S. Lai <laitcg@cox.net>
#
# It's intent is to provide a follow on program to POP3-Virusscan-Proxy 0.4
# by Folke Ashberg <folke@ashberg.de>.
#
# It is based upon his program but provides numerous changes to include
# scanning pop3 mail for spam, hardening the program, addaption to todays
# email environment, and many other changes.
#
# The initial release of p3scan includes patches made and submitted to the
# original project but were never incorporated.
#
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

PREFIX=/usr
MANDIR=$(PREFIX)/man/man8
CC=gcc
#CFLAGS=-Wall -ggdb
CFLAGS=-Wall -O2
LDFLAGS=-L. -lripmime -lpcre #-static

OBJECTS=getline.o parsefile.o p3scan.o \
	scanner_basic.o scanner_avpd.o scanner_trophie.o
LIBS=libripmime.a
PROGS=p3scan
DISTNAME="p3scan-1.0"

piddir=/var/run/$(PROGS)
datadir=/var/spool/$(PROGS)
notify=/var/spool/$(PROGS)notify
user=mail.mail
userdir=/etc/$(PROGS)
docdir=/usr/doc/$(DISTNAME)
all: $(PROGS)

ripmime/libripmime.a:
	cd ripmime && $(MAKE) libripmime || exit 1

libripmime.a: ripmime/libripmime.a
	ln -sf ripmime/libripmime.a libripmime.a

p3scan: $(OBJECTS) $(LIBS)
	$(CC) -o $@ $(OBJECTS) $(LDFLAGS)

.c.o:
	@# generic for all c .o
	$(CC) $(CFLAGS) -c $<

dep depend .dep:
	@echo "creating depencies"
	rm .tmp.dep -f
	@find -name "*.c"   -maxdepth 1 -print0 | xargs -n 1 -0rt $(CC) -M $(CFLAGS)  >>.tmp.dep
	mv .tmp.dep .dep
	    
install: p3scan
	ginstall -v -m 550 --strip p3scan $(PREFIX)/sbin/
	@if test -d $(piddir); then echo "$(piddir) exists, not creating."; else mkdir -p $(piddir); fi
	@if test -d $(userdir); then echo "$(userdir) exits, not creating."; \
	else mkdir -p $(userdir); chown $(user) $(userdir); fi
	@if [ -f $(userdir)/p3scan.conf ] ; then \
	    echo "$(userdir)/p3scan.conf already exists, copying to $(userdir)/p3scan.conf.sample" ; \
	    ginstall -v -m 600 p3scan.conf $(userdir)/p3scan.conf.sample ; \
	else \
	    ginstall -v -m 600 p3scan.conf $(userdir)/ ; \
	fi
	@if [ -f $(userdir)/p3scan.mail ] ; then \
	    echo "$(userdir)/p3scan.mail already exists, copying to $(userdir)/p3scan.mail.sample" ; \
	    ginstall -v -m 644 p3scan.mail $(userdir)/p3scan.mail.sample ; \
	else \
	    ginstall -v -m 644 p3scan.mail $(userdir)/ ; \
	fi

	@if test -d $(datadir)/children; then echo "$(datadir)/children exists, not creating."; else mkdir -p $(datadir)/children; fi
	@if test -d $(notify); then echo "$(notify) exists, not creating."; else mkdir $(notify); fi
	@chown $(user) $(piddir)
	@chown -R $(user) $(datadir)
	@chown $(user) $(notify)
	@chmod -R 700 $(datadir)
	@chmod 700 $(notify)
	ginstall -v -m 644 p3scan.8.gz $(MANDIR)
	ginstall -v -m 644 p3scan_readme.8.gz $(MANDIR)
	@if test -d $(docdir); then echo "$(docdir) exists, not creating."; else mkdir -p $(docdir); fi
	ginstall -v -m 644 AUTHORS $(docdir)
	ginstall -v -m 644 CHANGELOG $(docdir)
	ginstall -v -m 644 LICENSE $(docdir)
	ginstall -v -m 644 NEWS $(docdir)
	ginstall -v -m 644 README $(docdir)
	ginstall -v -m 644 README-rpm $(docdir)
	
#	@if [ -f /etc/SuSE-release ] ; then \
#	    echo "" ; \
#	    echo "It looks like a SuSE-System" ; \
#	    echo "Chdir to stuff and run p3scan.rc.suse-install " ; \
#	    echo "to install the startup-scripts" ; \
#	fi

#	@if [ -f /etc/debian_version ] ; then \
#	    echo "" ; \
#	    echo "It looks like a Debian-System" ; \
#	    echo "Chdir to stuff and run p3scan.rc.debian-install " ; \
#	    echo "to install the startup-scripts" ; \
#	fi

clean:
	cd ripmime && $(MAKE) clean
	rm -f ripmime/libripmime.a
	rm -f ripmime/ripOLE/ripole
	rm -f $(PROGS) $(OBJECTS) $(LIBS) .dep core

uninstall:
#	@echo "Does not remove active configuration files."
	@if [ -f /usr/sbin/$(PROGS) ] ; then rm -f /usr/sbin/$(PROGS) ; fi
	@if [ -f $(MANDIR)/p3scan.8.gz ] ; then rm -r $(MANDIR)/p3scan.8.gz ; fi
	@if [ -f $(MANDIR)/p3scan_readme.8.gz ] ; then rm -r $(MANDIR)/p3scan_readme.8.gz ; fi
	@if [ -f $(userdir)/p3scan.conf.sample ] ; then rm -f $(userdir)/p3scan.conf.sample ; fi
	@if [ -f $(userdir)/p3scan.mail.sample ] ; then rm -f $(userdir)/p3scan.mail.sample ; fi
	@{ test ! -d $(datadir) || { find $(datadir) -type d ! -perm -700 -exec chmod u+w {} ';' && rm -fr $(datadir); }; }
	@{ test ! -d $(notify) || { find $(notify) -type d ! -perm -700 -exec chmod u+w {} ';' && rm -fr $(notify); }; }
	@echo "$(PROGS), $(datadir), $(notify), and samples removed."
	@if [ -f $(userdir)/p3scan.conf ] ; then echo "$(userdir)/p3scan.conf remains." ; fi
	@if [ -f $(userdir)/p3scan.mail ] ; then echo "$(userdir)/p3scan.mail remains." ; fi
	@if [ -d $(docdir) ] ; then rm -rf $(docdir) ; fi
	@if [ -f $(MANDIR)/p3scan.8.gz ] ; then rm -f $(MANDIR)/p3scan.8.gz ; fi
	@if [ -f $(MANDIR)/p3scan_readme.8.gz ] ; then rm -f $(MANDIR)/p3scan_readme.8.gz ; fi

tags:
	@#VIM Users know why! *g*
	ctags --c-types=+c+p+f+x -R .

fulltags:
	@#VIM Users know why! *g*
	find -name "*.c" -maxdepth 1 -print0 \
	| xargs -n 1 -0r $(CC) -M -H $(CFLAGS) 2>.totag >/dev/null
	find -name "*.c" -maxdepth 1 -print0 \
	| xargs -n 1 -0r echo >>.totag
	cat .totag | sed "s/^[[:space:]]*//" | grep -v "^$(CC)" | sort | uniq >.totag
	ctags --c-types=+c+p+f+x -L .totag .
	rm -f .totag

dist:
	mkdir $(DISTNAME)
	mkdir $(DISTNAME)/stuff
	$(MAKE) clean
	cp Makefile getline.{c,h} parsefile.{c,h} p3scan.{c,h} $(DISTNAME)
	cp scanner.h scanner_*.c $(DISTNAME)
	cp p3scan.conf p3scan.mail $(DISTNAME)
	cp README LICENCE $(DISTNAME)
	cp ripmime $(DISTNAME) -Rv
	cp stuff  $(DISTNAME)/stuff -Rv
	tar cfv $(DISTNAME).tar $(DISTNAME)/
	rm -rf $(DISTNAME)
	gzip -9f $(DISTNAME).tar

	
.PHONY: clean install all tags fulltags dist dep depend

include .dep

