OCAPI_DIR := ..
INTERACTIVE_DIR := ../example_interactive
XDR_DIR := ./libxdr
CURL_DIR := ./curl_7.12.1
MEMWATCH_DIR := ./memwatch-2.71
PTHREAD_DIR := ./pthreads-win32
LIBS := -lwsock32 -lcurl -lpthreadGC2
RM := rm -rf
INTERACTIVE_SRCS += ${addprefix $(INTERACTIVE_DIR)/, interactive.c main.c}
XDR_SRCS += ${addprefix $(XDR_DIR)/, xdr_array.c xdr_float.c xdr_mem.c xdr_stdio.c xdr.c}
OCAPI_SRCS += ${addprefix $(OCAPI_DIR)/, buffers.c http.c ocapi.c utility.c stack.c data.c lex.dds.c dds.tab.c parser_DAS.c}
MEMWATCH_SRCS += ${addprefix $(MEMWATCH_DIR)/, memwatch.c}
CURL_include := "$(CURL_DIR)/include"
MEMWATCH_include := "$(MEMWATCH_DIR)"
PTHREAD_include := "$(PTHREAD_DIR)/include"
CURL_libs := "$(CURL_DIR)/lib"
XDR_libs := "$(XDR_DIR)"
PTHREAD_libs := "$(PTHREAD_DIR)/lib"

%.o: $(MEMWATCH_DIR)/%.c
	@echo 'Building Memwatch files: $<'
	@echo gcc -O0 -g3 -Wall -c -fmessage-length=0 -o$@ $<
	@gcc -O0 -g3 -Wall -c -fmessage-length=0 -o$@ $< && \
	echo -n $(@:%.o=%.d) '' > $(@:%.o=%.d) && \
	gcc -ansi -MM -MG -P -w -O0 -g3 -Wall -c -fmessage-length=0  $< >> $(@:%.o=%.d)
	@echo 'Finished building: $<'
	@echo ' '

%.o: $(INTERACTIVE_DIR)/%.c
	@echo 'Building interactive files: $<'
	@echo gcc -I$(CURL_DIR) -I$(CURL_libs) -std=c89 -pedantic -O0 -g3 -Wall -c -fmessage-length=0 -o$@ $<
	@gcc -I$(CURL_DIR) -I$(CURL_libs) -I$(MEMWATCH_DIR) -std=c89 -pedantic -O0 -g3 -Wall -DMEMWATCH -c -fmessage-length=0 -o$@ $< && \
	echo -n $(@:%.o=%.d) '' > $(@:%.o=%.d) && \
	gcc -ansi -MM -MG -P -w -I$(CURL_DIR) -I$(CURL_libs) -I$(MEMWATCH_DIR) -std=c89 -pedantic -O0 -g3 -Wall -DMEMWATCH -c -fmessage-length=0  $< >> $(@:%.o=%.d)
	@echo 'Finished building: $<'
	@echo ' '

%.o: $(XDR_DIR)/%.c
	@echo 'Building XDR files: $<'
	@echo gcc -O0 -g3 -Wall -c -fmessage-length=0 -o$@ $<
	@gcc -O0 -g3 -Wall -c -fmessage-length=0 -o$@ $< && \
	echo -n $(@:%.o=%.d) '' > $(@:%.o=%.d) && \
	gcc -ansi -MM -MG -P -w -std=c89 -pedantic -O0 -g3 -Wall -c -fmessage-length=0  $< >> $(@:%.o=%.d)
	@echo 'Finished building: $<'
	@echo ' '

%.o: $(OCAPI_DIR)/%.c
	@echo 'Building debugging OCAPI file: $<'
	@echo gcc -I$(CURL_DIR) -I$(CURL_libs) -I$(XDR_libs) -I$(PTHREAD_include) -std=c89 -pedantic -O0 -g3 -Wall -DMEMWATCH -c -fmessage-length=0 -o$@ $<
	@gcc -I$(CURL_DIR) -I$(CURL_libs) -I$(XDR_libs) -I$(MEMWATCH_DIR) -I$(PTHREAD_include) -pthread -std=c89 -pedantic -O0 -g3 -Wall -DMEMWATCH -c -fmessage-length=0 -o$@ $< && \
	echo -n $(@:%.o=%.d) '' > $(@:%.o=%.d) && \
	gcc -ansi -MM -MG -P -w -I$(CURL_DIR) -I$(CURL_libs) -I$(XDR_libs) -I$(MEMWATCH_DIR) -I$(PTHREAD_include) -I$(PTHREAD_libs) -std=c89 -pedantic -O0 -g3 -Wall -DMEMWATCH -c -fmessage-length=0  $< >> $(@:%.o=%.d)
	@echo 'Finished building: $<'
	@echo ' '

MEMWATCH_OBJS := $(MEMWATCH_SRCS:$(MEMWATCH_DIR)/%.c=%.o)
INTERACTIVE_OBJS := $(INTERACTIVE_SRCS:$(INTERACTIVE_DIR)/%.c=%.o)
XDR_OBJS := $(XDR_SRCS:$(XDR_DIR)/%.c=%.o)
OBJS := $(OCAPI_SRCS:$(OCAPI_DIR)/%.c=%.o)
DEPS := $(OCAPI_SRCS:$(OCAPI_DIR)/%.c=%.d) $(XDR_SRCS:$(XDR_DIR)/%.c=%.d)

-include $(DEPS)

all: OCAPI.exe

OCAPI.exe: $(MEMWATCH_OBJS) $(OBJS) $(XDR_OBJS) $(INTERACTIVE_OBJS)
	@echo 'Building target: $@'
	gcc -L$(CURL_include) -L$(CURL_libs) -L$(PTHREAD_include) -L$(PTHREAD_libs) -L$(MEMWATCH_include) -o $@ $(MEMWATCH_OBJS) $(INTERACTIVE_OBJS) $(XDR_OBJS) $(OBJS) $(USER_OBJS) $(LIBS)
	@echo 'Finished building: $@'
	ar -r libocapi.a $(OBJS) $(XDR_OBJS)

clean:
	-$(RM) $(MEMWATCH_OBJS) $(INTERACTIVE_OBJS) $(XDR_OBJS) $(OBJS) $(DEPS) OCAPI.exe libocapi.a

.PHONY: all clean dependents

-include $(OCAPI_DIR)/makefile.targets
