#   @configure_input@
#
#   Makefile for the "Bag Of Words Library", libbow.
#   Copyright (C) 1996, 1997 Andrew McCallum
#
#   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>
#   Date: September 1996
#
#   This file is part of the Bag-Of-Words Library, `libbow'.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Library General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#   
#   This library 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
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this library; if not, write to the Free
#   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

SHELL = /bin/sh

#### Start of system configuration section. ####

default:

srcdir = @srcdir@
VPATH = @srcdir@
RANLIB = @RANLIB@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
MAKEINFO = makeinfo
TEXI2DVI = texi2dvi
PERL = @PERL@

CC = @CC@

DEFS = @DEFS@
LIBS = @LIBS@
     
# All these are optional.  You can redifine CFLAGS, CPPFLAGS,
# INCLUDEFLAGS and LDFLAGS on the command line however you like.
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
INCLUDEFLAGS =
LDFLAGS =

prefix = @prefix@
exec_prefix = @exec_prefix@

# Installation locations
bindir = $(exec_prefix)/bin
libdir = $(exec_prefix)/lib
includedir = $(prefix)/include/bow
infodir = $(prefix)/info

#### End of system configuration section. ####

include $(srcdir)/Version

LIBBOW_H_FILES = \
bow/libbow.h \
bow/naivebayes.h \
bow/prind.h \
bow/tfidf.h

LIBBOW_C_FILES = \
array.c \
barrel.c \
bitvec.c \
bmalloc.c \
deflexer.c \
dv.c \
docnames.c \
email.c \
error.c \
foilgain.c \
heap.c \
info_gain.c \
int4docn.c \
int4str.c \
int4word.c \
io.c \
istext.c \
lex-email.c \
lex-gram.c \
lex-html.c \
lex-indirect.c \
lex-simple.c \
methods.c \
naivebayes.c \
normalize.c \
opts.c \
primes.c \
prind.c \
sarray.c \
scale.c \
scan.c \
split.c \
stem.c \
stoplist.c \
stopwords.c \
tfidf.c \
vpc.c \
wi2dvf.c \
wv.c

$(LIBBOW_C_FILES): $(LIBBOW_H_FILES)

LIBBOW_O_FILES = $(LIBBOW_C_FILES:.c=.o)

# Other name ideas: longbow, archer, shoelace,...
DEMO_C_FILES = \
rainbow.c \
arrow.c \
crossbow.c

DEMO_O_FILES = $(DEMO_C_FILES:.c=.o)
DEMO_EXECUTABLES = $(DEMO_C_FILES:.c=)

PERL_FILES = \
rainbow-ac.pl \
rainbow-pr.pl \
rainbow-stats.pl

PERL_RUNNABLE_FILES = $(PERL_FILES:.pl=)

DOC_TEXI_FILES = \
libbow.texi \
rainbow.texi \

DOC_INFO_FILES = $(DOC_TEXI_FILES:.texi=.info)
DOC_DVI_FILES = $(DOC_TEXI_FILES:.texi=.dvi)

OTHER_TEXI_FILES = \
libbow-desc.texi \
install.texi \
readme.texi \
version.texi

DIST_FILES = \
$(LIBBOW_C_FILES) \
$(LIBBOW_H_FILES) \
$(DEMO_C_FILES) \
$(PERL_FILES) \
$(DOC_TEXI_FILES) \
$(OTHER_TEXI_FILES) \
ChangeLog \
HACKING \
INSTALL \
Makefile.in \
NEWS \
README \
TODO \
Version \
configure \
configure.in \
install-sh \
mkinstalldirs

# Pattern rule
ALL_CPPFLAGS = $(CPPFLAGS) $(INCLUDEFLAGS) -I$(srcdir) -I$(srcdir)/argp $(DEFS)
ALL_CFLAGS = $(CFLAGS)
%.o: %.c
	$(CC) -c $(ALL_CPPFLAGS) $(ALL_CFLAGS) -o $@ $<

# Rules

default: $(DEMO_EXECUTABLES) $(PERL_RUNNABLE_FILES)

all: libbow.a $(DEMO_EXECUTABLES) $(PERL_RUNNABLE_FILES)

# Compiling code

libbow.a: $(LIBBOW_O_FILES)
	$(AR) rc $@ $^
	$(RANLIB) $@

argp/libargp.a:
	cd argp ; $(MAKE) libargp.a

ALL_LIBS = $(LIBS) -L. -lbow -L./argp -largp -lm

$(DEMO_EXECUTABLES): %: libbow.a argp/libargp.a %.o
	$(CC) $(CFLAGS) $@.o -o $@ $(LDFLAGS) $(ALL_LIBS)

# A special executable for loading into Lisp (for Kamal)
rainbow-lisp.o: rainbow.c
	$(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c -DNDEBUG -DRAINBOW_LISP $< -o $@

$(PERL_RUNNABLE_FILES): %: %.pl
	rm -f $@
	cat $< | sed -e 's,/usr/bin/perl,${PERL},' > $@
	chmod a+x $@

# Compiling documentation

MAKEINFO_FLAGS = -I $(srcdir)
%.dvi: %.texi version.texi
	$(TEXI2DVI) $(srcdir)/$*.texi
%.info: %.texi version.texi
	$(MAKEINFO) $(MAKEINFO_FLAGS) $(srcdir)/$*.texi
INSTALL: install.texi version.texi
	$(MAKEINFO) $(MAKEINFO_FLAGS) -o INSTALL -D INSTALL_ONLY \
	--no-header --no-split $(srcdir)/install.texi
README: readme.texi version.texi
	$(MAKEINFO) $(MAKEINFO_FLAGS) -o README -D README_ONLY \
	--no-header --no-split $(srcdir)/readme.texi

version.texi: $(srcdir)/Version
	rm -f version.texi
	echo '@set BOWVERSION' $(BOW_VERSION) \
		> version.texi
	if [ $(BOW_FTP_MACHINE) ]; then \
		echo '@set BOW_FTP_MACHINE $(BOW_FTP_MACHINE)' \
		>> version.texi; fi
	if [ $(LIBBOW_FTP_DIRECTORY) ]; then \
		echo '@set BOW_FTP_DIRECTORY $(BOW_FTP_DIRECTORY)' \
		>> version.texi; fi

# libbow.h has macros that indicate the libbow's version number; these
# must be kept up-to-date.
bow/libbow.h: Version
	rm -f bow/libbow-tmp.h~
	mv bow/libbow.h bow/libbow-tmp.h~
	cat bow/libbow-tmp.h~ | sed -e \
	's/e BOW_MAJOR_VERSION .*/e BOW_MAJOR_VERSION $(BOW_MAJOR_VERSION)/' \
	| sed -e \
	's/e BOW_MINOR_VERSION .*/e BOW_MINOR_VERSION $(BOW_MINOR_VERSION)/' \
	> $@

# The compiled functions in these files are defined entirely by libbow.h.
io.o bmalloc.o: bow/libbow.h

# Compiling the Makefile

Makefile: Makefile.in config.status
	$(SHELL) config.status
config.status: $(srcdir)/configure
	$(SHELL) $(srcdir)/configure --no-create
$(srcdir)/configure: configure.in
	cd $(srcdir); autoconf

# Installing

installdirs:
	$(srcdir)/mkinstalldirs $(bindir) $(includedir) $(libdir)
INSTALL_FILES = libbow.a bow/libbow.h \
	 $(DEMO_EXECUTABLES) $(PERL_RUNNABLE_FILES)
install: $(INSTALL_FILES) installdirs
	cd argp ; $(MAKE) install
	rm -f $(libdir)/libbow.a
	cp libbow.a $(libdir)
	$(RANLIB) $(libdir)/libbow.a
	rm -f $(includedir)/libbow.h
	cp bow/libbow.h $(includedir)
	cd $(bindir) ; rm -f $(DEMO_EXECUTABLES) $(PERL_RUNNABLE_FILES)
	$(INSTALL) $(DEMO_EXECUTABLES) $(bindir)
	$(INSTALL) $(PERL_RUNNABLE_FILES) $(bindir)

# Cleaning

mostlyclean:
	rm -f core *~
	cd argp ; $(MAKE) mostlyclean
clean: mostlyclean
	rm -f *.o libbow.a $(DEMO_EXECUTABLES) *.info *.dvi
	cd argp ; $(MAKE) clean
maintainer-clean: clean
	rm -f $(PERL_RUNNABLE_FILES) README INSTALL
	rm -f configure config.log config.cache config.status
	cd argp ; $(MAKE) maintainer-clean

# Making a distribution .tar.gz file

bow-$(BOW_VERSION).tar.gz: dist
dist: $(DIST_FILES)
	cvs rtag -F release-`echo $(BOW_VERSION) | tr . -` bow
	echo bow-$(BOW_VERSION) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	mkdir `cat .fname`/bow
	for file in $(DIST_FILES); do \
	  ln $$file `cat .fname`/$$file ; \
	done
	mkdir `cat .fname`/argp
	cd argp ; SNAP=../`cat ../.fname`/argp $(MAKE) snap
	tar -chvf `cat .fname`.tar `cat .fname`
	rm -f `cat .fname`.tar.gz
	gzip -9 `cat .fname`.tar
	rm -rf `cat .fname` .fname

diff: $(DIST_FILES)
	@if [ ! $(OLD) ] ; then \
	  echo You must set OLD to the version number against which to diff ; \
	  exit -1 ; \
	fi
	@if [ ! $(NEW) ] ; then \
	  echo You must set NEW to the version number with which to diff ; \
	  exit -1 ; \
	fi
	gunzip -c bow-$(NEW).tar.gz | (cd /tmp ; tar -xf -)
	gunzip -c bow-$(OLD).tar.gz | (cd /tmp ; tar -xf -)
	rm -f bow-$(OLD)-$(NEW).diff
	-diff -u -r /tmp/bow-$(OLD) /tmp/bow-$(NEW) > bow-$(OLD)-$(NEW).diff
	rm -rf /tmp/bow-$(NEW)
	rm -rf /tmp/bow-$(OLD)

snapshot: $(DIST_FILES)
	echo bow-`date +%y%m%d` > .fname
	cvs rtag -F snapshot-`cat .fname` bow
	rm -rf `cat .fname`
	mkdir `cat .fname`
	mkdir `cat .fname`/bow
	for file in $(DIST_FILES); do \
	  ln $$file `cat .fname`/$$file ; \
	done
	mkdir `cat .fname`/argp
	cd argp ; SNAP=../`cat ../.fname`/argp $(MAKE) snap
	tar -chvf `cat .fname`.tar `cat .fname`
	rm -f `cat .fname`.tar.gz
	gzip -9 `cat .fname`.tar
	rm -rf `cat .fname` .fname

dist-cmu: bow-$(BOW_VERSION).tar.gz
	cp -f $< /afs/cs/project/theo-11/www/naive-bayes

-include Makefile.local
