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

# An upgrade (configure with the old version) asks nothing; linux-manager.json holds the
# configuration, and dpkg-reconfigure asks the questions again.
if [ "$1" = "configure" ] && [ -n "$2" ]; then
    exit 0
fi

# Every question has a default, so a non-interactive install with nothing
# preseeded completes without prompting. The defaults are platform "none" and
# mode "manual", which together mean: install the files, configure nothing.

# The Manager's first administrator, asked whatever the platform is; the password only with a name.
db_input medium linux-manager/admin_username || true
db_go || true

db_get linux-manager/admin_username
if [ -n "$RET" ]; then
    db_input medium linux-manager/admin_password || true
    db_go || true
fi

db_input high linux-manager/platform || true
db_go || true

db_get linux-manager/platform
if [ "$RET" = "none" ]; then
    # Configuration is deferred. Asking anything else would collect answers
    # that nothing is going to apply.
    exit 0
fi

db_input high linux-manager/mode || true
db_input high linux-manager/router_enabled || true
db_input high linux-manager/vpn_enabled || true
db_go || true

db_get linux-manager/router_enabled
ROUTER="$RET"
db_get linux-manager/vpn_enabled
VPN="$RET"

# The mode says how much the operator supplies, so it decides which interface
# and address questions are worth asking; without the router the server has
# only the external interface.
db_get linux-manager/mode
MODE="$RET"
case "$MODE" in
    manual|interfaces)
        db_input high linux-manager/external_interface || true
        ;;
esac
if [ "$ROUTER" = "true" ]; then
    case "$MODE" in
        manual)
            db_input high linux-manager/internal_interface || true
            db_input high linux-manager/int_addr || true
            db_input high linux-manager/int_gateway_ip || true
            ;;
        interfaces)
            db_input high linux-manager/internal_interface || true
            ;;
    esac
fi
db_go || true

db_input high linux-manager/local_fqdn || true
if [ "$ROUTER" = "true" ] || [ "$VPN" = "true" ]; then
    db_input high linux-manager/local_cidrs || true
fi
if [ "$ROUTER" = "true" ]; then
    db_input high linux-manager/local_id_mode || true
    db_input high linux-manager/remote_addrs || true
    db_input high linux-manager/remote_id || true
    db_input high linux-manager/remote_cidrs || true
    db_input high linux-manager/psk || true
fi
db_input high linux-manager/pki_root_name || true
if [ "$VPN" = "true" ]; then
    db_input high linux-manager/p2s_enabled || true
    db_input high linux-manager/wg_enabled || true
fi
db_go || true

if [ "$VPN" = "true" ]; then
    db_get linux-manager/p2s_enabled
    if [ "$RET" = "true" ]; then
        db_input high linux-manager/p2s_address_pool || true
        db_go || true
    fi

    db_get linux-manager/wg_enabled
    if [ "$RET" = "true" ]; then
        db_input high linux-manager/wg_address || true
        db_input high linux-manager/wg_listen_port || true
        db_go || true
    fi
fi
