#=----------------------------------------------------------------------------=#
#   Makefile                                                                   #
#                                                                              #
#   Copyright © 2021 The Gem-graph Project                                     #
#                                                                              #
#   This file is part of gem-graph.                                            #
#                                                                              #
#   This program is free software: you can redistribute it and/or modify       #
#   it under the terms of the GNU Affero General Public License as             #
#   published by the Free Software Foundation, either version 3 of the         #
#   License, or (at your option) any later version.                            #
#                                                                              #
#   This program is distributed in the hope that it will be useful,            #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
#   GNU Affero General Public License for more details.                        #
#                                                                              #
#   You should have received a copy of the GNU Affero General Public License   #
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.     #
#=----------------------------------------------------------------------------=#

CCOPTS=-pthread -Wall -g -Os
LDFLAGS= -lc -lpthread
BINDIR=bin
INCDIR=include
SRCDIR=src
DEBDIR=debian
SERVEROBJ=	$(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/localworker.o \
		$(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \
		$(BINDIR)/main.o $(BINDIR)/arrows.o
CLIOBJ=		$(BINDIR)/cli.o

.DEFAULT_GOAL:= all
.PHONY: all clean deb tests install run run-cli run-tests

# ---- Tests enumeration ----------------------------------------------------- #
TEST_SCHEDULER=$(BINDIR)/tests/centers
TESTS=$(TEST_SCHEDULER)

$(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c
	@echo "Compiling $<"
	@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) $(LDFLAGS) -o $@ $<

# ---- General recipes ------------------------------------------------------- #

$(BINDIR)/%.o:	$(SRCDIR)/%.c $(INCDIR)/%.h $(INCDIR)/base.h
	@echo "Compiling $<"
	@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<

# ---- Main recipe ----------------------------------------------------------- #
$(BINDIR)/main.o:	$(SRCDIR)/main.c $(INCDIR)/base.h
	@echo "Compiling $<"
	@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<

$(BINDIR)/gem-graph-server:	$(SERVEROBJ) $(INCDIR)/base.h
	@echo "Building program to $@"
	@$(CC) -o $@ $(SERVEROBJ) $(LDFLAGS)
	@echo "Success!"

$(BINDIR)/gem-graph-ctl:	$(CLIOBJ) $(INCDIR)/base.h
	@echo "Building program to $@"
	@$(CC) -o $@ $(CLIOBJ) $(LDFLAGS)
	@echo "Success!"

# ---- Misc recipes ---------------------------------------------------------- #
clean:
	-rm -f $(SRCDIR)/*.o $(BINDIR)/* $(BINDIR)/tests/* *.deb

all: $(BINDIR)/gem-graph-server $(BINDIR)/gem-graph-ctl

tests: $(TESTS)

# ---- Build debian package -------------------------------------------------- #
$(BINDIR)/gem-graph-server.deb: all
	mkdir -p gem-graph-server/usr/bin
	mkdir -p gem-graph-server/usr/share/doc/gem-graph-server
	mkdir -p gem-graph-server/DEBIAN
	cp $(DEBDIR)/Manifest gem-graph-server/DEBIAN/control
	cp $(BINDIR)/gem-graph-server gem-graph-server/usr/bin
	cp $(DEBDIR)/copyright gem-graph-server/usr/share/doc/gem-graph-server/copyright
	dpkg-deb --build gem-graph-server
	rm -rf gem-graph-server

deb: $(BINDIR)/gem-graph-server.deb

# ---- Builder will call this to install the application before running ------ #
install:
	echo "Installing is not supported"

# ---- Builder uses this target to run your application ---------------------- #
run: all
	bin/gem-graph-server
run-cli: all
	bin/gem-graph-ctl
run-tests: tests
	bin/tests/scheduler