#!/bin/bash

_TYPE_=ed25519
_USER_=${USER}
_HOST_=$(hostname -f)
_EMAIL_="${_USER_}@${_HOST_}"
_DIR_="${HOME}/.ssh"
_FILE_=${_EMAIL_//@/_at_}
_FILE_=${_DIR_}/id_${_TYPE_}.${_FILE_//./_dot_}
_CFG_FILE_="${_DIR_}/config"

if [ ! -e "${_FILE_}" ] ; then
  echo 'generate ssh key pair first!' >&2
  exit 1
fi

cat >> "${_CFG_FILE_}" <<EOD
# only use the configured authentication identity and certificate files
IdentitiesOnly  yes

# default identity
Host  *
  IdentityFile  ${_FILE_}
EOD

_RETURN_=$?

if [ $_RETURN_ -ne 0 ] ; then
  echo 'failed to create ssh config' >&2
  exit 1
fi

chmod 0600 "${_CFG_FILE_}"
