refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / ssh-pubkey / ssh-pubkey.sh
1 #!/bin/bash
2 set -e
3 set -u
4
5 function main() {
6
7     if [ ! -d "$HOME/.ssh" ]; then
8         mkdir -p "$HOME/.ssh/"
9         chmod 0700 "$HOME/.ssh/"
10     fi
11
12     if [ ! -f "$HOME/.ssh/config" ]; then
13         # for the benefit of VSCode
14         touch "$HOME/.ssh/config"
15         chmod 0644 "$HOME/.ssh/config"
16     fi
17
18     if [ ! -f "$HOME/.ssh/authorized_keys" ]; then
19         touch "$HOME/.ssh/authorized_keys"
20         chmod 0600 "$HOME/.ssh/authorized_keys"
21     fi
22
23     if [ ! -f "$HOME/.ssh/id_rsa" ]; then
24         ssh-keygen -b 2048 -t rsa -f "$HOME/.ssh/id_rsa" -q -N ""
25         echo >&2 ""
26     fi
27
28     if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
29         ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
30         echo >&2 ""
31     fi
32
33     # TODO use the comment (if any) for the name of the file
34     echo >&2 ""
35     #shellcheck disable=SC2088
36     echo >&2 "~/Downloads/id_rsa.$(whoami).pub":
37     echo >&2 ""
38     rm -f "$HOME/Downloads/id_rsa.$(whoami).pub"
39     cp -r "$HOME/.ssh/id_rsa.pub" "$HOME/Downloads/id_rsa.$(whoami).pub"
40     cat "$HOME/Downloads/id_rsa.$(whoami).pub"
41     echo >&2 ""
42 }
43
44 main