#!/bin/bash

set -xeo pipefail

python3 tools/prepare_xep_list.py
python3 tools/prepare_software_list.py
python3 tools/prepare_compliance.py

lua normalize_json.lua

sqlite-utils insert xmpp.db xeps db_xeps.json --pk=id
sqlite-utils insert xmpp.db software db_software.json --pk=id
sqlite-utils insert xmpp.db software_platforms db_software_platforms.json --pk=software_id --pk=platform
sqlite-utils insert xmpp.db software_categories db_software_categories.json --pk=software_id --pk=category
sqlite-utils insert xmpp.db implementations db_implementations.json --pk=xep_id --pk=software_id
sqlite-utils insert xmpp.db compliance_levels db_compliance.json
sqlite-utils add-foreign-key xmpp.db implementations xep_id xeps id
sqlite-utils add-foreign-key xmpp.db implementations software_id software id
sqlite-utils add-foreign-key xmpp.db compliance_levels xep_id xeps id
sqlite-utils add-foreign-key xmpp.db software_platforms software_id software id
sqlite-utils add-foreign-key xmpp.db software_categories software_id software id

# Precompute row counts for performance
datasette inspect xmpp.db --inspect-file=counts.json

exec datasette serve -h 0.0.0.0 --metadata metadata.yml -i xmpp.db --inspect-file=counts.json
