#================================================================================
EXEC_NAME=vlkb
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/davix \
	  /usr/include/cfitsio \
	  /usr/local/include \
	  /usr/local/cfitsio/include
LIB_DIR = $(DEPS_LIB) \
	  /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
CXX_DEFAULT_FLAGS = -c -std=c++11 $(FLAGS_COMMON)
LDFLAGS = -Wall -lvlkbcommon -ldavix -lcfitsio -lcsv -last -last_pal -last_cminpack -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
INC_PARM += -I$(EXT_DIR)/include -I$(SRC_DIR)
LIB_PARM += -L$(EXT_DIR)/lib
#================================================================================
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 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) $(OBJ_FILES) $(LIB_PARM) $(LDFLAGS) -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)


gdb:
	gdb $(BIN_DIR)/$(EXEC_NAME) -x vlkb.gdb

# 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 vlkb.spec $(RPM_ROOT)/SPECS
	cp bin/vlkb $(RPM_ROOT)/SOURCES
	rpmbuild -bb --define "_topdir `pwd`/$(RPM_ROOT)"  --define "_prefix /usr/local" --define "version $(shell git describe | sed -r 's/-/./g')"  vlkb.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)/vlkb/usr/local
deb:
	mkdir -p $(DEB_ROOT)/vlkb/DEBIAN $(PREFIX)
	mkdir -p $(PREFIX)/bin $(PREFIX)/etc/vlkb
	mkdir -p $(PREFIX)/share/doc/vlkb
	mkdir -p $(PREFIX)/share/man/man1
	sed 's/Version:.*/Version: $(VERSION)/' vlkb.control > $(DEB_ROOT)/vlkb/DEBIAN/control
	echo "/usr/local/etc/vlkb/datasets.conf" > $(DEB_ROOT)/vlkb/DEBIAN/conffiles
	cp bin/vlkb $(PREFIX)/bin
	cp vlkb.datasets.conf $(PREFIX)/etc/vlkb/datasets.conf
	cp vlkb.changelog.Debian $(PREFIX)/share/doc/vlkb/changelog.Debian
	cp vlkb.copyright $(PREFIX)/share/doc/vlkb/copyright
	cp vlkb.1 $(PREFIX)/share/man/man1/vlkb.1
	gzip --best -n $(PREFIX)/share/man/man1/vlkb.1
	gzip --best -n $(PREFIX)/share/doc/vlkb/changelog.Debian
	cd $(DEB_ROOT) && dpkg-deb --root-owner-group --build vlkb && mv vlkb.deb ../vlkb_$(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)

