#!/bin/bash

set -e -u

create_key() {
	local keytype=$1
	local file=$2

	printf " %-30s creating ..." "$file"

	ssh-keygen -q -f "$file" -N '' -t $keytype
	echo
	if which restorecon >/dev/null 2>&1; then
		restorecon "$file" "$file.pub"
	fi
	return 0
}


types=$(kusa-conf sshd.hostkeys_types 2>/dev/null || :)
types=${types:-rsa dsa ecdsa ed25519}

for keytype in $types
do
	file="ssh_host_${keytype}_key"
	[ -f "$file" ] || create_key $keytype $file
done

echo -e "\nchecking ...\n"
for keytype in $types
do
	file="ssh_host_${keytype}_key.pub"
	printf " %-30s " $file
	ssh-keygen -l -f "$file"
done

exit 0
