# default target
.PHONY: all clean distclean realclean help
all:

# variables
AUTODEPENDDIR:=.deps/

CXXDEPEND=$(CXX) -MM

# configuration
Makefile.config:
	@echo "You need to run ./configure.sh before make"
	@exit 1

ifeq "Makefile.config" "$(wildcard Makefile.config)"
include Makefile.config
endif

SKIPAUTODEPS+=$(if $(findstring clean,$(MAKECMDGOALS)),yes)
SKIPAUTODEPS+=$(if $(findstring help,$(MAKECMDGOALS)),yes)

# object files
OBJECTS:=$(patsubst %.cpp,%.o,$(shell find . -name '*.cpp'))

ifneq "1" "$(HAS_MYSQL)"
OBJECTS:=$(filter-out %MySQL.o,$(OBJECTS))
endif

ifneq "1" "$(HAS_POSTGRESQL)"
OBJECTS:=$(filter-out %Postgres.o,$(OBJECTS))
endif


# load in generated dependencies, if they exist
ifeq "$(strip $(SKIPAUTODEPS))" ""
PHONYVARIABLE:=$(shell mkdir -p $(AUTODEPENDDIR))
include $(shell find $(AUTODEPENDDIR) -name '*.d' -type f)
endif

# rules
%.o: %.cpp Makefile.config
ifeq "$(strip $(SKIPAUTODEPS))" ""
	$(CXXDEPEND) $(INCLUDES) $(CXXFLAGS) -MT $@ $< >$(AUTODEPENDDIR)$(basename $(notdir $@)).d
endif
	$(CXX) $(INCLUDES) $(CXXFLAGS) -c -o $@ $<

# targets
all: typearque

typearque: $(OBJECTS)
	$(LINKER) -o typearque $(OBJECTS) $(LDFLAGS)

clean:
	$(RM) $(OBJECTS)
	$(RM) -r $(AUTODEPENDDIR)

realclean: clean
	$(RM) typearque
	$(RM) Makefile.config

distclean: realclean

help:
	@echo "This is a make file for Type-ARQUE."
	@echo
	@echo "Make targets:"
	@echo '  all        Compiles `typearque'\'". Please run ./configure.sh first."
	@echo "  help       Prints this help."
	@echo "  clean      Removes all intermediates."
	@echo "  realclean  Removes every generated and compiled file and directory."
	@echo "  distclean  Synonym for realclean."
