# SYNOPSIS:
#
#   make [all]      - makes everything.
#   make TARGET     - makes the given target.
#   make run_tests  - makes everything and runs all test
#   make run-%      - run specific test file (exclude .py)
#                     replace % with given test file
#   make clean      - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.


# Where to find user code.
USER_DIR = ../src

# Where to find test code.
TEST_DIR = ../test

INCLUDES = -I$(USER_DIR) -I$(TEST_DIR)
# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.
CPPFLAGS += -DUNIT_TEST -D_IR_LOCALE_=en-AU

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread -std=gnu++11

objects = $(patsubst %.cpp,%,$(wildcard *.cpp))

all : $(objects)

run_tests : all
	failed=""; \
	for py_unittest in *_test.py; do \
	  echo "RUNNING: $${py_unittest}"; \
		python3 ./$${py_unittest} || failed="$${failed} $${py_unittest}"; \
	done; \
	for shell_unittest in *_test.sh; do \
	  echo "RUNNING: $${shell_unittest}"; \
		bash ./$${shell_unittest} || failed="$${failed} $${shell_unittest}"; \
	done; \
	if [ -n "$${failed}" ]; then \
		echo "FAIL: :-( :-( Unit test(s)$${failed} failed! :-( :-("; exit 1; \
	else \
		echo "PASS: \o/ \o/ All unit tests passed. \o/ \o/"; \
	fi

run-% : all
	echo "RUNNING: $*"; \
	python3 ./$*.py;

clean :
	rm -f  *.o *.pyc $(objects)


# Keep all intermediate files.
.SECONDARY:

# All the IR protocol object files.
PROTOCOL_OBJS = $(patsubst %.cpp,%.o,$(wildcard $(USER_DIR)/ir_*.cpp))
PROTOCOLS = $(patsubst $(USER_DIR)/%,%,$(PROTOCOL_OBJS))

# Common object files
COMMON_OBJ = IRutils.o IRtimer.o IRsend.o IRrecv.o IRtext.o IRac.o $(PROTOCOLS)

# Common dependencies
COMMON_DEPS = $(USER_DIR)/IRrecv.h $(USER_DIR)/IRsend.h $(USER_DIR)/IRtimer.h \
              $(USER_DIR)/IRutils.h $(USER_DIR)/IRremoteESP8266.h \
							$(TEST_DIR)/IRsend_test.h $(USER_DIR)/IRtext.h $(USER_DIR)/i18n.h
# Common test dependencies
COMMON_TEST_DEPS = $(COMMON_DEPS) $(TEST_DIR)/IRsend_test.h

IRtext.o : $(USER_DIR)/IRtext.cpp $(USER_DIR)/IRtext.h $(USER_DIR)/IRremoteESP8266.h $(USER_DIR)/i18n.h $(USER_DIR)/locale/*.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $(USER_DIR)/IRtext.cpp

IRutils.o : $(USER_DIR)/IRutils.cpp $(USER_DIR)/IRutils.h $(USER_DIR)/IRremoteESP8266.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/IRutils.cpp

IRsend.o : $(USER_DIR)/IRsend.cpp $(USER_DIR)/IRsend.h $(USER_DIR)/IRremoteESP8266.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/IRsend.cpp

IRrecv.o : $(USER_DIR)/IRrecv.cpp $(USER_DIR)/IRrecv.h $(USER_DIR)/IRremoteESP8266.h $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/IRrecv.cpp

# new specific targets goes above this line

$(objects) : %: $(COMMON_OBJ) %.o
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

ir_%.o : $(USER_DIR)/ir_%.h $(USER_DIR)/ir_%.cpp $(COMMON_DEPS) $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $(USER_DIR)/ir_$*.cpp

ir_%.o : $(USER_DIR)/ir_%.cpp $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $(USER_DIR)/ir_$*.cpp

%.o : %.cpp $(COMMON_TEST_DEPS) $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $*.cpp

%.o : $(USER_DIR)/%.cpp $(USER_DIR)/%.h $(COMMON_DEPS) $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(INCLUDES) -c $(USER_DIR)/$*.cpp
