## consts

dir_lib := lib
dir_source := source
dir_temp := /tmp/zeitbild-frontend-web-temp
dir_build := build
dir_tools := tools

cmd_cat := cat
cmd_chmod := chmod
cmd_cp := cp
cmd_log := echo "--"
cmd_mkdir := mkdir -p
cmd_tsc := ${dir_tools}/typescript/node_modules/.bin/tsc


## rules

.PHONY: default
default: index templates style logic data

.PHONY: index
index: ${dir_build}/index.html

${dir_build}/index.html: \
		${dir_source}/index.html.tpl
	@ ${cmd_log} "index …"
	@ ${cmd_mkdir} $(dir $@)
	@ tools/make-index \
		${dir_source}/index.html.tpl \
		> $@

.PHONY: templates
templates: \
		templates-widgets-sources \
		templates-widgets-listview \
		templates-widgets-weekview \
		templates-pages-calendar_add \
		templates-pages-calendar_edit \
		templates-pages-event_add \
		templates-pages-event_edit \
		templates-pages-overview \
		templates-pages-login

.PHONY: templates-widgets-sources
templates-widgets-sources: \
		$(wildcard ${dir_source}/widgets/sources/templates/*)
	@ ${cmd_log} "templates:widgets:sources …"
	@ ${cmd_mkdir} ${dir_build}/templates/widget-sources
	@ ${cmd_cp} -r -u -v ${dir_source}/widgets/sources/templates/* ${dir_build}/templates/widget-sources/

.PHONY: templates-widgets-listview
templates-widgets-listview: \
		$(wildcard ${dir_source}/widgets/listview/templates/*)
	@ ${cmd_log} "templates:widgets:listview …"
	@ ${cmd_mkdir} ${dir_build}/templates/widget-listview
	@ ${cmd_cp} -r -u -v ${dir_source}/widgets/listview/templates/* ${dir_build}/templates/widget-listview/

.PHONY: templates-widgets-weekview
templates-widgets-weekview: \
		$(wildcard ${dir_source}/widgets/weekview/templates/*)
	@ ${cmd_log} "templates:widgets:weekview …"
	@ ${cmd_mkdir} ${dir_build}/templates/widget-weekview
	@ ${cmd_cp} -r -u -v ${dir_source}/widgets/weekview/templates/* ${dir_build}/templates/widget-weekview/

.PHONY: templates-pages-calendar_add
templates-pages-calendar_add: \
		$(wildcard ${dir_source}/pages/calendar_add/templates/*)
	@ ${cmd_log} "templates:calendar_add …"
	@ ${cmd_mkdir} ${dir_build}/templates/calendar_add
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/calendar_add/templates/* ${dir_build}/templates/calendar_add/

.PHONY: templates-pages-calendar_edit
templates-pages-calendar_edit: \
		$(wildcard ${dir_source}/pages/calendar_edit/templates/*)
	@ ${cmd_log} "templates:calendar_edit …"
	@ ${cmd_mkdir} ${dir_build}/templates/calendar_edit
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/calendar_edit/templates/* ${dir_build}/templates/calendar_edit/

.PHONY: templates-pages-event_add
templates-pages-event_add: \
		$(wildcard ${dir_source}/pages/event_add/templates/*)
	@ ${cmd_log} "templates:event_add …"
	@ ${cmd_mkdir} ${dir_build}/templates/event_add
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/event_add/templates/* ${dir_build}/templates/event_add/

.PHONY: templates-pages-event_edit
templates-pages-event_edit: \
		$(wildcard ${dir_source}/pages/event_edit/templates/*)
	@ ${cmd_log} "templates:event_edit …"
	@ ${cmd_mkdir} ${dir_build}/templates/event_edit
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/event_edit/templates/* ${dir_build}/templates/event_edit/

.PHONY: templates-pages-overview
templates-pages-overview: \
		$(wildcard ${dir_source}/pages/overview/templates/*)
	@ ${cmd_log} "templates:overview …"
	@ ${cmd_mkdir} ${dir_build}/templates/overview
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/overview/templates/* ${dir_build}/templates/overview/

.PHONY: templates-pages-login
templates-pages-login: \
		$(wildcard ${dir_source}/pages/login/templates/*)
	@ ${cmd_log} "templates:login …"
	@ ${cmd_mkdir} ${dir_build}/templates/login
	@ ${cmd_cp} -r -u -v ${dir_source}/pages/login/templates/* ${dir_build}/templates/login/

.PHONY: style
style: \
		$(wildcard ${dir_source}/style/*)
	@ ${cmd_log} "style …"
	@ ${cmd_mkdir} ${dir_build}
	@ ${cmd_cat} ${dir_source}/style/* > ${dir_build}/style.css

.PHONY: logic
logic: ${dir_build}/logic.js

${dir_temp}/logic-unlinked.js: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_source}/logic/helpers.ts \
		${dir_source}/logic/widget.ts \
		${dir_source}/logic/conf.ts \
		${dir_source}/logic/types.ts \
		${dir_source}/logic/backend.ts \
		${dir_source}/widgets/sources/logic.ts \
		${dir_source}/widgets/listview/logic.ts \
		${dir_source}/widgets/weekview/logic.ts \
		${dir_source}/pages/login/logic.ts \
		${dir_source}/pages/logout/logic.ts \
		${dir_source}/pages/oidc_finish/logic.ts \
		${dir_source}/pages/calendar_add/logic.ts \
		${dir_source}/pages/calendar_edit/logic.ts \
		${dir_source}/pages/event_add/logic.ts \
		${dir_source}/pages/event_edit/logic.ts \
		${dir_source}/pages/overview/logic.ts \
		${dir_source}/logic/main.ts
	@ ${cmd_log} "logic | compile …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib dom,es2020 $^ --outFile $@ # --strict

${dir_build}/logic.js: \
		${dir_lib}/plankton/plankton.js \
		${dir_temp}/logic-unlinked.js
	@ ${cmd_log} "logic | link …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_cat} $^ > $@

.PHONY: data
data:
	@ ${cmd_log} "data …"
	@ ${cmd_mkdir} ${dir_build}/data
	@ ${cmd_cp} -r -u -v ${dir_source}/data/* ${dir_build}/data/
