if [[ -n "${__included_kopidel_hostname-}" ]]; then
	return 0
fi
readonly __included_kopidel_hostname=1

readonly DEFAULT_HOSTNAME_TEMPLATE='alt-{mac}'

is_numeric_ipv4_address() (
	address="$1"
	remaining_bits=32
	while :; do
		IFS=. read -r part address <<< "$address"
		if [[ "$part" != [0-9]* ]]; then
			return 1
		fi
		printf -v value '%d' "$part" 2>/dev/null || return 1
		if [[ -z "$address" ]]; then
			(( value < (1 << remaining_bits) ))
			return
		fi
		if (( value > 255 || remaining_bits == 8 )); then
			return 1
		fi
		remaining_bits=$(( remaining_bits - 8 ))
	done
)

check_rfc1123_hostname() (
	LC_ALL=C
	in_name="$1"
	if [[ "$in_name" == *$'\n'* ]]; then
		IFS= read -r first_line <<< "$in_name"
		position=$(( $(printf '%s' "$first_line" | wc -c) + 1 ))
		printf "$(_ "RFC-1123: a line break at byte %d is not allowed.")\n" "$position" >&2
		return 1
	fi

	IFS=. read -r -a labels <<< "$in_name."
	number=0
	for label in "${labels[@]}"; do
		number=$(( number + 1 ))
		if [[ -z "$label" ]]; then
			printf "$(_ "RFC-1123: label %d is empty.")\n" "$number" >&2
			return 1
		fi
		if [[ "$label" == -* ]]; then
			printf "$(_ "RFC-1123: label %d starts with a hyphen.")\n" "$number" >&2
			return 1
		fi
		if [[ "$label" == *- ]]; then
			printf "$(_ "RFC-1123: label %d ends with a hyphen.")\n" "$number" >&2
			return 1
		fi
		if [[ "$label" =~ ^([A-Za-z0-9-]*)[^A-Za-z0-9-] ]]; then
			position=$(( $(printf '%s' "${BASH_REMATCH[1]}" | wc -c) + 1 ))
			printf "$(_ "RFC-1123: label %d, character %d after expansion: use an ASCII letter, digit or hyphen.")\n" "$number" "$position" >&2
			return 1
		fi
		length="$(echo -n "$label" | wc -c)"
		if (( length > 63 )); then
			printf "$(_ "RFC-1123: label %d is %d bytes after expansion; the limit is 63.")\n" "$number" "$length" >&2
			return 1
		fi
	done
)

valid_hostname_template() (
	LC_ALL=C
	in_template="$1"
	if [[ "$in_template" == *$'\n'* ]]; then
		check_rfc1123_hostname "$in_template"
		return 1
	fi
	if [[ "$in_template" == *'{mac}'* ]]; then
		placeholder='{mac}'
		zero_suffix=000000
		decimal_suffix=100000
	elif [[ "$in_template" == *'{rand}'* ]]; then
		placeholder='{rand}'
		zero_suffix=00000000
		decimal_suffix=10000000
	else
		echo "$(_ "Use exactly one placeholder ({mac} or {rand}).")" >&2
		return 1
	fi
	name="$(sed "s/$placeholder/$zero_suffix/" <<< "$in_template")" || return 1
	if [[ "$name" == *'{'* || "$name" == *'}'* ]]; then
		echo "$(_ "Use exactly one placeholder ({mac} or {rand}).")" >&2
		return 1
	fi
	check_rfc1123_hostname "$name" || return 1
	length="$(echo -n "$name" | wc -c)"
	if (( length > 64 )); then
		printf "$(_ "The expanded hostname is %d bytes. The system limit is 64.")\n" "$length" >&2
		return 1
	fi
	if ! is_numeric_ipv4_address "$name"; then
		name="$(sed "s/$placeholder/$decimal_suffix/" <<< "$in_template")" || return 1
		if ! is_numeric_ipv4_address "$name"; then
			return 0
		fi
	fi
	printf "$(_ "The template can produce a numeric IPv4 address (%s). Use a prefix such as lab-.")\n" "$name" >&2
	return 1
)

random_hostname_suffix() (
	cut -c "1-$1" /proc/sys/kernel/random/uuid
)

target_hostname_suffix() (
	LC_ALL=C
	in_net_dir="$1"
	set +f
	for iface in "$in_net_dir"/*; do
		if [[ ! -e "$iface/device" ]]; then
			continue
		fi
		if ! read -r type < "$iface/type" || [[ "$type" != 1 ]]; then
			continue
		fi
		if ! mac="$(tr 'A-F' 'a-f' < "$iface/address")"; then
			continue
		fi
		if [[ ! "$mac" =~ ^[[:xdigit:]][02468ace](:[[:xdigit:]]{2}){5}$ ||
			"$mac" == 00:00:00:00:00:00 ]]; then
			continue
		fi
		cut -d: -f4-6 <<< "$mac" | tr -d :
		return
	done
	random_hostname_suffix 6
)

render_target_hostname() (
	in_template="$1"
	in_net_dir="$2"
	if ! valid_hostname_template "$in_template"; then
		return 1
	fi
	if [[ "$in_template" == *'{rand}'* ]]; then
		suffix="$(random_hostname_suffix 8)" || return 1
		sed "s/{rand}/$suffix/" <<< "$in_template"
	else
		suffix="$(target_hostname_suffix "$in_net_dir")" || return 1
		sed "s/{mac}/$suffix/" <<< "$in_template"
	fi
)
