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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
#!/bin/bash
API_BASE="https://api.bitbucket.org/1.0/repositories/portix/dwb_extensions/src/tip/src/"
DWB_API_BASE="https://api.bitbucket.org/1.0/repositories/portix/dwb/src/tip/extensions/"
REPO_BASE="https://bitbucket.org/portix/dwb_extensions/raw/tip/src"
DATA_FORMAT="?format=yaml"
SCRIPT_DIR="${XDG_CONFIG_HOME-${HOME}/.config}/dwb/userscripts"
LOADER="${SCRIPT_DIR}/extension_loader.js"
INST_DIR="${XDG_DATA_HOME-$HOME/.local/share}/dwb/extensions"
SYSTEM_DIR="@SYSTEM_DIR@"
DIFF_VIEWER=${DIFF_VIEWER-vimdiff}
EDITOR=${EDITOR-vim}
META_INFO="$INST_DIR/.metainfo"
WGET_FLAGS=-q
CURL_FLAGS=-s
QUIET=0
BIND=0
GREEN="\033[32m"
RED="\033[31m"
BOLD="\033[1m"
NC="\033[0m"
error() {
if [ ${QUIET} -eq 0 ]; then
printf "${RED}==>${NC} %s\n" "$@"
fi
}
die() {
error "$@"
exit 1
}
notify() {
if [ ${QUIET} -eq 0 ]; then
printf "${GREEN}==>${NC} %s\n" "$@"
fi
}
check_dir() {
if [ ! -d "$1" ]; then
notify "creating $1"
mkdir -p "$1"
fi
}
update_metainfo () {
notify "retrieving data"
curl "${CURL_FLAGS}" "${API_BASE}${DATA_FORMAT}" | \
awk '/^\s*-\s*{path:/ { p=substr($3, index($3, "/") + 1);
print substr(p, 0, length(p)-1)" "substr($5, 0, length($5)-1) }' \
> "${META_INFO}"
curl "${CURL_FLAGS}" "${DWB_API_BASE}${DATA_FORMAT}" |
awk '/^\s*-\s*{path:/ { p=substr($3, index($3, "/") + 1);
print substr(p, 0, length(p)-1)" "substr($5, 0, length($5)-1) }' \
>> "${META_INFO}"
}
list() {
local ext exts
if [ ! -f "${META_INFO}" ]; then
update_metainfo
fi
notify "available extensions:"
awk '{ print " * "$1 }' ${META_INFO}
}
get_default_config() {
local l_file
if [ -f ${INST_DIR}/$1 ]; then
l_file=${INST_DIR}/$1
elif [ -f ${SYSTEM_DIR}/$1 ]; then
l_file=${SYSTEM_DIR}/$1
fi
if [ "${l_file}" ]; then
awk "/\/\/>DEFAULT_CONFIG/ { f=0 } f;
/\/\/<DEFAULT_CONFIG/ { f=1; next }" "${l_file}"
fi
}
yes_no() {
local question="${GREEN}==>${NC} $1 (Y/n)?"
printf "${question}"
while read -p " "; do
case ${REPLY} in
""|Y|y) return 0;;
n|N) return 1;;
*) error "try again"
printf "${question}";;
esac
done
}
install_extension() {
local ret;
local output="${INST_DIR}/$1"
check_dir "${INST_DIR}"
if [ "$1" ]; then
if [ -f ${SYSTEM_DIR}/$1 ]; then
notify "using ${SYSTEM_DIR}/$1"
return 0;
else
notify "downloading $1"
wget "${WGET_FLAGS}" "${REPO_BASE}/$1" -O "${output}"
case $? in
7) die "wget error: network failure";;
8) die "wget error: server responded an error";;
0) return 0;;
esac
fi
else
show_help
fi
if [ -f ${output} ]; then
rm ${output}
fi
return 1
}
heredoc() {
read -r -d '' $1
}
edit_config() {
local extension="$1"
if grep -q "^//<$1" "${LOADER}"; then
local tmpfile=$(mktemp /tmp/${1}_XXXX.js)
awk -v extb="//<$extension" -v exte="//>$extension" '
$0~extb { f=1; } f;
$0~exte { f=0 }' ${LOADER} > "${tmpfile}"
$EDITOR ${tmpfile}
add_to_loader ${extension} "$(< "${tmpfile}")"
rm ${tmpfile}
else
error "no config for $1"
fi
}
do_update() {
local tmploader="$(mktemp)"
local conf_orig="$(mktemp /tmp/XXXXX.orig.js)"
local conf_new="$(mktemp /tmp/XXXXX.new.js)"
while [ "$1" ]; do
extension="$1"
notify "updating ${extension}"
cp "${LOADER}" "${tmploader}"
do_install ${extension}
awk -v extb="//<$1" -v exte="//>$1" '
$0~extb { f=1; } f;
$0~exte { f=0 }' ${tmploader} > "${conf_orig}"
awk -v extb="//<$1" -v exte="//>$1" '
$0~extb { f=1; print "// THIS CONFIG WILL BE DISCARDED"; next } f;
$0~exte { f=0 }' "${LOADER}" > "${conf_new}"
${DIFF_VIEWER} "$conf_orig" "$conf_new"
add_to_loader "${extension}" "$(< ${conf_orig})"
shift
done
rm "${tmploader}" "${conf_orig}" "${conf_new}"
}
update() {
local tmpfile=$(mktemp)
if [ -f "${META_INFO}" ]; then
mv "${META_INFO}" "${tmpfile}"
fi
update_metainfo
local -a exts=()
for ext in $(awk 'NR==FNR{a[$0]=$1; next} !a[$0] { print $1 }' "${META_INFO}" "${tmpfile}"); do
if [ -f "${INST_DIR}/$ext" ]; then
exts[${#exts[@]}]=${ext}
elif grep -q "//<${ext}" ${LOADER} 2>/dev/null; then
exts[${#exts[@]}]=${ext}
fi
done
if [ ${#exts[@]} -eq 0 ]; then
notify "Up to date"
else
if yes_no "Update ${BOLD}${exts[*]}${NC}"; then
do_update ${exts[@]}
fi
fi
rm ${tmpfile}
}
extensions_load() {
local l_text
heredoc l_text <<!
//<$1
extensions.load("$1", {
$(get_default_config "$1")
});
//>$1
!
echo "$l_text"
}
extensions_bind() {
local l_text
heredoc l_text <<!
//<$1
var config_$1 = {
$(get_default_config "$1")
};
extensions.bind("$1", "t${1:0:3}", {
load : true,
command : "toggle$1",
config : config_$1
});
//>$1
!
echo "${l_text}"
}
add_to_loader()
{
check_dir "$SCRIPT_DIR"
local l_config
local tmp_file=$(mktemp)
if [ "$2" ]; then
l_config="$2"
elif [ "$BIND" -eq 1 ]; then
l_config="$(extensions_bind $1)"
else
l_config="$(extensions_load $1)"
fi
if [ ! -f "${LOADER}" ]; then
printf "#!javascript\n\n" > "${tmp_file}"
printf "\n$l_config\n" >> "${tmp_file}"
else
awk -v extb="//<$1" -v exte="//>$1" -v config="$l_config" '
BEGIN { f=0 }
$0~extb { f=2; print config; }
$0~exte { f=1; next }
f!=2 { print }
END { if (f==0) { print config } }' ${LOADER} > ${tmp_file}
fi
mv ${tmp_file} ${LOADER}
}
do_install() {
install_extension "$1"
add_to_loader "$1"
}
cl_uninstall() {
local file="${INST_DIR}/$1"
local tmp_file=$(mktemp)
notify "uninstalling $1"
if [ -f "${file}" ]; then
notify "removing ${file}"
rm ${file}
fi
notify "updating ${LOADER}"
awk -v extb="//<$1" -v exte="//>$1" '
BEGIN { f=1 }
$0~extb { f=0 }
$0~exte { f=1; next }
f==1 { print } ' "$LOADER" > "${tmp_file}"
mv "${tmp_file}" "${LOADER}"
}
cl_install() {
update_metainfo
do_install "$1"
edit_config "$1"
}
cl_update() {
if grep -q "^//<$1" "${LOADER}"; then
update_metainfo
do_update "$1"
elif yes_no "${BOLD}$1${NC} is not installed, install it"; then
cl_install $1
fi
}
cl_bind() {
add_to_loader "$1" "$(extensions_bind $1)"
edit_config "$1"
}
cl_load() {
add_to_loader "$1" "$(extensions_load $1)"
edit_config "$1"
}
show_installed() {
notify "installed extensions:"
awk '/\/\/<\w+/ { print " * "substr($0, 4) }' "${LOADER}"
}
show_help()
{
cat <<!
usage: $0 [option] [argument]
Options:
-b <extension> load extension with extensions.bind
-e <extension> edit configuration for <extension>
-i <extension> install <extension>, installing an already installed extension
will also overwrite the config for that extension
-I <extension> install <extension>, installing an already installed extension
will also overwrite the config for that extension, uses
extensions.bind instead of extensions.load
-h show this help and exit
-q quiet
-l list extensions
-l <extension> load extension with extensions.load
-r <extension> uninstall <extension>
-s show installed extensions
-u update all extensions
-U <extension> update <extension>, this will also redownload the extension
even if it doesn't need to be updated
!
}
if [ ! "$1" ]; then
error "missing argument"
show_help
exit 1
fi
while getopts "L:b:i:I:r:e:uU:qlhs" opt; do
case $opt in
i) cl_install ${OPTARG};;
I) BIND=1; cl_install ${OPTARG};;
b) cl_bind ${OPTARG};;
L) cl_load ${OPTARG};;
r) cl_uninstall ${OPTARG};;
q) QUIET=1;;
u) update;;
U) cl_update ${OPTARG};;
l) list;;
e) edit_config ${OPTARG};;
s) show_installed ${OPTARG};;
h) show_help;;
?) show_help;;
esac
done
|