-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetupenv
More file actions
executable file
·158 lines (130 loc) · 3.65 KB
/
setupenv
File metadata and controls
executable file
·158 lines (130 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env bash
LINKED=0
FAILED=0
ERRORS=()
show_usage ()
{
echo
echo "Links dotfiles to your $HOME directory"
echo
echo "USAGE: $0 [-f] [--force]"
echo
echo " force Prevents confirmation prompt to override existing files"
echo
[ ! -z "$1" ] && exit "$1"
}
link_dotfiles ()
{
local FILE
if [ -z "$1" ]; then
return
fi
pushd "$1" > /dev/null || return
for FILE in .*
do
if [[ "$FILE" == "." ]] || [[ "$FILE" == ".." ]] || [[ "$FILE" == ".git" ]]; then
continue
fi
if [[ "$FILE" == ".ssh" ]]; then
# make our ~/.ssh if it doesn't exist
if ! mkdir -p "$HOME/.ssh"; then
ERRORS+=("Failed to create $HOME/.ssh")
((++FAILED))
continue
fi
chmod 700 "$HOME/.ssh"
# setup proper permissions on existing keys
[[ -e "$HOME/.ssh/id_rsa" ]] && chmod 600 "$HOME/.ssh/id_rsa"
[[ -e "$HOME/.ssh/id_rsa.pub" ]] && chmod 644 "$HOME/.ssh/id_rsa.pub"
[[ -e "$HOME/.ssh/id_ed25519" ]] && chmod 600 "$HOME/.ssh/id_ed25519"
[[ -e "$HOME/.ssh/id_ed25519.pub" ]] && chmod 644 "$HOME/.ssh/id_ed25519.pub"
touch "$HOME/.ssh/authorized_keys"
chmod 644 "$HOME/.ssh/authorized_keys"
touch "$HOME/.ssh/known_hosts"
chmod 644 "$HOME/.ssh/known_hosts"
# Generate allowed_signers from local signing key for git signature verification
local EMAIL
EMAIL=$(git config user.email 2>/dev/null)
if [ -n "$EMAIL" ] && [ -f "$HOME/.ssh/id_ed25519.pub" ]; then
echo "$EMAIL $(cat "$HOME/.ssh/id_ed25519.pub")" > "$HOME/.ssh/allowed_signers"
chmod 644 "$HOME/.ssh/allowed_signers"
fi
# copy any files
if cp -rfv "$PWD/$FILE/"* "$HOME/$FILE/"; then
((++LINKED))
else
ERRORS+=("Failed to copy $FILE")
((++FAILED))
fi
continue
fi
if [ -e "$FILE" ]; then
if [ -d "$HOME/$FILE" ]; then
rm -rf "$HOME/$FILE"
fi
if ln -fsv "$PWD/$FILE" "$HOME/$FILE"; then
((++LINKED))
else
ERRORS+=("Failed to link $FILE")
((++FAILED))
fi
fi
done
popd > /dev/null || return
}
if [ "$1" == "--help" ]; then
show_usage 64
fi
if [[ "$1" != "--force" && "$1" != "-f" ]]; then
echo
read -p "This may overwrite existing files in your home directory. Are you sure? (Y/n) " -n 1
echo
case $REPLY in
[Nn]*)
exit 64
;;
esac
fi
# Link dotfiles from this dir
link_dotfiles "${PWD}"
# Copy bin files from this dir
if [ -d "./bin" ]; then
cp -rfv "${PWD}/bin" "$HOME"
fi
# Source platform specific profile
UNAME=$(uname)
case $UNAME in
"")
;;
"Darwin")
PLATFORM=mac
;;
*)
PLATFORM="$(tr '[:upper:]' '[:lower:]' <<< "$UNAME")"
;;
esac
if [ -d "$PLATFORM" ]; then
pushd "$PLATFORM" > /dev/null || exit 1
# Link dotfiles from platform dir
link_dotfiles "${PWD}"
# Run setupenv if exists
if [ -x "./setupenv" ]; then
./setupenv
fi
# Copy bin files from platform dir
if [ -d "./bin" ]; then
cp -rfv "${PWD}/bin" "$HOME"
fi
popd > /dev/null || true
fi
# Summary
echo
echo "Done: $LINKED linked, $FAILED failed"
if [ ${#ERRORS[@]} -gt 0 ]; then
echo
echo "Errors:"
for err in "${ERRORS[@]}"; do
echo " - $err"
done
exit 1
fi