#!/bin/sh
#
# Copyright (C) 2023 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

set -e

usage()
{
	echo "$0 <path-to-key> [...]"
	exit 1
}

if [ $# -lt 1 ] ; then
	usage
fi

file="$1"
shift 1

key_type="$(basename "${file}" | sed 's#^id_##')"

if [ "${key_type}" != "rsa" ] && [ "${key_type}" != "ed25519" ] ; then
	echo "Error: Unsupported '${key_type}' key type"
	echo "Error: only 'ed25519' and 'RSA' keys are supported in this script"
	exit 1
fi

mkdir -p "$(dirname "${file}")"
ssh-keygen -t "${key_type}" -f "${file}" "$@"

echo "public key: ${file}.pub"
