#================================================================================
EXEC_NAME=vlkb-obscore
INST_NAME=test
DEBUG_LEV=-v1
INSTALL_DIR=/usr/local
VERSION ?= $(shell git describe)
TAR_NAME := `basename $(PWD)`
#================================================================================
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)
#================================================================================
INC_DIR = $(DEPS_INC) \
	  /usr/include/cfitsio \
	  /usr/local/cfitsio/include
LIB_DIR = $(DEPS_LIB) \
	  /usr/lib64/ast \
	  /usr/local/lib \
	  /usr/local/cfitsio/lib
#================================================================================
CC=g++
CFLAGS_DEBUG   = -g -DFDB_DEBUG
CFLAGS_RELEASE = -O2 
FLAGS_COMMON   = -fPIC -Wall -Wextra -Wconversion -fno-common -pthread -DVERSIONSTR='"$(VERSION)"' \
-DBUILD='"$(shell LANG=us_US date; hostname)"'
CFLAGS_COMMON   = -c -Wstrict-prototypes $(FLAGS_COMMON)
CXX_DEBUG_FLAGS   = -g -DVERBOSE_DEBUG -DFDB_DEBUG
CXX_RELEASE_FLAGS = -O3
# libpqxx 7.7 needs: /usr/include/pqxx/array.hxx:85:3: note: ‘std::string_view’ is only available from C++17 onwards
CXX_DEFAULT_FLAGS = -c -std=c++17 $(FLAGS_COMMON)
LDFLAGS = -Wall -lvlkbcommon -lcfitsio -lpq -lpqxx -lcsv -last -last_grf_2.0 -last_grf_3.2 -last_grf_5.6 -last_grf3d -last_err -lstdc++ -lm  
INC_PARM=$(foreach d, $(INC_DIR), -I$d)
LIB_PARM=$(foreach d, $(LIB_DIR), -L$d)
#================================================================================
EXT_DIR = ext
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
DB_SRC_DIR=$(SRC_DIR)/database
DB_OBJ_DIR=$(OBJ_DIR)/database
INC_PARM += -I$(EXT_DIR)/include -I$(SRC_DIR)
LIB_PARM += -L$(EXT_DIR)/lib
#================================================================================
EXECUTABLE = $(BIN_DIR)/$(EXEC_NAME)
DB_CPP_FILES  = $(wildcard $(SRC_DIR)/database/*.cpp)
DB_OBJ_FILES  = $(addprefix $(DB_OBJ_DIR)/,$(notdir $(DB_CPP_FILES:.cpp=.o))) 
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 run debug release clean

all: debug 

.PHONY: run 
run: debug
	$(EXECUTABLE) $(INST_NAME) $(DEBUG_LEV)

release: CFLAGS   += $(CFLAGS_RELEASE) $(CFLAGS_COMMON)
release: CXXFLAGS += $(CXX_RELEASE_FLAGS) $(CXX_DEFAULT_FLAGS)
release: $(EXECUTABLE)

debug: CFLAGS   += $(CFLAGS_DEBUG) $(CFLAGS_COMMON)
debug: CXXFLAGS += $(CXX_DEBUG_FLAGS) $(CXX_DEFAULT_FLAGS)
debug: $(EXECUTABLE)

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

$(DB_OBJ_DIR)/%.o: $(DB_SRC_DIR)/%.cpp
	$(CC) $(CXXFLAGS) $(INC_PARM) -o $@ $<

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

clean:
	-rm -rf $(OBJ_DIR) $(BIN_DIR) $(EXT_DIR)

.PHONY: echo
echo:
	@echo EXECUTABLE:
	@echo $(EXECUTABLE)
	@echo CPP FILES:
	@echo $(CPP_FILES)
	@echo OBJ_FILES:
	@echo $(OBJ_FILES)
	@echo DB_OBJ_FILES:
	@echo $(DB_OBJ_FILES)
	@echo INC_PARM
	@echo $(INC_PARM)
	@echo LIB_PARM
	@echo $(LIB_PARM)
	@echo installedEXE
	@echo $(INSTALL_DIR)/$(EXEC_NAME)$(SUFFIX)



# release tar.gz

.PHONY: $(DEPS_DIR)
$(DEPS_DIR):
	make -C $@ $(DEPS_TARGET)

.PHONY: deps
deps : $(DEPS_DIR)
	mkdir -p $(EXT_DIR)
	cp -r $(DEPS_INC) $(EXT_DIR)
	cp -r $(DEPS_LIB) $(EXT_DIR)

.PHONY: deps-clean
deps-clean : DEPS_TARGET=clean
deps-clean : $(DEPS_DIR)

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

.PHONY: tar
tar: deps
	-tar -czvf $(TAR_NAME)-$(VERSION).tar.gz --transform="s|^|$(TAR_NAME)-$(VERSION)/|" $(PROTO_DIR) $(SRC_DIR) $(EXT_DIR) Makefile



# release rpm deb

.PHONY: rpm
rpm: RPM_ROOT=rpmbuild
rpm: release
	mkdir -p $(RPM_ROOT)/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
	cp $(EXEC_NAME).spec $(RPM_ROOT)/SPECS
	cp bin/$(EXEC_NAME) $(RPM_ROOT)/SOURCES
	rpmbuild -bb --define "_topdir `pwd`/$(RPM_ROOT)"  --define "_prefix /usr/local" --define "version $(shell git describe | sed -r 's/-/./g')"  $(EXEC_NAME).spec
	find $(RPM_ROOT)/RPMS/* -name '*.rpm' -print0 | xargs -0 cp -t .
	rm -fr $(RPM_ROOT)


.PHONY: deb
deb: DEB_ROOT=debbuild
deb: PREFIX=$(DEB_ROOT)/$(EXEC_NAME)/usr/local
deb:
	mkdir -p $(DEB_ROOT)/$(EXEC_NAME)/DEBIAN $(PREFIX)
	mkdir -p $(PREFIX)/bin $(PREFIX)/etc/$(EXEC_NAME)
	mkdir -p $(PREFIX)/share/doc/$(EXEC_NAME)
	mkdir -p $(PREFIX)/share/man/man1
	sed 's/Version:.*/Version: $(VERSION)/' $(EXEC_NAME).control > $(DEB_ROOT)/$(EXEC_NAME)/DEBIAN/control
	echo "/usr/local/etc/$(EXEC_NAME)/datasets.conf" > $(DEB_ROOT)/$(EXEC_NAME)/DEBIAN/conffiles
	cp bin/$(EXEC_NAME) $(PREFIX)/bin
	cp $(EXEC_NAME).datasets.conf $(PREFIX)/etc/$(EXEC_NAME)/datasets.conf
	cp $(EXEC_NAME).changelog.Debian $(PREFIX)/share/doc/$(EXEC_NAME)/changelog.Debian
	cp $(EXEC_NAME).copyright $(PREFIX)/share/doc/$(EXEC_NAME)/copyright
	cp $(EXEC_NAME).1 $(PREFIX)/share/man/man1/$(EXEC_NAME).1
	gzip --best -n $(PREFIX)/share/man/man1/$(EXEC_NAME).1
	gzip --best -n $(PREFIX)/share/doc/$(EXEC_NAME)/changelog.Debian
	cd $(DEB_ROOT) && dpkg-deb --root-owner-group --build $(EXEC_NAME) && mv $(EXEC_NAME).deb ../$(EXEC_NAME)_$(VERSION).deb && cd -
	rm -fr $(DEB_ROOT)


# gitlab Packages doc: https://docs.gitlab.com/ee/user/packages/generic_packages/
# make up/download PACK_EXT = rpm | deb
.PHONY: upload
upload: PACK_FILE := $(shell ls -t $(EXEC_NAME)*.$(PACK_EXT) | head -1)
upload: GITLAB_PROJ_ID := 79
upload: GITLAB_PROJ_NAME := $(shell basename -s .git `git config --get remote.origin.url`)
upload: VER_MAJOR := $(shell echo $(VERSION) | cut -f1 -d.)
upload: VER_MINOR := $(shell echo $(VERSION) | cut -f2 -d.)
upload: PACK_URL := "https://ict.inaf.it/gitlab/api/v4/projects/$(GITLAB_PROJ_ID)/packages/generic/$(GITLAB_PROJ_NAME)/$(VER_MAJOR).$(VER_MINOR)/$(PACK_FILE)"
upload:
	curl --header "PRIVATE-TOKEN: glpat-CJZDcks7bYqE__ePn4J6" --upload-file $(PACK_FILE) $(PACK_URL)


.PHONY: download
#download: PACK_FILE := $(EXEC_NAME)-$(shell echo $(VERSION) | sed -r "s/-/./g ")-1.x86_64.rpm
download: PACK_FILE := $(EXEC_NAME)_$(VERSION).deb
download: GITLAB_PROJ_ID := 79
download: GITLAB_PROJ_NAME := $(shell basename -s .git `git config --get remote.origin.url`)
download: VER_MAJOR := $(shell echo $(VERSION) | cut -f1 -d.)
download: VER_MINOR := $(shell echo $(VERSION) | cut -f2 -d.)
download: PACK_URL := "https://ict.inaf.it/gitlab/api/v4/projects/$(GITLAB_PROJ_ID)/packages/generic/$(GITLAB_PROJ_NAME)/$(VER_MAJOR).$(VER_MINOR)/$(PACK_FILE)"
download:
	curl -O --header "PRIVATE-TOKEN: glpat-CJZDcks7bYqE__ePn4J6" $(PACK_URL)

