#!/bin/bash
#
# Univention System Setup Boot
#
# SPDX-FileCopyrightText: 2016-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

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

SITE_EC2='univention/initialsetup/'  # EC2
SITE_USS='univention/setup/?username=root'

action_help () {
	echo
	echo "When started, all UCR variables and configurations will be set to prepare the"
	echo "VM for appliance mode: At next boot the configuration wizard will be started."
	echo
	echo "usage: ${0##*/} [start|stop]"
	echo
	exit 0
}

action_start () {
	univention-config-registry set --schedule \
		apache2/startsite="$(is_ucr_true server/amazon && echo "$SITE_EC2" || echo "$SITE_USS")" \
		system/setup/boot/start='true' \
		system/setup/boot/select/role='true' \
		nssldap/timelimit='10'

	univention-config-registry commit /var/www/univention/meta.json >/dev/null 2>&1
	systemctl reload apache2.service
}

action_stop () {
	univention-config-registry set system/setup/boot/start=false
	univention-config-registry unset --schedule \
		apache2/startsite \
		system/setup/boot/start \
		system/setup/boot/select/role \
		nssldap/timelimit

	systemctl reload apache2.service
}

case "${1:-}" in
""|"--help"|"-h") action_help ;;
start) action_start ;;
stop) action_stop ;;
*)
	echo
	echo "ERROR: Unknown action '$1' given"
	echo
	exit 1
esac
