-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotsman.sh
executable file
·296 lines (245 loc) · 7.56 KB
/
dotsman.sh
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/sh
# dotsman.sh
# Exit immediately if any command fails
set -e
# Change directory to root of dotfiles
containing=$(dirname "$(realpath "$0")")
cd "$containing"
cd "$(git rev-parse --show-toplevel)"
# Source file
items_file="$containing/items.ini"
# Get variables
editor="${EDITOR:=nano}" # default to nano
terminal="${TERMINAL:=foot}" # default to foot
# Constants
valid_section_regex="[a-z-]+"
###############################################################################
# Non-stateful utilities
###############################################################################
# notify <msg>...
# Wrapper around notify-send
notify() {
notify-send "dotsman" "$@"
}
# Wrapper around envsubst with a poor man's fallback
subst() {
# In case envsubst is not available, use sed
if command -v envsubst > /dev/null 2>&1; then
envsubst
else
# The innermost sed is to escape slashes in path for outer sed command to be valid
sed "s/\$HOME/$(echo "$HOME" | sed 's:/:\\\/:g')/"
fi
}
# is_same_file <file1> <file2>
# Check if <file1> and <file2> are actually the same file by symlinks
is_same_file() {
file1="$1"
file2="$2"
if [ "$(stat -L -c %d:%i "$file1")" = "$(stat -L -c %d:%i "$file2")" ]; then
return 0
else
return 1
fi
}
###############################################################################
# File-parsing helpers
###############################################################################
# parse_sections <filename>
parse_sections() {
filename="$1"
# Note: BSD grep does not support GNU grep's -P flag
# shellcheck disable=SC2002
cat "$filename" | grep -E "^\[$valid_section_regex]$" | sed -E 's/\[|\]//g'
}
# extract_section <section_name> <filename>
# Extract a section from <filename> and output in the form of key-value pairs
extract_section() {
section_name="$1"
filename="$2"
# shellcheck disable=SC2002
cat "$filename" | awk '
BEGIN{state=0}
/\['"$section_name"'\]/&&state==0{state++;next}
state==1&&/\['"$valid_section_regex"'\]/{exit;}
state==1{print $0}
' | sed '/^$/d'
}
# extract_value <key> <key_value_pairs>
# Extract a value from key-value pairs
extract_value() {
key="$1"
key_value_pairs="$2"
echo "$key_value_pairs" | grep "^$key=" | sed "s/^$key=//"
}
###############################################################################
# Tasks
###############################################################################
# link_section <header>
# Recursive function to link section and child sections
link_section() (
# Use () instead of {} to scope variables by running in a subshell
header="$1"
# Extract relevant section from config file
key_value_pairs=$(extract_section "$header" "$items_file")
# Extract values
src=$(extract_value src "$key_value_pairs")
dest=$(extract_value dest "$key_value_pairs")
call=$(extract_value call "$key_value_pairs")
IFS=","
for child in $call; do
echo "Recursing: $header -> $child"
link_section "$child"
done
if [ -z "$src" ]; then
echo "Skipping, src is empty: $header"
return
fi
src_expanded=$(realpath "$src")
dest_expanded=$(echo "$dest" | subst)
dest_expanded_parent=$(dirname "$dest_expanded")
if [ ! -d "$dest_expanded_parent" ]; then
if [ "$dry_run" = true ]; then
echo mkdir -p "$dest_expanded_parent"
else
(set -x ; mkdir -p "$dest_expanded_parent")
fi
fi
if [ -e "$dest_expanded" ]; then
if is_same_file "$dest_expanded" "$src_expanded"; then
echo "Dest exists and matches source, skipping: $header"
return
fi
echo "Failed to create link: $dest_expanded exists"
exit 1
fi
if [ "$dry_run" = true ]; then
echo ln -s -v "$src_expanded" "$dest_expanded"
else
(set -x ; ln -s -v "$src_expanded" "$dest_expanded")
fi
)
run_rofi() {
# Parse section headers in config file
all_sections="$(parse_sections "$items_file")"
# Let user select which config
selected_config_name=$( (echo "$all_sections") | rofi -dmenu -p "Select config file")
# Extract relevant section from config file
key_value_pairs=$(extract_section "$selected_config_name" "$items_file")
# Extract values
file=$(extract_value file "$key_value_pairs")
sudo=$(extract_value sudo "$key_value_pairs")
posthook=$(extract_value posthook "$key_value_pairs")
cd=$(extract_value cd "$key_value_pairs")
selected_config_file_expanded=$(echo "$file" | subst)
if [ -z "$selected_config_file_expanded" ]; then
notify "No valid entry selected!"
exit
fi
if ! [ -e "$selected_config_file_expanded" ]; then
notify "Path to $selected_config_name ($selected_config_file_expanded) does not exist!"
fi
if [ "$sudo" = "true" ]; then
editor="sudoedit"
fi
if [ -n "$posthook" ]; then
cmd="$posthook"
else
cmd=':'
fi
magic="while $editor $selected_config_file_expanded || exit; do ($cmd) && break || (echo 'Command failed, press enter to edit'; read); done"
if [ -n "$cd" ]; then
magic="cd $cd; $magic"
fi
$terminal -e bash -o pipefail -c "$magic"
}
# run_install <program>
run_install() {
program="$1"
if [ $# -eq 1 ]; then
program="$1"
dry_run=true
elif [ $# -eq 2 ]; then
if [ "$1" = '--no-dry-run' ]; then
program="$2"
dry_run=false
elif [ "$2" = '--no-dry-run' ]; then
program="$1"
dry_run=false
else
echo "$usage"
exit 1
fi
else
echo "$usage"
exit 1
fi
# Parse section headers in config file
all_sections="$(parse_sections "$items_file")"
if ! echo "$all_sections" | grep -q "^$program\$"; then
if [ "$program" != "all" ]; then
echo "$program is not a valid entry!"
echo "$items_file contains these programs:"
# shellcheck disable=SC2086
echo $all_sections
exit 1
fi
fi
if [ "$program" = "all" ]; then
for section in $all_sections; do
key_value_pairs=$(extract_section "$section" "$items_file")
call=$(extract_value call "$key_value_pairs")
if [ -n "$call" ]; then
echo "Skipping: $section"
continue
fi
link_section "$section"
done;
else
link_section "$program"
fi
if [ "$dry_run" = true ]; then
echo "Run again with --no-dry-run"
fi
}
###############################################################################
# Main
###############################################################################
usage_install='install <program> [--no-dry-run]'
usage_rofi='rofi'
show_usage() {
echo 'usage:'
echo " $1"
if [ $# -gt 1 ]; then
echo " $2"
fi
}
main() {
# Behave differently if called from a symlink named "install"
if [ "$(basename "$0")" = 'install' ]; then
usage=$(show_usage "$usage_install")
if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi
run_install "$@"
exit 0
fi
usage=$(show_usage "$usage_install" "$usage_rofi")
if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi
if [ "$1" = 'rofi' ]; then
run_rofi
exit 0
fi
if [ "$1" = 'install' ]; then
shift 1
run_install "$@"
exit 0
fi
echo "$usage"
exit 1
}
main "$@"