include settings.mk

C_FILES=parser.c math/math.c main.c
OBJS=$(C_FILES:.c=.o)
BIN=mean

DATE=$(shell date '+%Y-%m-%d-%H%M%S')
REPORT_MD=report-$(DATE).md
REPORT_PDF=report-$(DATE).pdf

%.o:%.c
	$(CC) $(CFLAGS) $< -c -o $@

bin: $(BIN)

$(BIN): $(OBJS) Makefile
	@echo "Building $(BIN)"
	$(CC) $(OBJS) -o $@

clean:
	rm -fr $(OBJS) $(BIN) *~ latex html *.md *.pdf *.o
	cd test && make clean

doc:
	doxygen 
	cd latex && make

check-leaks: $(BIN)
	valgrind --leak-check=summary ./$(BIN) 2 4 6
	valgrind --leak-check=full ./$(BIN) `seq 1 100`
	valgrind --leak-check=full ./$(BIN) `seq 1 10000`
	valgrind --leak-check=full ./$(BIN) `seq 1 100000`


black-box: $(BIN)
	@echo Calling mean with 1 2 3
	./$(BIN) 1 2 3


	@echo Callin mean with 1 5 9
	./$(BIN) 1 5 9

check: $(OBJS)
	cd test && make check

big: $(BIN)
	START=`echo 2^30-2 | bc ` ; STOP=`echo 2^30-1 | bc` ; echo ./$(BIN) `seq  $$START  $$STOP `
#	./$(BIN) 100000000000 100000000000000

check-report: $(BIN)
	@echo "# Test report" > $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "##Date: ">> $(REPORT_MD)
	@echo "$(DATE)" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "##Computer: ">> $(REPORT_MD)
	@printf "**OS:**         `uname -o`\n\n" >> $(REPORT_MD)
	@printf "**Hostname:**   `uname -n`\n\n" >> $(REPORT_MD)
	@printf "**Kernel:**     `uname -s`\n\n" >> $(REPORT_MD)
	@printf "**Machine:**    `uname -m`\n\n" >> $(REPORT_MD)
	@printf "**Processor:**  `uname -p`\n\n" >> $(REPORT_MD)
	@printf "**Hardware:**   `uname -i`\n\n" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "# Compiler" >> $(REPORT_MD)
	@echo "**Compiler:** ">> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	@echo "`$(CC) --version` " >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "**Flags:**" >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	@echo "$(CFLAGS)" >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "# Unit tests" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	make check | grep -A 2 "Tests:" >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "# Memory leak" >> $(REPORT_MD)
	@echo "" >> $(REPORT_MD)
	@echo "\`\`\`" >> $(REPORT_MD)
	make check-leaks 2>&1 | grep -A 3 "HEAP SUMMARY" >> $(REPORT_MD) ; echo "RET=$$?"; if [ "$$?" = "0" ] ||  [ "$$?" = "0" ] ; then exit 0; else  exit 1; fi
	@echo "\`\`\`" >> $(REPORT_MD)
# Generate pdf
	pandoc $(REPORT_MD) -o $(REPORT_PDF)
# Send the report via email
#	echo "Test report (mean project) attached" | mailx -s "Test report (mean) $(DATE)" -A  $(REPORT_PDF) a-students-email@student.chalmers.se




