# Compiler and linker Configuration
CC       = gcc
LINK.c	 = gcc
INCLUDES = -I. -I../include -I/usr/local/include
MYCFLAGS = -Wall -ggdb -MMD
CFLAGS   = $(MYCFLAGS)
CFLAGS   += -DDEBUG -D_REENTRANT 
CFLAGS   += $(INCLUDES)
LFLAGS   = -L/usr/local/lib
LIBS	 += -lthread -lnsl 
RPCGENFLAGS     = -N -C -A 	

#
#   Program Configuration
#
DBS  = dbs
LIST = $(DBS)_list
TPC  = $(DBS)_tpc
RM   = res_man
TM   = trans_man

RM_DEF   =   ../include/$(RM).x
TM_DEF	 =   ../include/$(TM).x

TARGETS = $(LIST)_cat $(TPC)_create $(TPC) $(TPC)_test
SOURCES = $(TARGETS:%=%.c) utilities.c verify.c
DEPENDENCIES = $(SOURCES:.c=.d)

all : $(TARGETS)

$(LIST)_cat : $(LIST)_cat.o client_tm.o $(RM)_clnt.o $(RM)_xdr.o $(TM)_clnt.o $(TM)_xdr.o utilities.o verify.o error.o

$(TPC)_create : $(TPC)_create.o client_tm.o $(RM)_clnt.o  $(RM)_xdr.o $(TM)_clnt.o $(TM)_xdr.o utilities.o verify.o error.o

$(TPC) : $(TPC).o  client_tm.o $(RM)_clnt.o  $(RM)_xdr.o $(TM)_clnt.o $(TM)_xdr.o utilities.o  verify.o doOne.o error.o

$(TPC)_test : $(TPC)_test.o  client_tm.o $(RM)_clnt.o  $(RM)_xdr.o $(TM)_clnt.o $(TM)_xdr.o utilities.o verify.o	error.o

#
# Resource Manager Protocol Configuration
#
../include/$(RM).h: $(RM_DEF)
	rpcgen $(RPCGENFLAGS) -h -o ../include/$(RM).h $(RM_DEF)
$(RM)_xdr.c: $(RM_DEF)
	rpcgen $(RPCGENFLAGS) -c -o $(RM)_xdr.c $(RM_DEF)
$(RM)_clnt.c: $(RM_DEF)
	rpcgen $(RPCGENFLAGS) -l -o $(RM)_clnt.c $(RM_DEF)

#
# Transaction Manager Protocol Configuration
#
$(TM)_xdr.o : CFLAGS += -D_DBS_PROT_XDR_
../include/$(TM).h: $(TM_DEF)
	rpcgen $(RPCGENFLAGS) -h -o ../include/$(TM).h $(TM_DEF)
$(TM)_xdr.c: $(TM_DEF)
	rpcgen $(RPCGENFLAGS) -c -o $(TM)_xdr.c $(TM_DEF)
$(TM)_clnt.c: $(TM_DEF)
	rpcgen $(RPCGENFLAGS) -l -o $(TM)_clnt.c $(TM_DEF)

#
# Dependency
#
$(SOURCES) : ../include/$(RM).h ../include/$(TM).h

%: %.o
	$(CC) -o $@ $^ $(LFLAGS) $(LIBS)

%.d: %.c
	touch $@

#
# Cleanup
#
clean:
	rm -f *.o ../include/*.o $(RM).h $(TM).h $(TARGETS) *.d

really_clean: clean
	rm -f *.d *~ res_man_clnt.c res_man_xdr.c trans_man_clnt.c \
		trans_man_xdr.c log_man_clnt.c log_man_xdr.c 

include $(DEPENDENCIES)
