SHELL := /bin/bash

APP_PORT ?= 18080
DATA_FILES := $(wildcard ../data/xsk_education.db) $(wildcard ../data/sqlite_migration_report_*.json) $(wildcard ../archives/text-source-before-sqlite-*.tar.gz.sha256)

.PHONY: check smoke data-hash migrate-sqlite-dry-run migrate-sqlite build up ps health deploy logs install-gitea-backup

check:
	node --check app/static/app.js
	node --check app/static/admin.js
	python3 -m compileall -q app

smoke:
	node scripts/smoke_test.js
	python3 scripts/db_smoke_test.py

data-hash:
	sha256sum $(DATA_FILES)

migrate-sqlite-dry-run:
	python3 scripts/migrate_text_to_sqlite.py --data-root ../data --db-path ../data/xsk_education.db --archives-root ../archives --dry-run

migrate-sqlite:
	python3 scripts/migrate_text_to_sqlite.py --data-root ../data --db-path ../data/xsk_education.db --archives-root ../archives

build:
	docker compose build

up:
	docker compose up -d --remove-orphans

ps:
	docker compose ps

health:
	set -a; . ./.env; curl --fail --silent --show-error --retry 10 --retry-delay 1 --retry-connrefused --retry-all-errors -u "records:$${BASIC_AUTH_PASSWORD}" "http://127.0.0.1:$${APP_PORT:-$(APP_PORT)}/api/health"; echo

deploy: check smoke
	$(MAKE) --no-print-directory data-hash
	$(MAKE) --no-print-directory build
	$(MAKE) --no-print-directory up
	sleep 2
	$(MAKE) --no-print-directory health
	$(MAKE) --no-print-directory data-hash
	$(MAKE) --no-print-directory ps

logs:
	docker compose logs --tail=120 xsk-education-management

install-gitea-backup:
	python3 scripts/install_gitea_backup_hook.py
