#!/bin/sh
# Generate a key if it does not exist
# Print and copy its public part
# ARGS
# - key filename
# - encryption method (default ed25519)
# - further args for ssh-keygen
keys="$HOME/.ssh/keys"
name="${1:-id_ed25519}"
mkdir -p "$keys"
test -f "$keys/$name.pub" || ssh-keygen -t ${2:-ed25519} -b 4096 -f "$keys/$name" -q -N ""
cat "$keys/$name.pub"
if test "$OSTYPE" = "cygwin"
then cat "$keys/$name.pub">/dev/clipboard
else xclip -sel clip 2>/dev/null <"$keys/$name.pub"
fi && echo "SSH Public key copied to clipboard" >&2 || true
