#    Makefile for CRM114
#
#
#   If you want to install the executables somewhere else, change
#    BINDIR here.  Default is /usr/bin 
#
prefix=/usr
BINDIR=${prefix}/bin

# VER_SUFFIX defines a version suffix for our installed executables, 
# handy when you want many versions of CRM114 coexisting.
#
# e.g.: VER_SUFFIX=-927b, then if you make install, you get crm-927b
# 
VER_SUFFIX ?= 

#
#      The following forces the compiler to be GCC.  If you have trouble
#      with your default compiler, and you want to force GCC, uncomment it.
# CC=gcc
#
#     What version is this software (PLEASE don't change this just 
#     to mess with my brain. - wsy)
#
VERSION = 20040221-BlameYokohama
#
#
#  Tell the compiler full optimization, allow debugging, and warn on every
#  possible error
#  
CFLAGS += -O3 -g -Wall
#
#
#   Define the TRE directory (used only for building distributions)
TRE_TARFILE = tre-0.6.4
#
# ---------   If you're compiling under BSD, Simson Garfinkel suggests you
#    uncomment the following to get a BSD-sane environment.  Leave them
#    untouched (commented out) for Linux builds.
# Add for FreeBSD
#CFLAGS += -I/usr/local/include
#LDFLAGS += -L/usr/local/lib
#LIBS += -lintl -liconv
#
# ------------------ end of Simson's FreeBSD changes
#
#
#    End of user-configurable options... if you modify anything below
#    this line, you risk early insanity and blindness.
#
#


all: crm114 cssutil cssdiff cssmerge 

experimental: crm114_tre
	install -m 755 crm114_tre $(BINDIR)/crma

crm114: crm114.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(MAKE) crm114_tre
	#  ./crm114 is used locally; make sure it's there too.
	ln -f -s crm114_tre crm114

crm114_gnu: crm114.o crmregex_gnu.o crm_mathexec.o
	$(CC) $(LDFLAGS) crm114.o crmregex_gnu.o crm_mathexec.o -lm -o crm114_gnu $(LIBS)

crm114_tre: crm114.o crmregex_tre.o crm_mathexec.o
	#
	#   Note: if you haven't installed TRElib, the next step 
	#   will get an error.
	#       For TRElib, look in the TRE sub-directory of this kit.
	#   You will also need to add /usr/local/lib to /etc/ld.so.conf,
	#   and then run ldconfig (as root) to set the library up and make
	#   the library known to the static and runtime linkers.
	#
	$(CC) $(LDFLAGS) crm114.o crmregex_tre.o crm_mathexec.o -static -lm -ltre -o crm114_tre $(LIBS)

crm114.o: crm114.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -DVERSION='"$(VERSION)"' -c crm114.c -o crm114.o

crmregex_gnu.o: crmregex_gnu.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -c crmregex_gnu.c -o crmregex_gnu.o

crmregex_tre.o: crmregex_tre.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -c crmregex_tre.c -o crmregex_tre.o

crm_mathexec.o: crm_mathexec.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -c crm_mathexec.c -o crm_mathexec.o

cssutil: cssutil.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -c cssutil.c -o cssutil.o  
	$(CC)  $(CFLAGS) cssutil.o -static -lm -ltre -o cssutil  

cssdiff: cssdiff.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -c cssdiff.c -o cssdiff.o
	$(CC)  $(CFLAGS) -static -ltre cssdiff.o -o cssdiff

cssmerge: cssmerge.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
	$(CC)  $(CFLAGS) -static -ltre cssmerge.c -o cssmerge

clean:
	-rm -f crm114
	-rm -f cssutil
	-rm -f cssdiff
	-rm -f cssmerge
	-rm -f crm114
	-rm -f crm114_gnu
	-rm -f crm114_tre
	-rm -f *.o

install_crm114: crm114
	#
	#   NOTE - the default CRM114 now uses the TRE regex lib, not GNU
	#          If you want the GNU one (not recommended) you should
	#          use 'install_gnu' as the make target.
	#    
	$(MAKE) install_tre

install_utils:
	$(MAKE) cssmerge cssutil cssdiff
	install -m 755 -s cssdiff $(BINDIR)/cssdiff$(VER_SUFFIX)
	install -m 755 -s cssmerge $(BINDIR)/cssmerge$(VER_SUFFIX)
	install -m 755 -s cssutil $(BINDIR)/cssutil$(VER_SUFFIX)

install: FORCE
	$(MAKE) install_crm114 install_utils

install_tre: crm114_tre
	install -m 755 -s crm114_tre $(BINDIR)/crm$(VER_SUFFIX)

# use different name so we can keep it around along with default one
# and same VER_SUFFIX postfix 
install_gnu: crm114_gnu
	install -m 755 -s crm114_gnu $(BINDIR)/crmg$(VER_SUFFIX)

install_binary_only: FORCE
	install -m 755 -s crm114_tre $(BINDIR)/crm$(VER_SUFFIX)
	install -m 755 -s cssdiff $(BINDIR)/cssdiff$(VER_SUFFIX)
	install -m 755 -s cssmerge $(BINDIR)/cssmerge$(VER_SUFFIX)
	install -m 755 -s cssutil $(BINDIR)/cssutil$(VER_SUFFIX)	

uninstall: FORCE
	-rm -rf $(BINDIR)/crm$(VER_SUFFIX)
	-rm -rf $(BINDIR)/crmg$(VER_SUFFIX)
	-rm -rf $(BINDIR)/crma$(VER_SUFFIX)
	-rm -rf $(BINDIR)/cssdiff$(VER_SUFFIX)
	-rm -rf $(BINDIR)/cssmerge$(VER_SUFFIX)
	-rm -rf $(BINDIR)/cssutil$(VER_SUFFIX)

megatest: FORCE
	#        
	#        This runs a moderately interesting set of base tests 
	#        to exercise much of CRM114 under TRE.  
	#
	#        If the output diff contains more
	#        than different PIDs for the minion processes, you may have
	#        a defective CRM114 kit or installation.
	#
	./megatest.sh > megatest_test.log
	diff megatest_knowngood.log megatest_test.log & sleep 1

cssfiles: spamtext.txt nonspamtext.txt
	#
	#   This may take a LONG TIME to run, up to 10 minutes per megabyte
	#   of text.  Please adjust your expectations accordingly.
	#
	#    Also, be advised that relearning the same text more than once
	#    can be ill-advised.  Remove the .css files first if you are
	#    relearning your entire corpus (new .css files will be created
	#    if they don't already exist)
	#
	./crm114 rewriteutil.crm scrub_mailfile_rewrites.mfp < spamtext.txt > scrubbed_spamtext.txt
	./crm114 rewriteutil.crm scrub_mailfile_rewrites.mfp < nonspamtext.txt > scrubbed_nonspamtext.txt
	./crm114 mailfilter.crm --learnspam < scrubbed_spamtext.txt
	./crm114 mailfilter.crm --learnnonspam < scrubbed_nonspamtext.txt

tarfile: all
	rm -rf crm114-$(VERSION).src
	mkdir crm114-$(VERSION).src
	cp  crm114.c crmregex_gnu.c crmregex_tre.c \
	crm_mathexec.c \
	crm114_sysincludes.h crm114_structs.h crm114_config.h \
	crm114.h regex.h \
	cssutil.c  cssdiff.c cssmerge.c \
	QUICKREF.txt INTRO.txt knownbugs.txt FAQ.txt things_to_do.txt \
	classify_details.txt Makefile README CRM114_Mailfilter_HOWTO.txt \
	rename-gz \
	megatest.sh megatest_knowngood.log\
	matchtest.crm windowtest.crm overalterisolatedtest.crm exectest.crm \
	inserttest_a.crm inserttest_b.crm learntest.crm classifytest.crm \
	escapetest.crm argtest.crm beeptest.crm skudtest.crm aliustest.crm \
	nestaliustest.crm eval_infiniteloop.crm \
	traptest.crm fataltraptest.crm uncaughttraptest.crm \
	backwardstest.crm approxtest.crm\
	mathalgtest.crm mathrpntest.crm\
	rewritetest.crm test_rewrites.mfp \
	shroud.crm classifymail.crm \
	userdirtest.crm bracktest.crm unionintersecttest.crm \
	statustest.crm windowtest_fromvar.crm \
	pad.crm pad.dat \
	COLOPHON.txt \
	mailfilter.crm mailfilter.cf \
	mailexpand.crm \
	rewriteutil.crm \
	classifymail.crm \
	procmailrc.recipe reto_procmail_recipe.recipe \
	priolist.mfp blacklist.mfp whitelist.mfp rewrites.mfp \
	scrub_mailfile_rewrites.mfp \
	crm114.spec \
	inoc_passwd.txt \
	GPL-License.txt \
	crm114-$(VERSION).src
	cp -a $(TRE_TARFILE) crm114-$(VERSION).src
	cp -a BSD_build_files crm114-$(VERSION).src
	tar -cvf crm114-$(VERSION).src.tar crm114-$(VERSION).src
	rm -rf crm114-$(VERSION).src

src_gzip: tarfile
	rm -rf crm114-$(VERSION).src.tar.gz
	gzip crm114-$(VERSION).src.tar

i386_tarfile: all
	rm -rf crm114-$(VERSION).i386
	mkdir crm114-$(VERSION).i386
	cp crm114_tre cssutil cssdiff cssmerge \
	Makefile \
	CRM114_Mailfilter_HOWTO.txt \
	COLOPHON.txt \
	QUICKREF.txt INTRO.txt knownbugs.txt FAQ.txt things_to_do.txt \
	mailfilter.crm mailfilter.cf mailexpand.crm \
	rewriteutil.crm scrub_mailfile_rewrites.mfp rewrites.mfp \
	whitelist.mfp blacklist.mfp priolist.mfp \
	GPL-License.txt \
	crm114-$(VERSION).i386
	tar -cvf crm114-$(VERSION).i386.tar crm114-$(VERSION).i386
	rm -rf crm114-$(VERSION).i386

i386_gzip: i386_tarfile
	rm -rf crm114-$(VERSION).i386.tar.gz
	gzip crm114-$(VERSION).i386.tar

css_tarfile: spam.css nonspam.css
	rm -rf crm114-$(VERSION).css
	mkdir crm114-$(VERSION).css
	cp spam.css nonspam.css \
	crm114-$(VERSION).css
	tar -cvf crm114-$(VERSION).css.tar crm114-$(VERSION).css
	rm -rf crm114-$(VERSION).css

css_gzip: css_tarfile
	rm -rf crm114-$(VERSION).css.tar.gz
	gzip crm114-$(VERSION).css.tar

distribution: install src_gzip i386_gzip css_gzip
	md5sum crm114-$(VERSION).*.tar.gz

FORCE: