#!/bin/sh

set -e

# Django requires non-IP/localhost URLs to contain at least one dot, so
# adjust the testbed's FQDN; also ensure that a deb.* subdomain can be
# resolved.
canonical_hostname="$(hostname -f)"
if ! echo "$canonical_hostname" | grep -Fq .; then
	canonical_hostname="$canonical_hostname.localdomain"
fi
(grep -v '^127\.0\.1\.1[[:space:]]' /etc/hosts; \
 echo "127.0.1.1 $canonical_hostname $(hostname) deb.$canonical_hostname") >/etc/hosts.new
mv /etc/hosts.new /etc/hosts

# Regenerate the self-signed certificate.
server_cfg=/etc/debusine/server
python3 -m trustme -d "$server_cfg" \
	-i "$canonical_hostname" "deb.$canonical_hostname"
chown debusine-server: "$server_cfg"/*.key "$server_cfg"/*.pem
chmod 600 "$server_cfg"/*.key

# Setup debusine-server with similar steps as an admin would do:
# Creates the database, migrate, set up nginx, allowed-hosts
#
# It assumes that postgres is already running

# Setup postgresql database and user
sudo -u postgres createuser debusine-server || true
sudo -u postgres createdb --owner debusine-server debusine || true

# Set up nginx
sed -e "s/server_name .*;/server_name $canonical_hostname;/" \
    -e "s#ssl_certificate .*;#ssl_certificate $server_cfg/server.pem;#" \
    -e "s#ssl_certificate_key .*;#ssl_certificate_key $server_cfg/server.key;#" \
	examples/nginx/nginx-vhost.conf \
	>/etc/nginx/sites-available/debusine.example.net
sed -e "s/server_name .*;/server_name deb.$canonical_hostname;/" \
    -e "s#ssl_certificate .*;#ssl_certificate $server_cfg/server.pem;#" \
    -e "s#ssl_certificate_key .*;#ssl_certificate_key $server_cfg/server.key;#" \
	examples/nginx/nginx-vhost-deb.conf \
	>/etc/nginx/sites-available/deb.debusine.example.net

ln -sf /etc/nginx/sites-available/debusine.example.net \
	/etc/nginx/sites-available/deb.debusine.example.net \
	/etc/nginx/sites-enabled

rm -f /etc/nginx/sites-enabled/default

# Trust self-signed certificate
cp /etc/debusine/server/client.pem \
	"/usr/local/share/ca-certificates/$canonical_hostname.crt"
update-ca-certificates

# Limit the memory usage on multi-core machines
echo "workers = 2" >> /etc/debusine/server/gunicorn.conf.py

mv /etc/debusine/server/local.py.sample /etc/debusine/server/local.py
cat >> /etc/debusine/server/local.py <<EOF

# Configure the test architecture as default.
DEBUSINE_FALLBACK_ARCHITECTURE = '$AUTOPKGTEST_TESTBED_ARCH'

# Log everything to the systemd journal, to make it easier to gather output.
# TODO: This can be dropped when
# https://salsa.debian.org/freexian-team/debusine/-/issues/597 is fully
# implemented.
from .defaults import LOGGING
LOGGING = LOGGING.copy()
LOGGING["formatters"]["messageonly"] = {"format": "%(message)s"}
LOGGING["handlers"]["journal"] = {
    "level": "DEBUG",
    "class": "systemd.journal.JournalHandler",
    "formatter": "messageonly",
}
LOGGING["loggers"] = {
    "root": {
        "handlers": ["journal"],
        "level": "DEBUG",
        "propagate": False,
    },
}
EOF

systemctl reset-failed debusine-server-migrate
systemctl restart debusine-server-migrate
systemctl restart debusine-server
systemctl restart debusine-server-celery
systemctl restart debusine-server-periodic-tasks
systemctl restart debusine-server-provisioner
systemctl restart debusine-server-scheduler
systemctl restart nginx

# Create a debian:environments collection for use by tests
sudo -u debusine-server debusine-admin create_collection \
	debian debian:environments </dev/null
