#================================================================================
EXEC_NAME = vlkbd
VERSION ?= $(shell git describe)
BUILD_ ?= $(shell LANG=us_US date; hostname)
#================================================================================
DEPS_DIR := ../common ../../ext/aria-csv ../../ext/nlohmann-json
DEPS_INC := $(foreach d, $(DEPS_DIR), $d/include)
DEPS_LIB := $(foreach d, $(DEPS_DIR), $d/lib)
#================================================================================
COMMON_DIR=../common
COMMON_LIB = $(COMMON_DIR)/lib/libvlkbcommon.a
#================================================================================
INC_DIR=src $(DEPS_INC) $(COMMON_DIR)/include  ../../ext \
	/usr/include/cfitsio \
	/usr/include/postgresql
LIB_DIR= $(DEPS_LIB) $(COMMON_DIR)/lib /usr/lib64/ast /usr/local/lib
#================================================================================
CC=g++
CXX_DEBUG_FLAGS=-g -DFDB_DEBUG
CXX_RELEASE_FLAGS=-O2
CXX_DEFAULT_FLAGS=-c -x c++ -std=c++11 -fPIC -Wall -Wextra -Wconversion -fno-common -DVERSIONSTR='"$(VERSION)"' -DBUILD='"$(BUILD_)"'
# FIXME: -last_pal missing in some builds (not realluy needed only for linker)
LDFLAGS = -Wall -lvlkbcommon -lpq -lpqxx -lcfitsio -lrabbitmq -last -last_grf_2.0 -last_grf_3.2 -last_grf_5.6 -last_grf3d -last_err -pthread -lstdc++ -lm
INC_PARM=$(foreach d, $(INC_DIR), -I$d)
LIB_PARM=$(foreach d, $(LIB_DIR), -L$d)
#================================================================================
SRC_DIR=src
OBJ_DIR=obj
BIN_DIR=bin
#================================================================================
EXECUTABLE	:= $(BIN_DIR)/$(EXEC_NAME)
CPP_FILES 	:= $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES 	:= $(addprefix $(OBJ_DIR)/,$(notdir $(CPP_FILES:.cpp=.o)))
#================================================================================
NPROCS = $(shell grep -c 'processor' /proc/cpuinfo)
MAKEFLAGS += -j$(NPROCS)
#================================================================================

.PHONY: all
all : debug

.PHONY: release
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS) $(CXX_DEFAULT_FLAGS)
release: $(EXECUTABLE)

.PHONY: debug
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS) $(CXX_DEFAULT_FLAGS)
debug: $(EXECUTABLE)

$(EXECUTABLE) : $(COMMON_LIB) makedir $(OBJ_FILES)
	$(CC) $(OBJ_FILES) $(LIB_PARM) $(LDFLAGS) -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	        $(CC) $(CXXFLAGS) $(INC_PARM) -o $@ $<

.PHONY: $(COMMON_LIB)
$(COMMON_LIB) :
	   make -C $(COMMON_DIR)

.PHONY: makedir
makedir:
	-mkdir -p $(OBJ_DIR) $(BIN_DIR)


.PHONY: clean
clean :
	-rm -fr $(OBJ_DIR) $(BIN_DIR)



.PHONY: test
test :
	@tabs 20
	@echo -e "EXEC_NAME:\t"  $(EXEC_NAME)
	@echo -e "VERSION:\t"  $(VERSION)
	@echo -e "CPP_FILES:\t"  $(CPP_FILES)
	@echo -e "OBJ_FILES:\t"  $(OBJ_FILES)
	@echo -e "C_FILES:\t"  $(C_FILES)
	@echo -e "C_OBJ_FILES:\t"  $(C_OBJ_FILES)
	@echo -e "INC_PARM:\t"  $(INC_PARM)
	@echo -e "LIB_PARM:\t"  $(LIB_PARM)


