#!/bin/sh
# For when script calls itself
if [ -n "$PASSWORD" ]
then
pass show "$PASSWORD" &&
exit
exit 1
fi
if [ "$MENUCMD" = "tofi" ]
then
menucmd="tofi --matching-algorithm=normal"
else
menucmd="$MENUCMD"
fi
die ()
{
echo "$1" >&2
}
notify ()
{
notify-send -t 1000 "keyadd" "$1"
die "$1"
}
SSHFOLDER="$HOME/.ssh"
# Test if can connect to ssh-agent
ssh-add -l > /dev/null 2>&1
if [ $? -gt 1 ] # ignore if there are no identities
then
notify "Could not connect to agent."
exit 1
fi
if [ -z "${key:=$1}" ]
then
key="$(find "$SSHFOLDER" -iname "*.pub" |
sed "s,$SSHFOLDER/,," | # Clean
sed 's/\.pub$//' |
$menucmd)"
else
shift
fi
die "key: $key"
[ ! -f "$SSHFOLDER/$key" ] && exit 1
HOST=$(hostnamectl hostname)
die "host: $HOST"
if [ "$1" = "-d" ]
then
if ssh-add -q -d - < "$SSHFOLDER"/"$key".pub 2> /dev/null
then
notify "Deleted $key"
else
notify "Could not delete."
fi
# check if key is already added
elif ssh-add -l | grep -q "$(ssh-keygen -lf "$SSHFOLDER"/"$key")"
then
notify "Key already added."
else
export PASSWORD="keys/$HOST/ssh/$key"
export SSH_ASKPASS="$0"
ssh-add -q - < "$SSHFOLDER"/"$key" &&
notify "Added $key."
fi