#!/bin/sh

WAIT_SECONDS=8

read_os_name() {
	local pretty_name
	pretty_name='OpenWrt'
	if [ -r /etc/openwrt_release ]; then
		. /etc/openwrt_release
		pretty_name="${DISTRIB_DESCRIPTION:-${DISTRIB_ID:-OpenWrt} ${DISTRIB_RELEASE:-unknown}}"
	fi
	printf '%s\n' "$pretty_name"
}

read_one_kvm_version() {
	local version
	version="$(one-kvm --version 2>/dev/null | awk '{ print $NF }')"
	if [ -z "$version" ] && command -v opkg >/dev/null 2>&1; then
		version="$(opkg status one-kvm 2>/dev/null | sed -n 's/^Version: //p' | head -n 1)"
	fi
	printf '%s\n' "${version:-unknown}"
}

read_web_endpoint() {
	local data_dir
	data_dir="$(uci -q get one-kvm.main.data_dir 2>/dev/null)"
	/usr/libexec/one-kvm-web-endpoint "${data_dir:-/mnt/shared/one-kvm}" 2>/dev/null
}

read_ipv4_addresses() {
	command -v ip >/dev/null 2>&1 || return 0
	ip -o -4 address show scope global 2>/dev/null \
		| awk 'NF >= 4 { split($4, address, "/"); print $2, address[1] }'
}

default_interface() {
	command -v ip >/dev/null 2>&1 || return 0
	ip -4 route show default 2>/dev/null \
		| awk '{ for (i = 1; i < NF; i++) if ($i == "dev") { print $(i + 1); exit } }'
}

choose_web_ip() {
	local preferred_interface="$1"
	local addresses="$2"
	local address candidate
	for candidate in "$preferred_interface" \
		"$(uci -q get network.lan.device 2>/dev/null)" \
		"$(uci -q get one-kvm-os.main.uplink 2>/dev/null)"; do
		[ -n "$candidate" ] || continue
		address="$(printf '%s\n' "$addresses" | awk -v preferred="$candidate" '$1 == preferred { print $2; exit }')"
		if [ -n "$address" ]; then
			printf '%s\n' "$address"
			return 0
		fi
	done
	printf '%s\n' "$addresses" | awk '
		$1 ~ /^(docker[0-9]+|veth)/ || $1 ~ /^br-[0-9a-f]+$/ { next }
		NF >= 2 { print $2; exit }'
}

addresses=''
endpoint=''
second=0
while [ "$second" -le "$WAIT_SECONDS" ]; do
	addresses="$(read_ipv4_addresses)"
	endpoint="$(read_web_endpoint)"
	[ -n "$addresses" ] && [ -n "$endpoint" ] && break
	[ "$second" -eq "$WAIT_SECONDS" ] || sleep 1
	second=$((second + 1))
done

preferred_interface="$(default_interface)"
web_ip="$(choose_web_ip "$preferred_interface" "$addresses")"
endpoint_scheme="${endpoint%%://*}"
endpoint_authority="${endpoint#*://}"
endpoint_port="${endpoint_authority##*:}"
case "$endpoint_scheme" in http|https) ;; *) endpoint='' ;; esac
case "$endpoint_port" in ''|*[!0-9]*) endpoint='' ;; esac

printf '\n'
cat <<'EOF'

 ██████╗ ███╗   ██╗███████╗    ██╗  ██╗██╗   ██╗███╗   ███╗
██╔═══██╗████╗  ██║██╔════╝    ██║ ██╔╝██║   ██║████╗ ████║
██║   ██║██╔██╗ ██║█████╗█████╗█████╔╝ ██║   ██║██╔████╔██║
██║   ██║██║╚██╗██║██╔══╝╚════╝██╔═██╗ ╚██╗ ██╔╝██║╚██╔╝██║
╚██████╔╝██║ ╚████║███████╗    ██║  ██╗ ╚████╔╝ ██║ ╚═╝ ██║
 ╚═════╝ ╚═╝  ╚═══╝╚══════╝    ╚═╝  ╚═╝  ╚═══╝  ╚═╝     ╚═╝

EOF
printf '\n'
printf ' One-KVM version:  %s\n' "$(read_one_kvm_version)"
printf ' OS version:       %s\n' "$(read_os_name)"
printf ' Hostname:         %s\n' "$(hostname 2>/dev/null || printf unknown)"
if [ -n "$addresses" ]; then
	printf '%s\n' "$addresses" | while read -r interface address; do
		[ -n "$interface" ] && [ -n "$address" ] || continue
		printf ' IPv4 for %-8s %s\n' "${interface}:" "$address"
	done
else
	printf '%s\n' ' IPv4:             not assigned'
fi
printf '\n'
if [ -z "$endpoint" ]; then
	printf '%s\n' ' One-KVM Web UI:   waiting for service configuration'
elif [ -n "$web_ip" ]; then
	printf ' One-KVM Web UI:   %s://%s:%s\n' "$endpoint_scheme" "$web_ip" "$endpoint_port"
else
	printf ' One-KVM Web UI:   waiting for a network address (port %s)\n' "$endpoint_port"
fi
printf '\n'
printf '%s\n' ' For more information, help or support, visit:'
printf '%s\n' ' https://docs.one-kvm.cn/'
printf '\n'

# procd starts this process after rcS, so the banner is visible before any input.
# Keep the usual press-Enter step and login policy after displaying it.
exec /sbin/askfirst /usr/libexec/login.sh
