#
#  Makefile for the Essence source directory 
#
#  Darren Hardy, University of Colorado - Boulder, July 1994
#
#  Makefile.in,v 1.43 1995/11/16 19:26:32 duane Exp
#
prefix = @prefix@
INSTALL_BINDIR	= $(prefix)/bin
INSTALL_LIBDIR	= $(prefix)/lib
INSTALL_UTILDIR	= $(INSTALL_LIBDIR)/gatherer
INSTALL_MANDIR	= $(prefix)/man

CC		= @CC@
INSTALL		= @INSTALL@
INSTALL_BIN	= @INSTALL_PROGRAM@
INSTALL_FILE	= @INSTALL_DATA@
RANLIB		= @RANLIB@
LN_S		= @LN_S@
YACC		= @YACC@
LEX		= @LEX@
LEXLIB		= @LEXLIB@
XTRA_LIBS	= @XTRA_LIBS@

MV		= mv
RM		= rm -f

DEBUG		= $(DEBUG_TOP) #-O #-g #-DDEBUG 
LFLAGS		=
YFLAGS		= -d #-t #-v

#######################################################################
#  You should not need to change anything below here
#######################################################################


#
#  REGEX_DEFINE defines which regular expression library to use.  
#  The BSD library uses ed(1)-style regular expressions, while the
#  GNU and POSIX libraries use the extended POSIX-style expressions.
#  The default is to use the POSIX-style regular expressions given
#  in the included GNUregex.c file (the full distribution is available 
#  from ftp://ftp.gnu.ai.mit.edu/pub/gnu/regex*.tar.gz).  Note that 
#  at this time only USE_POSIX_REGEX will give the full functionality.
#
REGEX_DEFINE	= -DUSE_POSIX_REGEX
REGEX_INCLUDE	= 
REGEX_TYPE	= posix

#  Defines for GNU dbm library.
GDBM_DEFINE	=
GDBM_INCLUDE	= 
GDBM_LDFLAGS	= 
GDBM_LIBS	= -lgdbm

GDBM_FLAGS	= $(GDBM_DEFINE) $(GDBM_INCLUDE)
REGEX_FLAGS	= $(REGEX_DEFINE) $(REGEX_INCLUDE)

#  Stuff from the public domain file(1) code
FILEDIR		= ./file
FILE_OBJS	= $(FILEDIR)/apprentice.o \
		  $(FILEDIR)/ascmagic.o \
		  $(FILEDIR)/file.o \
		  $(FILEDIR)/fsmagic.o \
		  $(FILEDIR)/is_tar.o \
		  $(FILEDIR)/print.o \
		  $(FILEDIR)/softmagic.o

# Objects relating to Post-processing 
PPOBJS		= post_process.o lex.yy.o y.tab.o

INCLUDE		= -I../../common/include -I../include

CFLAGS		= $(DEBUG) $(INCLUDE)
ALL_CFLAGS	= $(CFLAGS) $(GDBM_FLAGS) $(REGEX_FLAGS) 

LIBDIR		= ../lib
LIBFILE		= libessence.a
LDFLAGS		= -L../../common/lib -L$(LIBDIR) $(GDBM_LDFLAGS)
LIBS		= -lessence -ltemplate -lurl -lutil -lmd5 -lregex \
		  $(GDBM_LIBS) $(LEXLIB) $(XTRA_LIBS)

OBJS		= candidate.o data_object.o mkwordlist.o oid.o unnest.o \
		  db.o recognize.o summarize.o

CONFIGS		= byname-$(REGEX_TYPE).cf \
		  bycontent-$(REGEX_TYPE).cf \
		  byurl-$(REGEX_TYPE).cf \
		  stoplist.cf \
		  $(FILEDIR)/magic 
MAIN		= main.o
PROGS		= essence 

MANPAGES	= 
MANEXT		= 1

all:	$(LIBFILE) install-lib $(PROGS) mkmagic

ctags:
	@ctags -w *.c

clean:
	-rm -f core $(PROGS) *.o $(LIBFILE) tags mon.out Makefile.bak
	-@for dir in $(FILEDIR); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) $@); \
	done
	-rm -f lex.yy.? y.tab.?

realclean:	clean
	-rm -f Makefile
	-@for dir in $(FILEDIR); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) $@); \
	done

install:	all install-lib install-mkdir do-install

install-mkdir:
	@if [ ! -d $(INSTALL_BINDIR) ]; then mkdir $(INSTALL_BINDIR); fi
	@if [ ! -d $(INSTALL_LIBDIR) ]; then mkdir $(INSTALL_LIBDIR); fi
	@if [ ! -d $(INSTALL_LIBDIR)/gatherer ]; then mkdir $(INSTALL_LIBDIR)/gatherer; fi
	@if [ ! -d $(INSTALL_MANDIR) ]; then mkdir $(INSTALL_MANDIR); fi
	@if [ ! -d $(INSTALL_MANDIR)/man$(MANEXT) ]; then \
		mkdir $(INSTALL_MANDIR)/man$(MANEXT); fi

do-install:	
	@for f in $(PROGS); do \
		echo $(INSTALL_BIN) $$f $(INSTALL_UTILDIR); \
		$(INSTALL_BIN) $$f $(INSTALL_UTILDIR); \
	done
	@for f in $(CONFIGS); do \
		echo $(INSTALL_FILE) $$f $(INSTALL_UTILDIR); \
		$(INSTALL_FILE) $$f $(INSTALL_UTILDIR); \
	done
	$(INSTALL_FILE) quick-sum.cf $(INSTALL_UTILDIR)
	$(INSTALL_BIN) quick-sum $(INSTALL_UTILDIR)
	-$(RM) $(INSTALL_UTILDIR)/byname.cf $(INSTALL_UTILDIR)/bycontent.cf \
	       $(INSTALL_UTILDIR)/byurl.cf
	$(MV)   $(INSTALL_UTILDIR)/byname-$(REGEX_TYPE).cf \
		$(INSTALL_UTILDIR)/byname.cf
	$(MV)   $(INSTALL_UTILDIR)/bycontent-$(REGEX_TYPE).cf \
		$(INSTALL_UTILDIR)/bycontent.cf
	$(MV)   $(INSTALL_UTILDIR)/byurl-$(REGEX_TYPE).cf \
		$(INSTALL_UTILDIR)/byurl.cf

$(LIBFILE):	$(OBJS) $(PPOBJS)
	@echo "Making file(1) objects..."
	@(cd $(FILEDIR); $(MAKE) $(MFLAGS) \
		CC="$(CC)" \
		LIBDEFS=-DMAKE_LIBRARY_ONLY \
	       	INSTALL_BINDIR="$(INSTALL_BINDIR)" \
		MAGIC="$(INSTALL_UTILDIR)/magic" \
	       	INSTALLDIR="$(INSTALL_UTILDIR)" \
	       	DEBUG_TOP="$(DEBUG_TOP)" \
		all-objs)
	ar r $(LIBFILE) $(OBJS) $(FILE_OBJS) $(PPOBJS)
	$(RANLIB) $(LIBFILE)

install-lib:	$(LIBDIR)/$(LIBFILE)

$(LIBDIR)/$(LIBFILE):	$(LIBFILE)
	$(INSTALL_FILE) $(LIBFILE) $(LIBDIR)/$(LIBFILE)
	$(RANLIB) $(LIBDIR)/$(LIBFILE)

essence:	$(LIBDIR)/$(LIBFILE) $(MAIN)
	$(CC) -o $@ $(MAIN) $(LDFLAGS) $(LIBS)

mkmagic:
	@(cd $(FILEDIR); $(MAKE) $(MFLAGS) mkmagic)

recognize.o:	recognize.c
	$(CC) $(CFLAGS) $(REGEX_FLAGS) -c recognize.c

summarize.o:	summarize.c
	$(CC) $(CFLAGS) $(REGEX_FLAGS) -c summarize.c

main.o:	main.c
	$(CC) $(CFLAGS) $(REGEX_FLAGS) -DUSE_INSTALL_LIBDIR=\"$(INSTALL_UTILDIR)\" -c main.c

db.o:	db.c
	$(CC) $(CFLAGS) $(GDBM_FLAGS) -c db.c

$(PPOBJS): y.tab.h

y.tab.c y.tab.h:	pp_rules.y
	$(YACC) $(YFLAGS) pp_rules.y

lex.yy.c:	pp_rules.l y.tab.h
	$(LEX) $(LFLAGS) pp_rules.l
