#!/bin/sh
set -e

#DEBHELPER#

. /usr/share/debconf/confmodule

CONFIG_FILE=/etc/slapos/slapos-node-unofficial.cfg

MASTER_URL=
COMPUTER_ID=
SOFTWARE_ROOT=
PARTITION_AMOUNT=
IPV4_LOCAL_NETWORK=

if [ -f "$CONFIG_FILE" ]; then
    MASTER_URL="`sed -ne 's#^\s*master_url[^=]*=\s*\(.*\)$#\1#p' \"$CONFIG_FILE\"`"
    COMPUTER_ID="`sed -ne 's#^\s*computer_id[^=]*=\s*\(.*\)$#\1#p' \"$CONFIG_FILE\"`"
    SOFTWARE_ROOT="`sed -ne 's#^\s*software_root[^=]*=\s*\(.*\)$#\1#p' \"$CONFIG_FILE\"`"
    PARTITION_AMOUNT="`sed -ne 's#^\s*partition_amount[^=]*=\s*\(.*\)$#\1#p' \"$CONFIG_FILE\"`"
    IPV4_LOCAL_NETWORK="`sed -ne 's#^\s*ipv4_local_network[^=]*=\s*\(.*\)$#\1#p' \"$CONFIG_FILE\"`"
fi

if [ -n "$MASTER_URL" ]; then
    db_set shared/slapos/master_url "$MASTER_URL"
    db_input medium shared/slapos/master_url || true
else
    db_input high shared/slapos/master_url || true
fi

db_go || true
db_get shared/slapos/master_url
( echo "$RET" | grep -q "^https://" ) && \
    db_input high shared/slapos/master_url_with_ssl_note || true

if [ -n "$COMPUTER_ID" ]; then
    db_set slapos-node-unofficial/computer_id "$COMPUTER_ID"
    db_input medium slapos-node-unofficial/computer_id || true
else
    db_input high slapos-node-unofficial/computer_id || true
fi

if [ -n "$SOFTWARE_ROOT" ]; then
    db_set slapos-node-unofficial/software_root "$SOFTWARE_ROOT"
    db_input medium slapos-node-unofficial/software_root || true
else
    db_input high slapos-node-unofficial/software_root || true
fi

if [ -n "$PARTITION_AMOUNT" ]; then
    db_set slapos-node-unofficial/partition_amount "$PARTITION_AMOUNT"
    db_input medium slapos-node-unofficial/partition_amount || true
else
    db_input high slapos-node-unofficial/partition_amount || true
fi

if [ -n "$IPV4_LOCAL_NETWORK" ]; then
    db_set slapos-node-unofficial/ipv4_local_network "$IPV4_LOCAL_NETWORK"
    db_input medium slapos-node-unofficial/ipv4_local_network || true
else
    db_input high slapos-node-unofficial/ipv4_local_network || true
fi

db_go || true

exit 0
