#!/bin/sh
#
# Univention System Setup
#  helper script: getting settings from dhclient
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2009-2024 Univention GmbH
#
# https://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <https://www.gnu.org/licenses/>.

case "${reason:-}" in
	PREINIT)
		# The DHCP client is requesting that an interface be
		# configured as required in order to send packets prior to
		# receiving an actual address. - dhclient-script(8)

		#if [ -n "$alias_ip_address" ]; then
		#	# Bring down alias interface. Its routes will disappear too.
		#	ifconfig $interface:0- inet 0
		#fi

		# silence the kernel printk
		echo "0" >/proc/sys/kernel/printk

		# basic interface setup if it is not already up
		ip link show "${interface:?}" | grep -qw UP ||
			ip link set "$interface" dynamic off up

		# We need to give the kernel some time to get the interface up.
		sleep 1
		# echo "$PRINTK" >/proc/sys/kernel/printk
		;;
	BOUND|RENEW|REBIND|REBOOT)
		# shellcheck disable=SC2086
		set -- ${new_routers:-}  # IFS
		first_router="$1"

		i=1
		for new_nameserver in ${new_domain_name_servers:-}
		do
			eval "nameserver_$i=$new_nameserver"
			i=$((i+1))
		done
		if [ -n "${dhclientscript_outputfile:-}" ]; then
			cat >"$dhclientscript_outputfile" <<%EOF%
${interface}_ip="${new_ip_address:-}"
${interface}_netmask="${new_subnet_mask:-}"
${interface}_broadcast="${new_broadcast_address:-}"
gateway="${first_router:-}"
nameserver_1="${nameserver_1:-}"
nameserver_2="${nameserver_2:-}"
nameserver_3="${nameserver_3:-}"
domainname="${new_domain_name:-}"
%EOF%
		fi
		;;
esac

exit 0
