#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later

###################################################################
### Post-installation script to configure a network boot server ###
###################################################################

# Safety first
set -o errexit
set -o noglob
set -o nounset
set -o errtrace

### INPUT PARAMETERS:

# The name of a regular user on the system
regular_user=

# DNS/NIS domain name (optional), auto-detected
domainname=

# List of DNS servers (optional), auto-detected, example: 8.8.8.8
dns4_servers=

# Optional DHCP pool, default "NetworkAddr+100 ... NetworkAddr+250"
dhcp4_pool_start=100
dhcp4_pool_end=250

# IPv4 gateway address (optional), can be set relative to a network address
ip4_routers=

# Wired network interface name (optional), can be auto-detected
wired_iface=

# Selected profile, default "altboot", alternative "talos"
profile=altboot

# Talos artifacts will be cached by default
taloscache=1

# Default Talos source server for downloading artifacts
talosserver="https://factory.altlinux.space"

# Default Talos schematic without any customizations
talosschematic=376567988ad370138ad8b2698212367b8edcb69b5fd68c80be1f2ec7d603b4ba

### INTERNAL VARIABLES:

# IP settings
ip4_address=
ip4_pfx_len=
ip4_netmask=
ip4_network=
ip4_broadcast=

# 1: if big endian byte order is used
bigendian=

# 1: configuration update is allowed
update_config=

# 1: the program configuration has been updated
config_updated=

# 1: the script should exit after checking the configuration
check_only=

# Non-empty: silent execution, default is empty
quiet=

# Non-empty: prints diagnostics for each processed action
verbose=

# List of activated services
start_services=dhcpd

# Full path to this script
scriptname="$(realpath -- "$0")"

# Short name of this program
progname="${scriptname##*/}"

# The default path to the program configuration
readonly defconf=/etc/sysconfig/netboot-setup

# Supplemental sources location
readonly libdir=/usr/libexec/netboot-setup

### ENTRY POINT:

# Use default configuration
[ ! -r "$defconf" ] ||
	. "$defconf"
umask 0022

# Bootstrap
. "$libdir"/nb-setup.sh
sub="$(find "$libdir" -mindepth 1 -maxdepth 1 -type f -and \
	-name '*.sh' -and -not -name 'nb-*.sh' -printf '%f ')_"
for module in $sub; do
	[ "$module" != _ ] ||
		continue
	. "$libdir/$module"
done
unset sub module

# Catch all unexpected errors
trap 'unexpected_error "${BASH_SOURCE[0]##*/}" "$LINENO"' ERR

# Parsing command line arguments
parse_cmdline "$@"

# This program requires root privileges to run
check_superuser

# Determining the byte order
check_byteorder

# Consistency check
msg "Checking configuration..."
[ -n "$wired_iface" ] ||
	wired_iface="$(find_wired_iface)"
check_wired_iface "$wired_iface"
check_dhcp4_pool ||
	fatal "Invalid DHCP pool: '%s - %s' [rv=%d]." \
		"$dhcp4_pool_start" "$dhcp4_pool_end" "$?"
check_ip4_routers
[ -n "$ip4_routers" ] || [ -z "$(find_wired_iface)" ] ||
	ip4_routers="$ip4_address"
check_domain_name
check_dns4_servers
check_username

# Exit if this action is completed
if [ -n "$check_only" ]; then
	msg "The configuration has been checked successfully!"
	exit 0
fi

# Applying the new configuration
"${profile}_setup"

# Activating gateway mode on this server
enable_gateway4

# Enabling services
for svc in $start_services; do
	if systemctl is-active "$svc" >/dev/null; then
		systemctl restart "$svc"
	else
		systemctl enable --now "$svc"
	fi
done

# Saving configuration
write_config

