# Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend
# 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-backend-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: main

.PHONY: core
core: node_modules ${dir_temp}/espe-core.js ${dir_temp}/espe-core.d.ts

.PHONY: node_modules
node_modules:
	@ ${cmd_mkdir} ${dir_build}/node_modules
	@ ${cmd_log} "node modules …"
	@ ${cmd_cp} -r -u ${dir_lib}/node/node_modules/* ${dir_build}/node_modules/

${dir_temp}/espe-core.js ${dir_temp}/espe-core.d.ts: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_source}/helpers.ts \
		${dir_source}/helpers/password.ts \
		${dir_source}/database.ts \
		${dir_source}/types.ts \
		${dir_source}/repositories/name_index.ts \
		${dir_source}/repositories/member.ts \
		${dir_source}/services/name_index.ts \
		${dir_source}/services/member.ts \
		${dir_source}/service-admin.ts \
		${dir_source}/api/base.ts \
		${dir_source}/api/actions/meta_ping.ts \
		${dir_source}/api/actions/meta_spec.ts \
		${dir_source}/api/actions/session_begin.ts \
		${dir_source}/api/actions/session_end.ts \
		${dir_source}/api/actions/member_project.ts \
		${dir_source}/api/actions/member_summon.ts \
		${dir_source}/api/actions/member_info.ts \
		${dir_source}/api/actions/member_register.ts \
		${dir_source}/api/actions/member_list.ts \
		${dir_source}/api/actions/member_read.ts \
		${dir_source}/api/actions/member_modify.ts \
		${dir_source}/api/actions/member_delete.ts \
		${dir_source}/api/actions/member_password_change_initialize.ts \
		${dir_source}/api/actions/member_password_change_execute.ts \
		${dir_source}/api/functions.ts \
		${dir_source}/conf.ts
	@ ${cmd_log} "compile | core …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020 --strict $^ --outFile ${dir_temp}/espe-core.js --declaration

.PHONY: main
main: core ${dir_build}/espe data

${dir_temp}/espe-main-raw.js: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_temp}/espe-core.d.ts \
		${dir_source}/main.ts
	@ ${cmd_log} "compile | main …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020 $^ --outFile $@

${dir_build}/espe: \
		${dir_source}/head.js \
		${dir_lib}/plankton/plankton.js \
		${dir_temp}/espe-core.js \
		${dir_temp}/espe-main-raw.js
	@ ${cmd_log} "link | main …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_cat} $^ > $@
	@ ${cmd_chmod} +x $@

.PHONY: test
test: core ${dir_build}/test-data.json ${dir_build}/espe-test.mocha.js

${dir_build}/test-data.json: \
		${dir_source}/test/data.json
	@ ${cmd_log} "test | copy data …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_cp} -r -u -v $^ $@

${dir_temp}/espe-test-raw.js: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_temp}/espe-core.d.ts \
		${dir_lib}/node/node_modules/@types/mocha/index.d.ts \
		${dir_source}/test/main.mocha.ts
	@ ${cmd_log} "compile | test …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020 $^ --outFile $@

${dir_build}/espe-test.mocha.js: \
		${dir_source}/head.js \
		${dir_lib}/plankton/plankton.js \
		${dir_temp}/espe-core.js \
		${dir_temp}/espe-test-raw.js
	@ ${cmd_log} "link | test …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_cat} $^ > $@
	@ ${cmd_chmod} +x $@

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