# 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/mimir-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 := tsc


## rules

.PHONY: default
default: main

.PHONY: main
main: ${dir_build}/mimir

${dir_temp}/head.js:
	@ ${cmd_log} "head …"
	@ ${cmd_mkdir} $(dir $@)
	@ echo "#!/usr/bin/env node\n" > $@

${dir_temp}/mimir-raw.js: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_source}/helpers/borg.ts \
		${dir_source}/conf.ts \
		${dir_source}/logic/serialization/_abstract.ts \
		${dir_source}/logic/serialization/files.ts \
		${dir_source}/logic/serialization/postgresql_dump.ts \
		${dir_source}/logic/serialization/_factory.ts \
		${dir_source}/logic/transfer/_abstract.ts \
		${dir_source}/logic/transfer/local.ts \
		${dir_source}/logic/transfer/borg.ts \
		${dir_source}/logic/transfer/_factory.ts \
		${dir_source}/main.ts
	@ ${cmd_log} "compile …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020 --strict $^ --outFile $@

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