# Licence: GPLv3+ <http://www.gnu.org/licenses/gpl.txt>
# Copyright 2015: Julien Moutinho <julm+air@autogeree.net>

#
## GNUmakefile
###
.SECONDARY:
.SUFFIXES:
	MAKEFLAGS += -r

uniq=$(if $1,$(call \
	uniq,$(wordlist 2,$(words $1),$1),$2 $(if \
		$(filter $(firstword $1),$2),,$(firstword $1))) \
 ,$2)

views := png html5
#all: $(views)

#
## Sensors
###

SENSOR_LABEL_temperature=Température (°C)
SENSOR_LABEL_humidity=Humidité (%)
SENSOR_LABEL_dust=Particules
SENSOR_LABEL_air=Qualité de l’air
SENSOR_LABEL_co_ch4_lpg=CO/CH4/LPG
sensors := \
 temperature \
 humidity \
 air \
 dust \
 co_ch4_lpg

#
## Round Robin Database (RRD)
###

RRD_CF := AVERAGE
.rrd = $(wildcard rrd/*/*.rrd)

#
## INO
###

# For Seeeduino
BOARD_TAG := uno

ARDUINO_LIBS := \
 AirQuality_Sensor \
 DustSensor \
 Humidity_Temperature_Sensor \
 OLED_Display_96X96 \
 RTC_DS1307 \
 Wire
 #Grove_Barometer_HP20x
 #LCD_Display9696 \

#USER_LIB_PATH := $(realpath ./libraries)

ifeq ($(NO_INO),)
include $(ARDMK_DIR)/Arduino.mk
endif

ino: $(TARGET_EEP) $(TARGET_HEX)

#
## PNG
###

define view/sensor/png
endef

define view/sensor/year/png
view/$(sensor)/$(year)/png:
endef

define view/sensor/year/month/png
view/$(sensor)/$(year)/$(month)/png: \
view/$(sensor)/$(year)/$(month).png

view/$(sensor)/$(year)/$(month).png: \
rrd/$(year)/$(month).rrd
	mkdir -p $$(@D)
	rrdtool graph $$@ \
	 -w 785 -h 120 -a PNG \
	 --slope-mode \
	 --start $(month_begin) --end $(month_end) \
	 --vertical-label '$$(SENSOR_LABEL_$(sensor))' \
	 --x-grid HOUR:8:DAY:1:DAY:1:86400:%d \
	 DEF:$(sensor)=rrd/$(year)/$(month).rrd:$(sensor):$$(RRD_CF) \
	 LINE1:$(sensor)'#ff0000':"$(sensor)"
endef

define view/sensor/year/month/day/png
view/$(sensor)/$(year)/$(month)/$(day)/png: \
view/$(sensor)/$(year)/$(month)/$(day).png

view/$(sensor)/$(year)/$(month)/$(day).png: \
rrd/$(year)/$(month).rrd
	mkdir -p $$(@D)
	rrdtool graph $$@ \
	 -w 785 -h 120 -a PNG \
	 --slope-mode \
	 --start $(day_begin) --end $(day_end) \
	 --vertical-label '$$(SENSOR_LABEL_$(sensor))' \
	 --x-grid MINUTE:10:HOUR:1:HOUR:1:0:%H \
	 DEF:$(sensor)=rrd/$(year)/$(month).rrd:$(sensor):$$(RRD_CF) \
	 LINE1:$(sensor)'#ff0000':"$(sensor)";
endef

define view/year/month/png
view/$(year)/$(month)/png: \
$(foreach sensor,$(sensors), view/$(sensor)/$(year)/$(month)/png)
endef
define view/year/month/day/png
view/$(year)/$(month)/$(day)/png: \
$(foreach sensor,$(sensors), view/$(sensor)/$(year)/$(month)/$(day)/png)
endef

#
## HTML5
###

define view/sensor/html5
endef

define view/sensor/year/html5
view/$(sensor)/$(year)/html5:
endef

define view/sensor/year/month/html5
view/$(sensor)/$(year)/$(month)/html5: \
view/$(sensor)/$(year)/$(month).html5

view/$(sensor)/$(year)/$(month).html5: \
view/$(sensor)/$(year)/$(month)/png
	printf ' \
	<report sensor="$(sensor)" year="$(year)" month="$(month)"> \
		$(foreach day,$(days), \
		<date \
		 day="$(day)" \
		 day-name="$$(shell date +'%A' -d '$(year)/$(month)/$(day)')"/>) \
	 </report>' | \
	xsltproc --output $$@ \
	 xsl/sensor/year/month.html5.xsl -
endef
define view/year/month/html5
view/$(year)/$(month)/html5: \
view/$(year)/$(month).html5

view/$(year)/$(month).html5: \
view/$(year)/$(month)/png
	mkdir -p $$(@D)
	printf ' \
	<report year="$(year)" month="$(month)"> \
		$(foreach day,$(days), \
		<date \
		 day="$(day)" \
		 day-name="$$(shell date +'%A' -d '$(year)/$(month)/$(day)')"> \
			$(foreach sensor,$(sensors), \
				<sensor name="$(sensor)"/> \
			 ) \
		</date>) \
	 </report>' | \
	xsltproc --output $$@ \
	 xsl/year/month.html5.xsl -
endef

#
## Dates
###
years=$(call uniq,$(foreach rrd,$(.rrd),$(word 2,$(subst /, ,$(rrd)))))
months=$(foreach rrd,$(filter rrd/$(year)/%,$(.rrd)),$(basename $(word 3,$(subst /, ,$(rrd)))))
days=$(shell seq -w $$(date +'%d' -d '$(year)/$(month)/01 + 1 month - 1 second'))

month_begin=$(shell date +'%s' -d '$(year)/$(month)/01')
month_end=$(shell date +'%s' -d '$(year)/$(month)/01 + 1 month - 1 second')

day_begin=$(shell date +'%s' -d '$(year)/$(month)/$(day)')
day_end=$(shell date +'%s' -d '$(year)/$(month)/$(day) + 1 day - 1 second')

define foreach/sensor
.PHONY:     view/$(sensor)/$(view)
view/$(view): view/$(sensor)/$(view)
$(call view/sensor/$(view))
endef

define foreach/year/sensor/view
.PHONY:               view/$(sensor)/$(year)/$(view)
view/$(sensor)/$(view): view/$(sensor)/$(year)/$(view)
$(call view/sensor/year/$(view))
endef

define foreach/year/month/sensor/view
.PHONY:                         view/$(sensor)/$(year)/$(month)/$(view)
view/$(sensor)/$(year)/$(view): view/$(sensor)/$(year)/$(month)/$(view)
$(call view/sensor/year/month/$(view))
endef
define foreach/year/month/view
.PHONY:             view/$(year)/$(month)/$(view)
view/$(year)/$(view): view/$(year)/$(month)/$(view)
$(call view/year/month/$(view))
endef

define foreach/year/month/day/sensor/view
.PHONY:                                  view/$(sensor)/$(year)/$(month)/$(day)/$(view)
view/$(sensor)/$(year)/$(month)/$(view): view/$(sensor)/$(year)/$(month)/$(day)/$(view)
$(call view/sensor/year/month/day/$(view))
endef
define foreach/year/month/day/view
.PHONY:                        view/$(year)/$(month)/$(day)/$(view)
view/$(year)/$(month)/$(view): view/$(year)/$(month)/$(day)/$(view)
$(call view/year/month/day/$(view))
endef

$(foreach sensor,$(sensors), \
	$(foreach view,$(views), \
		$(eval $(call foreach/sensor)) \
	 ) \
 )
$(foreach year,$(years), \
	$(foreach view,$(views), \
		$(eval $(call foreach/year/view)) \
	 ) \
	$(foreach sensor,$(sensors), \
		$(foreach view,$(views), \
			$(eval $(call foreach/year/sensor/view)) \
		 ) \
	 ) \
	$(foreach month,$(months), \
		$(foreach view,$(views), \
			$(eval $(call foreach/year/month/view)) \
		 ) \
		$(foreach sensor,$(sensors), \
			$(foreach view,$(views), \
				$(eval $(call foreach/year/month/sensor/view)) \
			 ) \
		 ) \
		$(foreach day,$(days), \
			$(foreach view,$(views), \
				$(eval $(call foreach/year/month/day/view)) \
			 ) \
			$(foreach sensor,$(sensors), \
				$(foreach view,$(views), \
					$(eval $(call foreach/year/month/day/sensor/view)) \
				 ) \
			 ) \
		 ) \
	 ) \
 )