STRIP   ?= strip
DESTDIR ?= dist
INSTALL ?= install

SUBDIRS := $(sort $(dir $(wildcard   */Makefile))) \
           $(sort $(dir $(wildcard */*/Makefile)))

%-all: %
	$(MAKE) -C $<

all: $(addsuffix -all,$(SUBDIRS))

install-bin: all
	$(INSTALL) -m 755 -d    $(DESTDIR)/usr/bin
	$(INSTALL) -m 755       src/radangel $(DESTDIR)/usr/bin
	$(INSTALL) -m 755       src/websockify/websockify $(DESTDIR)/usr/bin
	$(INSTALL) -m 755       src/inotail/inotail $(DESTDIR)/usr/bin
	$(STRIP)   -s           $(DESTDIR)/usr/bin/radangel
	$(STRIP)   -s           $(DESTDIR)/usr/bin/websockify
	$(STRIP)   -s           $(DESTDIR)/usr/bin/inotail

uninstall-bin:
	rm -f $(DESTDIR)/usr/bin/radangel
	rm -f $(DESTDIR)/usr/bin/websockify
	rm -f $(DESTDIR)/usr/bin/inotail

install-html:
	$(INSTALL) -m 755 -d    $(DESTDIR)/usr/share/radangel/html
	$(INSTALL) -m 644       html/index.html $(DESTDIR)/usr/share/radangel/html
	$(INSTALL) -m 755 -d    $(DESTDIR)/usr/share/radangel/html/js
	$(INSTALL) -m 644       html/js/streamplot.js $(DESTDIR)/usr/share/radangel/html/js
	$(INSTALL) -m 644       html/js/chroma.min.js $(DESTDIR)/usr/share/radangel/html/js
	$(INSTALL) -m 644       html/js/plotly-cartesian-latest.min.js $(DESTDIR)/usr/share/radangel/html/js

uninstall-html:
	rm -rf $(DESTDIR)/usr/share/radangel

install-config:
	$(INSTALL) -m 755 -d    $(DESTDIR)/lib/systemd/system
	$(INSTALL) -m 644       service/datasrc.service $(DESTDIR)/lib/systemd/system
	$(INSTALL) -m 644       service/radangel.service $(DESTDIR)/lib/systemd/system
	$(INSTALL) -m 644       service/websockify.service $(DESTDIR)/lib/systemd/system
	$(INSTALL) -m 755 -d    $(DESTDIR)/etc/nginx/conf.d
	$(INSTALL) -m 644       config/radangel.conf $(DESTDIR)/etc/nginx/conf.d
	$(INSTALL) -m 755 -d    $(DESTDIR)/lib/udev/rules.d
	$(INSTALL) -m 644       config/60-radangel.rules $(DESTDIR)/lib/udev/rules.d

uninstall-config:
	rm -f $(DESTDIR)/lib/systemd/system/datasrc.service
	rm -f $(DESTDIR)/lib/systemd/system/radangel.service
	rm -f $(DESTDIR)/lib/systemd/system/websockify.service
	rm -f $(DESTDIR)/etc/nginx/conf.d/radangel.conf
	rm -f $(DESTDIR)/etc/udev/rules.d/60-radangel.rules

install: install-bin install-html install-config

uninstall: uninstall-bin uninstall-html uninstall-config

%-clean: %
	$(MAKE) -C $< clean

clean: $(addsuffix -clean,$(SUBDIRS))
	rm -rf $(DESTDIR)
