## consts

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

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


## rules

.PHONY: default
default: sql ${dir_build}/zeitbild node_modules

.PHONY: sql
sql: \
		$(wildcard ${dir_source}/repositories/sql/*)
	@ ${cmd_log} "sql …"
	@ ${cmd_mkdir} ${dir_build}/sql
	@ ${cmd_cp} -r -u $^ ${dir_build}/sql/

${dir_temp}/conf.ts: \
		${dir_source}/conf.ts.tpl \
		${dir_source}/conf.schema.json
	@ ${cmd_mkdir} $(dir $@)
	${dir_tools}/coin $@
	
${dir_temp}/zeitbild-unlinked.js: \
		${dir_lib}/plankton/plankton.d.ts \
		${dir_source}/helpers.ts \
		${dir_source}/base.ts \
		${dir_source}/conf.ts \
		${dir_source}/database.ts \
		${dir_source}/auth.ts \
		${dir_source}/types.ts \
		${dir_source}/value_objects/access_level.ts \
		${dir_source}/repositories/auth_internal.ts \
		${dir_source}/repositories/user.ts \
		${dir_source}/repositories/resource.ts \
		${dir_source}/repositories/calendar.ts \
		${dir_source}/services/auth_internal.ts \
		${dir_source}/services/user.ts \
		${dir_source}/services/resource.ts \
		${dir_source}/services/calendar.ts \
		${dir_source}/api/base.ts \
		${dir_source}/api/transformations/datetime.ts \
		${dir_source}/api/actions/meta_ping.ts \
		${dir_source}/api/actions/meta_spec.ts \
		${dir_source}/api/actions/session_prepare.ts \
		${dir_source}/api/actions/session_begin.ts \
		${dir_source}/api/actions/session_oidc.ts \
		${dir_source}/api/actions/session_end.ts \
		${dir_source}/api/actions/users.ts \
		${dir_source}/api/actions/calendar_list.ts \
		${dir_source}/api/actions/calendar_get.ts \
		${dir_source}/api/actions/calendar_add.ts \
		${dir_source}/api/actions/calendar_change.ts \
		${dir_source}/api/actions/calendar_remove.ts \
		${dir_source}/api/actions/calendar_event_get.ts \
		${dir_source}/api/actions/calendar_event_add.ts \
		${dir_source}/api/actions/calendar_event_change.ts \
		${dir_source}/api/actions/calendar_event_remove.ts \
		${dir_source}/api/actions/events.ts \
		${dir_source}/api/functions.ts \
		${dir_source}/main.ts
	@ ${cmd_log} "compile …"
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_tsc} --lib es2020 --strict $^ --outFile $@

${dir_temp}/head.js:
	@ ${cmd_mkdir} $(dir $@)
	@ ${cmd_echo} "#!/usr/bin/env node" > $@

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

.PHONY: node_modules
node_modules:
	@ cd ${dir_build} && npm install sqlite3 bcrypt
