# Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
# Copyright (C) 2024 Christian Fraß
# 
# 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 3 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 
# <https://www.gnu.org/licenses/>. 


## consts

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

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


## rules

.PHONY: _default
_default: ${dir_build}/logic.js ${dir_build}/style.css ${dir_build}/index.html data

${dir_temp}/logic-unlinked.js: \
	${dir_lib}/plankton/plankton.d.ts \
	${dir_source}/logic/helpers.ts \
	${dir_source}/logic/backend.ts \
	${dir_source}/logic/conf.ts \
	${dir_source}/pages/index/logic.ts \
	${dir_source}/pages/login/logic.ts \
	${dir_source}/pages/logout/logic.ts \
	${dir_source}/pages/create/logic.ts \
	${dir_source}/pages/list/logic.ts \
	${dir_source}/pages/view/logic.ts \
	${dir_source}/pages/register/logic.ts \
	${dir_source}/pages/password_change_init/logic.ts \
	${dir_source}/pages/password_change_exec/logic.ts \
	${dir_source}/logic/main.ts
	@ ${cmd_log} "logic | compile …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020,dom $^ --outFile $@

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

${dir_build}/style.css: \
	${dir_source}/style/style.css \
	${dir_source}/pages/index/style.css \
	${dir_source}/pages/login/style.css \
	${dir_source}/pages/logout/style.css \
	${dir_source}/pages/create/style.css \
	${dir_source}/pages/list/style.css \
	${dir_source}/pages/view/style.css \
	${dir_source}/pages/register/style.css \
	${dir_source}/pages/password_change_init/style.css \
	${dir_source}/pages/password_change_exec/style.css
	@ ${cmd_log} "style …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_cat} $^ > $@

${dir_build}/index.html: \
	${dir_source}/structure/index.html.tpl \
	${dir_source}/pages/index/structure.html \
	${dir_source}/pages/login/structure.html \
	${dir_source}/pages/logout/structure.html \
	${dir_source}/pages/create/structure.html \
	${dir_source}/pages/list/structure.html \
	${dir_source}/pages/view/structure.html \
	${dir_source}/pages/register/structure.html \
	${dir_source}/pages/password_change_init/structure.html \
	${dir_source}/pages/password_change_exec/structure.html
	@ ${cmd_log} "structure …"
	@ ${cmd_mkdir} $(dir $@)
	@ tools/make-index $^ > $@

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