#!/bin/bash -eu

if ip_command="$(type -P ip)" && dhcp_client="$(type -P dhcpcd)" &&
	[[ -z $("$ip_command" -o address show scope global) ]]; then
	"$dhcp_client" -4 -b || true
fi

host_key="$RUNTIME_DIRECTORY/ssh_host_ed25519_key"
if [[ ! -s "$host_key" ]]; then
	ssh-keygen -q -t ed25519 -N '' -f "$host_key"
fi
sshd_path=/usr/sbin/sshd
if [[ ! -x "$sshd_path" ]]; then
	echo "SSH server is unavailable at $sshd_path." >&2
	exit 1
fi
exec "$sshd_path" -D -e -f /dev/null -h "$host_key" \
	-o PidFile="$RUNTIME_DIRECTORY/sshd.pid" -o AuthorizedKeysFile=/etc/kopidel-instdebug.pub \
	-o AuthenticationMethods=publickey -o AllowUsers=root \
	-o PermitRootLogin=prohibit-password -o UsePAM=yes -o DisableForwarding=yes
