#!/bin/sh
# Fixed local database bootstrap; no supplied SQL, credentials or network address.
set -eu
test "$(id -u)" = 0
if ! getent passwd mission-core-monitor >/dev/null; then
 adduser --system --group --home /var/lib/mission-core-monitor --no-create-home --disabled-login mission-core-monitor
fi
install -d -m 0750 -o mission-core-monitor -g mission-core-node /var/lib/mission-core-monitor
install -d -m 0755 -o postgres -g postgres /run/mission-core-monitor-db
install -d -m 0755 /etc/tmpfiles.d
printf '%s\n' 'd /run/mission-core-monitor-db 0755 postgres postgres -' > /etc/tmpfiles.d/mission-core-monitor.conf
# R18 used a dash, which the PostgreSQL systemd template expands as a slash.
# Rename only our exact cluster. pg_renamecluster preserves this custom data
# directory, updates its configuration paths, and retains all database files.
if [ -d /etc/postgresql/16/ndc-monitor ]; then
 test ! -d /etc/postgresql/16/ndcmonitor
 test "$(pg_conftool -s 16 ndc-monitor show data_directory)" = /var/lib/mission-core-monitor-db
 test "$(pg_conftool -s 16 ndc-monitor show port)" = 5433
 pg_renamecluster 16 ndc-monitor ndcmonitor
fi
if [ ! -d /etc/postgresql/16/ndcmonitor ]; then
 pg_createcluster 16 ndcmonitor --port=5433 --socketdir=/run/mission-core-monitor-db --datadir=/var/lib/mission-core-monitor-db --start-conf=auto -- --auth-local=peer --auth-host=reject
fi
test "$(pg_conftool -s 16 ndcmonitor show data_directory)" = /var/lib/mission-core-monitor-db
test "$(pg_conftool -s 16 ndcmonitor show port)" = 5433
install -d -m 0755 /etc/postgresql/16/ndcmonitor/conf.d
cat > /etc/postgresql/16/ndcmonitor/conf.d/60-mission-core-monitor.conf <<'CONF'
listen_addresses = ''
unix_socket_directories = '/run/mission-core-monitor-db'
shared_preload_libraries = 'timescaledb'
shared_buffers = '32MB'
work_mem = '2MB'
maintenance_work_mem = '16MB'
max_connections = 12
max_worker_processes = 4
max_parallel_workers = 0
timescaledb.max_background_workers = 2
timescaledb.telemetry_level = 'off'
max_wal_size = '128MB'
min_wal_size = '32MB'
temp_file_limit = '32MB'
statement_timeout = '5s'
log_statement = 'none'
log_min_error_statement = 'panic'
CONF
install -d -m 0755 /etc/systemd/system/postgresql@16-ndcmonitor.service.d
cat > /etc/systemd/system/postgresql@16-ndcmonitor.service.d/60-mission-core-monitor.conf <<'CONF'
[Service]
ExecStartPre=+/usr/bin/install -d -m 0755 -o postgres -g postgres /run/mission-core-monitor-db
MemoryHigh=256M
MemoryMax=384M
CPUQuota=25%
TasksMax=32
Nice=10
CONF
systemctl daemon-reload
systemctl restart postgresql@16-ndcmonitor.service
runuser -u postgres -- psql -X -P pager=off -v ON_ERROR_STOP=1 -h /run/mission-core-monitor-db -p 5433 -d postgres <<'SQL'
SELECT 'CREATE ROLE "mission-core-monitor" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE' WHERE NOT EXISTS(SELECT FROM pg_roles WHERE rolname='mission-core-monitor') \gexec
SELECT 'CREATE DATABASE mission_core_monitor OWNER "mission-core-monitor"' WHERE NOT EXISTS(SELECT FROM pg_database WHERE datname='mission_core_monitor') \gexec
SQL
runuser -u postgres -- psql -X -P pager=off -v ON_ERROR_STOP=1 -h /run/mission-core-monitor-db -p 5433 -d mission_core_monitor -c 'CREATE EXTENSION IF NOT EXISTS timescaledb'
runuser -u mission-core-monitor -- psql -X -P pager=off -v ON_ERROR_STOP=1 -h /run/mission-core-monitor-db -p 5433 -d mission_core_monitor -f /usr/lib/mission-core-node/monitor/schema.sql
systemctl enable mission-core-node-monitor.service
systemctl restart mission-core-node-monitor.service
