#
#  Copyright (C) 2015-2020, Thomas Maier-Komor
#
#  This file is part of hostscope.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

PREFIX		= /var/tmp/jwm
MKDIR		= /usr/bin/mkdir
RMDIR		= /bin/rmdir
RM		= /bin/rm
SED		= /usr/bin/sed
DIFF		= /usr/bin/diff
INSTALL		= /usr/bin/install -c

PROTOC		= 
PROTOBUF_LIBS	= 
PROTOBUF_CFLAGS	= 
PBSRC		= 
PBOBJ		= $(PBSRC:%.proto=build/%.pb.o)
PBFILES		= $(PBSRC:%.proto=%.pb.cc) $(PBSRC:%.proto=%.pb.h)

WFC		= wfc
WFCSRC		= hostscope.wfc

CXX		= g++

DEFS		= -DWFC  -D__STDC_FORMAT_MACROS '-DPREFIX="$(PREFIX)"'
CXXFLAGS	= -g -Wall $(DEFS) $(PROTOBUF_CFLAGS)
LDFLAGS		=

COMMONSRC	= common.cc log.cc $(PBSRC:%.proto=%.pb.cc) $(WFCSRC:%.wfc=%_wfc.cc)
COMMONOBJ	= $(COMMONSRC:%.cc=build/%.o)

CCXXSRC		= hostscope.cc
CCXXOBJ		= $(CCXXSRC:%.cc=build/%.o)

DCXXSRC		= hostscoped.cc linuxcpu.cc linuxdisk.cc linuxprocs.cc \
		  linuxmem.cc linuxnets.cc linuxpower.cc zfs.cc
DCXXOBJ		= $(DCXXSRC:%.cc=build/%.o)

DEPS		= $(DCXXSRC:%.cc=build/%.d) $(CCXXSRC:%.cc=build/%.d) $(COMMONSRC:%.cc=build/%.d)

ALLSRCS		= $(PBSRC) $(CSRC) $(COMMONSRC) $(CCXXSRC) $(DCXXSRC)

DLIBS		= -lrt $(PROTOBUF_LIBS)
CLIBS		= -lncurses -lrt $(PROTOBUF_LIBS)


all: build | deps release

deps:		$(DEPS)

release:	CXXFLAGS=-g -O2 -Wall $(DEFS)
release:	build hostscoped hostscope hostscopebr

debug:		CXXFLAGS=-g -DDEBUG -Wall $(DEFS)
debug:		build hostscoped hostscope hostscopebr

profile:	CXXFLAGS=-g -O2 -Wall -DPROFILE -I../librta $(DEFS)
profile:	LIBS	= -L../librta -Xlinker -R../librta -lrta
profile:	build hostscoped hostscope hostscopebr

settings:
	@echo "VERSION=$(VERSION)"
	@echo "CXXFLAGS=$(CXXFLAGS)"
	@echo "PBFILES=$(PBFILES)"
	@echo $(DEPS)

%.pb.h %.pb.cc: %.proto
	$(PROTOC) --cpp_out=. $<

md5: $(CCXXSRC) $(DCXXSRC) $(COMMONSRC) $(PBFILES) $(WFCSRC)
	md5sum $^

hostscope_wfc.cc: hostscope.wfc
	$(WFC) -o $(@:.cc=) $<
	mv hostscope_wfc.cpp $@

clean:
	$(RM) $(DCXXOBJ) $(CCXXOBJ) $(DEPS) $(COMMONOBJ) $(PBFILES) .depend
	$(RMDIR) build

build:
	-$(MKDIR) build

install: hostscope hostscoped
	$(MKDIR) -p $(PREFIX)/bin $(PREFIX)/sbin $(PREFIX)/etc
	cp -n hostscope.conf $(PREFIX)/etc
	$(INSTALL) -v -s -m 0755 hostscope $(PREFIX)/bin
	$(INSTALL) -v -s -m 0755 -o root hostscopebr $(PREFIX)/sbin || $(INSTALL) -v -s -m 0755 hostscopebr $(PREFIX)/sbin
	$(INSTALL) -v -s -m 04755 -o root hostscoped $(PREFIX)/sbin || $(INSTALL) -v -s -m 0755 hostscoped $(PREFIX)/sbin

hostscope: $(CCXXOBJ) $(COMMONOBJ)
	$(CXX) $(LDFLAGS) $(CXXFLAGS) $^ -o $@ $(CLIBS) $(LIBS)

hostscoped: $(DCXXOBJ) $(COMMONOBJ)
	$(CXX) $(LDFLAGS) $(CXXFLAGS) $^ -o $@ $(DLIBS) $(LIBS)

hostscopebr: build/hostscopebr.o $(COMMONOBJ)
	$(CXX) $(LDFLAGS) $(CXXFLAGS) $^ -o $@ $(DLIBS) $(LIBS)

always:

version.h: always
	sh mkversion.sh

build/%.d: %.c
	$(CC) -MM $(CFLAGS) $< > $@

build/%.d: %.cc
	$(CXX) -MM -MG -MT$(@:.d=.o) $(CXXFLAGS) $< > $@

build/%.d: %.cpp
	$(CXX) -MM -MG -MT$(@:.d=.o) $(CXXFLAGS) $< > $@

build/%.o: %.cc
	$(CXX) $(CXXFLAGS) -c $< -o $@

build/%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

.depend: build version.h | $(DEPS)
	cat $(DEPS) > .depend

-include .depend
