#!/bin/bash
# SPDX-FileCopyrightText: 2004-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

# shellcheck source=/dev/null
. /usr/share/univention-lib/ucr.sh

# deconfigure temporary interfaces
ucr search --brief --value '^appliance-mode-temporary$' |
	cut -f1 -d: |
	grep -E '^interfaces/[^/]+_[^/]/type$' |
	while read -r
	do
		echo "${REPLY%type}"{netmask,address,broadcast,network,type}
	done |
	xargs -r ucr unset

# Show correct EC2 server address in motd and meta.json
ucr commit /etc/motd.setup /var/www/univention/meta.json >/dev/null 2>&1

# Add public ssh key if the system was started in EC2:
if is_ucr_true server/amazon
then
	log_action_cont_msg "Getting ssh keys from EC2"
	[ -d /root/.ssh ] ||
		mkdir -p /root/.ssh
	[ -e /root/.ssh/authorized_keys ] ||
		touch /root/.ssh/authorized_keys

	key="$(wget -q -O - http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key)"
	grep -q "$key" /root/.ssh/authorized_keys ||
		echo "$key" >>/root/.ssh/authorized_keys
fi

is_ucr_true system/setup/boot/start &&
	exec /usr/share/univention-system-setup/startxwithfirefox > "/var/log/univention/system-setup-boot.log" 2>&1
:
