forked from getumbrel/umbrel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
karen
executable file
·72 lines (59 loc) · 1.53 KB
/
karen
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
#!/usr/bin/env bash
# karen watches for signals and executes triggers in the events dir
# karen gets triggered a lot
check_root () {
if [[ $UID != 0 ]]; then
echo "Error: This script must be run as root."
echo "Can I speak to a manager please?"
exit 1
fi
}
check_if_not_already_running() {
if ps ax | grep $0 | grep -v $$ | grep bash | grep -v grep
then
echo "karen is already running"
exit 1
fi
}
check_dependencies () {
for cmd in "$@"; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "This script requires \"${cmd}\" to be installed"
exit 1
fi
done
}
check_root
check_if_not_already_running
check_dependencies fswatch readlink dirname
if [[ -n "$1" ]]; then
root_dir="$(readlink -f $1)"
else
root_dir="$(dirname $(readlink -f ${BASH_SOURCE[0]}))/events"
fi
signal_dir="$root_dir/signals"
trigger_dir="$root_dir/triggers"
if [[ ! -d "$root_dir" ]]; then
echo "Root dir does not exist '$root_dir'"
exit 1
fi
echo "karen is running in $root_dir"
fswatch -0 --event=PlatformSpecific $signal_dir | while read -d "" event; do
signal="${event#"$signal_dir"}"
signal="${signal#"/"}"
trigger="$trigger_dir/$signal"
args=""
echo "Got signal: $signal"
app_prefix="app-"
if [[ "$signal" == "$app_prefix"* ]]; then
# This is an app signal, let the app trigger handle it
trigger="$trigger_dir/app"
args="${signal#$app_prefix}"
fi
if test -x "$trigger"; then
echo "karen is getting triggered!"
"$trigger" $args &
else
echo "No trigger found"
fi
done