#!/bin/sh
set -e

# Source debconf library.
. /usr/share/debconf/confmodule

# Fetching configuration from debconf
db_get kontena-agent/server_uri
URI="KONTENA_URI=${RET}"
sed -i -r "s#\#?KONTENA_URI=.*#${URI}#" /etc/kontena-agent.env

db_get kontena-agent/grid_token
TOKEN="KONTENA_TOKEN=${RET}"
sed -i -r "s#\#?KONTENA_TOKEN=.*#${TOKEN}#" /etc/kontena-agent.env

# reduce permissions on config File
chmod 0600 /etc/kontena-agent.env

# reload and restart docker
systemctl daemon-reload
systemctl restart docker

systemctl daemon-reload
systemctl restart kontena-agent
systemctl is-enabled kontena-agent || systemctl enable kontena-agent
