-
Notifications
You must be signed in to change notification settings - Fork 13
/
libhooks.sh
executable file
·63 lines (54 loc) · 1.78 KB
/
libhooks.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
#!/bin/bash
rebuild_config () {
config="$1"
hookdir="$2"
if [ $# != 2 ]; then
echo "BUG: rebuild_config called by $0 with incorrect # of parameters" >&2
return 1
fi
magic_string="# Autogenerated from $0 via rebuild_config"
magic_string_re="^# Autogenerated from ($0|.* via rebuild_config)"
if [ -L "$config" ]; then
echo "Error: $config is a symlink; won't overwrite." >&2
return 1
fi
if [ -e "$config" ]; then
if ! [ -s "$config" ]; then
echo "# $config is empty"
elif ! grep -Eq "$magic_string_re" "$config"; then
cat <<EOF >&2
Error: can't find '$magic_string_re' in $config
Presumably hand-written so won't overwrite; please break into parts.
EOF
return 1
fi
fi
echo "# Rebuilding $config ..."
cat <<EOF > "$config"
# Autogenerated from $0 via rebuild_config at `date`
EOF
# Ensure we have $ZDOT_FIND_HOOKS; if this is being invoked from
# be.sh then we probably don't.
source $ZDOTDIR/.shared_env
# sort by filename not by path
$ZDOT_FIND_HOOKS "$hookdir" | \
sed 's/\(.\+\)\/\(.\+\)/\2 -%- \1\/\2/' | \
sort -k1 | \
sed 's/.* -%- //' | \
while read conf; do
echo "# Appending $conf"
# Allow for executable hooks, for generating content dynamically,
# triggered by including a magic cookie in the hook file.
if grep -q '%% Executable hook %%' "$conf"; then
echo "# Output of $conf follows:" >> "$config"
if ! "$conf" >> "$config"; then
echo >&2 "$conf failed; aborting."
exit 1
fi
else
echo "# Include of $conf follows:" >> "$config"
cat "$conf" >> "$config"
fi
echo >> "$config"
done
}