#
# Makefile for TDB
#

#
# User configurable options
#

# Set this path to where to you want this package installed after compilation
DESTDIR	  = /tes/tdb/v3.3

# Choose a C compiler.  It must be ANSI-C compliant.
CC	  = gcc	# GNU's ANSI C compiler
#CC	  = acc	# Sun's ANSI C compiler
#CC	  = clcc # CenterLine's ANSI C compiler

# Choose an optimization or debug level.  It is recommended
# that you keep debugging information in the object files.
OPT	  = -g 

# Choose a dependency generator flag for the compiler chosen
DPND_FLAG = -MM	# for gcc
#DPND_FLAG = -M	# for acc

# Choose a yacc compiler
YACC	  = bison -dy		# GNU's yacc equivalent
#YACC	  = yacc -dl		# Standard Unix yacc
#YACC	  = byacc -d		# Berkeley yacc

# Choose a lex parser
LEX	  = flex -Cf		# GNU's lex equivalent
#LEX	  = lex			# Standard Unix lex

# Comment out the next line if you do not have the mmap() system call.
MMAP	  = -DHAVE_MMAP

# Comment out the next line if you do not want TDB to recover from
# signals itself.
SIGS	  = -DHANDLE_SIGS

# Uncomment the next line if you are running a System V operating system
#SYSV_FLAG= -DSYSV

# Uncomment the next line if you want to profile the code
#PROFILE   = -p
#TIMES	   = -DDO_TIMES

#
# You shouldn't have to change anything below this section
#

#DBMALL	  = -DDBMALLOC
#PIPE	   = -pipe

CFLAGS	  = $(OPT) $(MMAP) $(SIGS) $(SYSV_FLAG) $(INC) \
	    $(DBMALL) $(PROFILE) $(PIPE) $(TIMES)
LDFLAGS	  = $(PROFILE)
INC	  = -I../include
TDBLIB	  = ../lib/libtdb.a
LIBS	  = $(TDBLIB)
FIXDIR	  = 2>&1 | sed 's:\.\./::g'

BINDIR	  = $(DESTDIR)/bin
LIBDIR	  = $(DESTDIR)/lib
SRCDIR	  = $(DESTDIR)/src
INCDIR	  = $(DESTDIR)/include

PROGRAMS  = check_desc tdbgen generic tdb_dump gen
SCRIPTS	  = tdb-problem

PURE_SRC  = df.c di.c \
	    ds_parent.c ds_file.c ds_join.c ds_exit.c ds_signal.c ds_global.c \
	    symbol.c tdb_error.c util.c list.c memfile.c date_util.c id.c     \
	    timer.c pds.c

INTER_SRC = yacc.y lex.l date.y date.l

DERIV_SRC = info.tab.c info.yy.c

SRC	  = $(PURE_SRC) $(DERIV_SRC)
OBJ       = $(SRC:%.c=%.o)

HEADER_PRI= list.h memfile.h tdb_defines.h tdb_proto.h tdb_struct.h util.h \
	    tdb_ds_proto.h
HEADER_PUB= tdb_error.h tdb_public.h
HEADER_SRC= $(HEADER_PRI:%=../include/%) $(HEADER_PUB:%=../include/%)

OUTPUT_OPTION = -o $@

default: $(PROGRAMS)

$(TDBLIB): $(OBJ)
	ar ru $(TDBLIB) $?
	-ranlib $(TDBLIB)

version:
	what $(TDBLIB) | sort | uniq > libtdb.version

check_desc: check_desc.o $(TDBLIB)
	$(CC) $(LDFLAGS) -o check_desc check_desc.o $(LIBS)

tdbgen: tdbgen.o $(TDBLIB)
	$(CC) $(LDFLAGS) -o tdbgen tdbgen.o $(LIBS)

generic: generic.o $(TDBLIB)
	$(CC) $(LDFLAGS) -o generic generic.o $(LIBS)

gen: gen.o $(TDBLIB)
	$(CC) $(LDFLAGS) -o gen gen.o $(LIBS)

tdb_dump: tdb_dump.o
	$(CC) $(LDFLAGS) -o tdb_dump tdb_dump.o

# info.tab.c: info.tab.h
# info.tab.o: info.tab.h

# info.tab.h: yacc.y
# 	rm -rf td1
# 	mkdir td1
# 	cd td1; $(YACC) ../yacc.y $(FIXDIR)
# 	cd td1; sed -e 's/yy/yyinfo/g' -e 's/YY/YYINFO/g' -e 's/\.\.\///g' \
# 		< y.tab.h > info.tab.h
# 	cd td1; sed -e 's/yy/yyinfo/g' -e 's/YY/YYINFO/g' -e 's/\.\.\///g' \
# 		< y.tab.c > info.tab.c
# 	rm -f info.tab.h
# 	rm -f info.tab.c
# 	mv td1/info.tab.h .
# 	mv td1/info.tab.c .
# 	rm -rf td1

# info.yy.c: lex.l
# 	rm -rf td2
# 	mkdir td2
# 	cd td2; $(LEX) ../lex.l $(FIXDIR)
# 	cd td2; sed -e 's/yy/yyinfo/g' -e 's/YY/YYINFO/g' -e 's/\.\.\///g' \
# 		< lex.yy.c > info.yy.c
# 	rm -f info.yy.c
# 	mv td2/info.yy.c .
# 	rm -rf td2

# test_date: test_date.o $(TDBLIB)
# 	$(CC) test_date.o -o test_date $(LIBS)

# date.tab.c: date.tab.h
# date.tab.o: date.tab.h

# date.tab.h: date.y
# 	rm -rf td3
# 	mkdir td3
# 	cd td3; $(YACC) ../date.y $(FIXDIR)
# 	cd td3; sed -e 's/yy/yydate/g' -e 's/YY/YYDATE/g' -e 's/\.\.\///g' \
# 		< y.tab.h > date.tab.h
# 	cd td3; sed -e 's/yy/yydate/g' -e 's/YY/YYDATE/g' -e 's/\.\.\///g' \
# 		< y.tab.c > date.tab.c
# 	rm -f date.tab.h
# 	rm -f date.tab.c
# 	mv td3/date.tab.h .
# 	mv td3/date.tab.c .
# 	rm -rf td3

# date.yy.c: date.l
# 	rm -rf td4
# 	mkdir td4
# 	cd td4; $(LEX) ../date.l $(FIXDIR)
# 	cd td4; sed -e 's/yy/yydate/g' -e 's/YY/YYDATE/g' -e 's/\.\.\///g' \
# 		< lex.yy.c > date.yy.c
# 	rm -f date.yy.c
# 	mv td4/date.yy.c .
# 	rm -rf td4

install: install-library install-source install-include install-bin

install-library: $(LIBDIR) $(LIBDIR)/libtdb.a
install-source:  $(SRCDIR) $(SRCDIR)/Makefile
install-source:  $(SRCDIR) $(SRCDIR)/makedep
install-source:  $(SRCDIR) $(SRC:%=$(SRCDIR)/%)
install-source:  $(SRCDIR) $(INTER_SRC:%=$(SRCDIR)/%)
install-include: $(SRCDIR) $(HEADER_PUB:%=$(SRCDIR)/%)
install-include: $(SRCDIR) $(HEADER_PRI:%=$(SRCDIR)/%)
install-include: $(INCDIR) $(HEADER_PUB:%=$(INCDIR)/%)
install-bin:     $(BINDIR) $(PROGRAMS:%=$(BINDIR)/%)
install-bin:	 $(BINDIR) $(SCRIPTS:%=$(BINDIR)/%)

$(DESTDIR):
	-mkdir $@

$(LIBDIR): $(DESTDIR)
	-mkdir $@

$(SRCDIR): $(DESTDIR)
	-mkdir $@

$(INCDIR): $(DESTDIR)
	-mkdir $@

$(BINDIR): $(DESTDIR)
	-mkdir $@

$(LIBDIR)/libtdb.a: $(TDBLIB)
	@rm -f      $@
	cp $(TDBLIB) $@
	-@ranlib    $@

$(SRCDIR)/%.h: ../include/%.h
	@rm -f $@
	cp $<  $@

$(SRCDIR)/%: %
	@rm -f $@
	cp $<  $@

$(INCDIR)/%: ../include/%
	@rm -f $@
	cp $<  $@

$(BINDIR)/%: %
	@rm -f $@
	cp $<  $@

cl: $(DERIV_SRC)
	# cmode
	# load -C $(CFLAGS) $(PURE_SRC) $(DERIV_SRC)

cleanexec:
	rm -f $(PROGRAMS)

clean: cleanexec
	-rm -f *.o \#*\# *~ *.bak
	-rm -f $(TDBLIB)
	-rm -rf td1 td2 td3 td4

TAGS: $(PURE_SRC) $(INTR_SRC) $(HEADER_SRC)
	etags -t $(PURE_SRC) $(INTR_SRC) $(HEADER_SRC)

DIST=/user/esther/mann/testtdb

dist:
	-mkdir -p $(DIST)/src
	-mkdir -p $(DIST)/include
	-mkdir -p $(DIST)/lib
	-cp Makefile makedep $(PURE_SRC) $(DERIV_SRC) $(INTER_SRC) $(DIST)/src
	-cp $(PROGRAMS:%=%.c) $(DIST)/src
	-cp id.h pds.h info.tab.h $(DIST)/src
	-cp $(HEADER_SRC) $(DIST)/include

#
# Rules for generating the dependencies below
#

depend:
	@./makedep

deriv_src: $(DERIV_SRC)

create_depends:
	@$(CC) $(DPND_FLAG) $(CFLAGS) $(DERIV_SRC) $(PURE_SRC)
	@$(CC) $(DPND_FLAG) $(CFLAGS) $(PROGRAMS:%=%.c)

# DO NOT DELETE THIS LINE -- makedep depends upon it.

# Dependencies for info.tab.o
info.tab.o: info.tab.c
info.tab.o: ../include/tdb_public.h
info.tab.o: ../include/tdb_defines.h
info.tab.o: ../include/tdb_struct.h
info.tab.o: ../include/tdb_proto.h

# Dependencies for info.yy.o
info.yy.o: info.yy.c
info.yy.o: ../include/tdb_public.h
info.yy.o: ../include/tdb_defines.h
info.yy.o: ../include/tdb_struct.h
info.yy.o: ../include/tdb_proto.h
info.yy.o: ../include/tdb_error.h
info.yy.o: info.tab.h

# Dependencies for date.tab.o
date.tab.o: date.tab.c

# Dependencies for date.yy.o
date.yy.o: date.yy.c
date.yy.o: ../include/tdb_public.h
date.yy.o: ../include/tdb_error.h
date.yy.o: date.tab.h

# Dependencies for df.o
df.o: df.c
df.o: ../include/tdb_public.h
df.o: ../include/tdb_defines.h
df.o: ../include/tdb_struct.h
df.o: ../include/tdb_proto.h
df.o: ../include/tdb_error.h
df.o: ../include/util.h
df.o: pds.h
df.o: info.tab.h
df.o: ../include/list.h
df.o: ../include/memfile.h
df.o: id.h

# Dependencies for di.o
di.o: di.c
di.o: ../include/tdb_public.h
di.o: ../include/tdb_defines.h
di.o: ../include/tdb_struct.h
di.o: ../include/tdb_proto.h
di.o: ../include/tdb_error.h
di.o: info.tab.h
di.o: ../include/util.h

# Dependencies for ds_parent.o
ds_parent.o: ds_parent.c
ds_parent.o: ../include/tdb_public.h
ds_parent.o: ../include/tdb_error.h
ds_parent.o: ../include/tdb_ds_proto.h

# Dependencies for ds_file.o
ds_file.o: ds_file.c
ds_file.o: ../include/tdb_public.h
ds_file.o: ../include/tdb_error.h
ds_file.o: ../include/tdb_defines.h
ds_file.o: ../include/tdb_struct.h
ds_file.o: ../include/tdb_ds_proto.h
ds_file.o: ../include/tdb_proto.h
ds_file.o: info.tab.h
ds_file.o: ../include/util.h
ds_file.o: id.h

# Dependencies for ds_join.o
ds_join.o: ds_join.c
ds_join.o: ../include/tdb_public.h
ds_join.o: ../include/tdb_error.h
ds_join.o: ../include/tdb_defines.h
ds_join.o: ../include/tdb_struct.h
ds_join.o: ../include/tdb_proto.h
ds_join.o: ../include/tdb_ds_proto.h
ds_join.o: id.h

# Dependencies for ds_exit.o
ds_exit.o: ds_exit.c
ds_exit.o: ../include/tdb_public.h
ds_exit.o: ../include/tdb_error.h
ds_exit.o: ../include/tdb_defines.h
ds_exit.o: ../include/tdb_struct.h
ds_exit.o: ../include/tdb_proto.h
ds_exit.o: ../include/tdb_ds_proto.h

# Dependencies for ds_signal.o
ds_signal.o: ds_signal.c
ds_signal.o: ../include/tdb_public.h
ds_signal.o: ../include/tdb_error.h
ds_signal.o: ../include/tdb_defines.h
ds_signal.o: ../include/tdb_struct.h
ds_signal.o: ../include/tdb_proto.h
ds_signal.o: ../include/tdb_ds_proto.h

# Dependencies for ds_global.o
ds_global.o: ds_global.c
ds_global.o: ../include/tdb_public.h
ds_global.o: ../include/tdb_error.h
ds_global.o: ../include/tdb_defines.h
ds_global.o: ../include/tdb_struct.h
ds_global.o: ../include/tdb_proto.h
ds_global.o: ../include/tdb_ds_proto.h
ds_global.o: ../include/util.h

# Dependencies for symbol.o
symbol.o: symbol.c
symbol.o: ../include/tdb_public.h
symbol.o: ../include/tdb_defines.h
symbol.o: ../include/tdb_struct.h
symbol.o: ../include/tdb_proto.h
symbol.o: ../include/tdb_error.h
symbol.o: info.tab.h
symbol.o: ../include/util.h

# Dependencies for tdb_error.o
tdb_error.o: tdb_error.c
tdb_error.o: ../include/tdb_error.h

# Dependencies for util.o
util.o: util.c
util.o: ../include/tdb_public.h
util.o: ../include/tdb_error.h
util.o: ../include/tdb_defines.h
util.o: ../include/util.h

# Dependencies for list.o
list.o: list.c
list.o: ../include/list.h

# Dependencies for memfile.o
memfile.o: memfile.c
memfile.o: ../include/memfile.h

# Dependencies for date_util.o
date_util.o: date_util.c

# Dependencies for id.o
id.o: id.c
id.o: ../include/tdb_public.h
id.o: ../include/tdb_error.h
id.o: id.h

# Dependencies for timer.o
timer.o: timer.c

# Dependencies for pds.o
pds.o: pds.c
pds.o: pds.h

# Dependencies for check_desc.o
check_desc.o: check_desc.c
check_desc.o: ../include/tdb_public.h
check_desc.o: ../include/tdb_error.h

# Dependencies for tdbgen.o
tdbgen.o: tdbgen.c
tdbgen.o: ../include/tdb_public.h
tdbgen.o: ../include/tdb_error.h

# Dependencies for generic.o
generic.o: generic.c
generic.o: ../include/tdb_public.h
generic.o: ../include/tdb_error.h
generic.o: ../include/tdb_defines.h
generic.o: ../include/tdb_struct.h
generic.o: ../include/tdb_proto.h

# Dependencies for tdb_dump.o
tdb_dump.o: tdb_dump.c
