#!/bin/sh
#
# Univention Setup
#  install all updates
#
# SPDX-FileCopyrightText: 2014-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

# shellcheck source=../setup_utils.sh
. /usr/lib/univention-system-setup/scripts/setup_utils.sh

info_header "$0" "$(gettext "Upgrading the system")"

# Activate the online repository
/usr/sbin/ucr set repository/online=yes

is_profile_var_true "update/system/after/setup" ||
	exit 0

progress_msg "$(gettext "This might take a while depending on the number of pending updates.")"

eval "$(ucr shell version/version server/role ldap/master hostname)"

case "${server_role:-}" in
domaincontroller_master)
	# Update to latest patchlevel
	version="${version_version:?}-99"
	;;
*)
	[ -e /var/univention-join/joined ] &&
		version=$(echo "@%@version/version@%@-@%@version/patchlevel@%@" | /usr/sbin/univention-ssh --no-split /etc/machine.secret "${hostname:?}\$@${ldap_master:?}" '/usr/sbin/ucr filter' 2>>/var/log/univention/setup.log)
	: "${version:=${version_version:?}-0}"
	;;
esac

echo "Running upgrade: univention-upgrade --noninteractive --updateto $version"
exec /usr/sbin/univention-upgrade --noninteractive --updateto "$version"
