#!/bin/sh
set -e
. /usr/share/debconf/confmodule

# The answer to a question in ANSWER, or "" when the question has not been seen; the
# default debconf holds for an unseen question is not an answer.
answer() {
    ANSWER=""
    db_fget "$1" seen
    if [ "$RET" = "true" ]; then
        db_get "$1"
        ANSWER="$RET"
    fi
}

case "$1" in
    configure)
        # --- Read the installer's answers ---
        answer linux-manager/platform;         LINUX_MANAGER_PLATFORM="$ANSWER"
        answer linux-manager/mode;             LINUX_MANAGER_MODE="$ANSWER"
        answer linux-manager/router_enabled;   LINUX_MANAGER_ROUTER_ENABLED="$ANSWER"
        answer linux-manager/vpn_enabled;      LINUX_MANAGER_VPN_ENABLED="$ANSWER"
        answer linux-manager/int_addr;         LINUX_MANAGER_INT_ADDR="$ANSWER"
        answer linux-manager/external_interface; LINUX_MANAGER_EXTERNAL_INTERFACE="$ANSWER"
        answer linux-manager/internal_interface; LINUX_MANAGER_INTERNAL_INTERFACE="$ANSWER"
        answer linux-manager/local_fqdn;       LINUX_MANAGER_LOCAL_FQDN="$ANSWER"
        answer linux-manager/local_id_mode;    LINUX_MANAGER_LOCAL_ID_MODE="$ANSWER"
        answer linux-manager/local_cidrs;      LINUX_MANAGER_LOCAL_CIDRS="$ANSWER"
        answer linux-manager/int_gateway_ip;   LINUX_MANAGER_INT_GATEWAY_IP="$ANSWER"
        answer linux-manager/remote_addrs;     LINUX_MANAGER_REMOTE_ADDRS="$ANSWER"
        answer linux-manager/remote_id;        LINUX_MANAGER_REMOTE_ID="$ANSWER"
        answer linux-manager/remote_cidrs;     LINUX_MANAGER_REMOTE_CIDRS="$ANSWER"
        answer linux-manager/psk;              LINUX_MANAGER_PSK="$ANSWER"
        answer linux-manager/p2s_enabled;      LINUX_MANAGER_P2S_ENABLED="$ANSWER"
        answer linux-manager/p2s_address_pool; LINUX_MANAGER_P2S_ADDRESS_POOL="$ANSWER"
        answer linux-manager/pki_root_name;    LINUX_MANAGER_PKI_ROOT_NAME="$ANSWER"
        answer linux-manager/wg_enabled;       LINUX_MANAGER_WG_ENABLED="$ANSWER"
        answer linux-manager/wg_address;       LINUX_MANAGER_WG_ADDRESS="$ANSWER"
        answer linux-manager/wg_listen_port;   LINUX_MANAGER_WG_LISTEN_PORT="$ANSWER"

        export LINUX_MANAGER_PLATFORM LINUX_MANAGER_MODE LINUX_MANAGER_INT_ADDR \
               LINUX_MANAGER_ROUTER_ENABLED LINUX_MANAGER_VPN_ENABLED \
               LINUX_MANAGER_EXTERNAL_INTERFACE \
               LINUX_MANAGER_INTERNAL_INTERFACE LINUX_MANAGER_LOCAL_FQDN \
               LINUX_MANAGER_LOCAL_ID_MODE LINUX_MANAGER_LOCAL_CIDRS \
               LINUX_MANAGER_INT_GATEWAY_IP \
               LINUX_MANAGER_REMOTE_ADDRS LINUX_MANAGER_REMOTE_ID \
               LINUX_MANAGER_REMOTE_CIDRS LINUX_MANAGER_PSK \
               LINUX_MANAGER_P2S_ENABLED LINUX_MANAGER_P2S_ADDRESS_POOL \
               LINUX_MANAGER_PKI_ROOT_NAME LINUX_MANAGER_WG_ENABLED \
               LINUX_MANAGER_WG_ADDRESS LINUX_MANAGER_WG_LISTEN_PORT

        # --- Hand a candidate configuration to ucf ---
        # ucf compares it against the file in /etc and decides what to do about
        # local changes, prompting through debconf only for a real conflict.
        # This is what makes dpkg-reconfigure apply without destroying edits.
        CANDIDATE="$(mktemp)"
        /usr/lib/linux-manager/generate-config "$CANDIDATE"
        ucf --three-way --debconf-ok "$CANDIDATE" /etc/linux-manager/linux-manager.json
        ucfr linux-manager /etc/linux-manager/linux-manager.json
        chmod 0600 /etc/linux-manager/linux-manager.json
        rm -f "$CANDIDATE"

        # The key now lives in the configuration file; do not keep a copy. An
        # empty answer means "leave alone" next time, so clearing it here does
        # not blank the key on the next dpkg-reconfigure.
        db_set linux-manager/psk ""

        # The Manager's administrator account for linux-manager-setup to create, when a name and a
        # password were given; the answers are cleared so that a later run does not ask for it again.
        db_get linux-manager/admin_username
        ADMIN_USERNAME="$RET"
        db_get linux-manager/admin_password
        ADMIN_PASSWORD="$RET"
        if [ -n "$ADMIN_USERNAME" ] && [ -n "$ADMIN_PASSWORD" ]; then
            (umask 077; ADMIN_USERNAME="$ADMIN_USERNAME" ADMIN_PASSWORD="$ADMIN_PASSWORD" python3 -c \
                'import json, os, sys; json.dump({"username": os.environ["ADMIN_USERNAME"], "password": os.environ["ADMIN_PASSWORD"]}, sys.stdout)' \
                > /etc/linux-manager/manager-admin.json)
        elif [ -n "$ADMIN_USERNAME" ]; then
            echo 'linux-manager: no administrator password was given; no Ubuntu Linux Manager administrator is created' >&2
        fi
        db_set linux-manager/admin_username ""
        db_set linux-manager/admin_password ""

        # The firewall is not touched here. Enabling it, allowing SSH and
        # setting the forward policy all belong to linux-manager-setup, which does
        # them only once the configuration says this host is a router.

        # The setup, web and manager services wait for libpq in the linker cache,
        # which dpkg refreshes after this script, so they are started without
        # waiting for them.
        if [ -d /run/systemd/system ]; then
            systemctl --no-block restart linux-manager-setup.service >/dev/null 2>&1 || true
            systemctl --no-block restart linux-manager-web.service >/dev/null 2>&1 || true
            systemctl --no-block restart linux-manager-ui.service >/dev/null 2>&1 || true
        fi
        ;;
esac

#DEBHELPER#

db_stop
