-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify-send
63 lines (52 loc) · 1.46 KB
/
notify-send
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
###############################################################
# Unofficial 'Bash strict mode' #
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ #
###############################################################
set -euo pipefail
IFS=$'\n\t'
###############################################################
WAIT_FOR_DISPLAY_TIMEOUT=${1:-30}
# Simplified from https://stackoverflow.com/a/49533938/2268426
DEBUG=${DEBUG:-false}
error_print() {
local text="${1}"
echo "${text}" >&2
}
get_display() {
pgrep -a Xorg | awk '{print $3}'
}
get_user_using_display() {
local display_to_find_user_for="${1}"
who | grep "(${display_to_find_user_for})" | awk '{print $1}' | head -n 1
}
run_main() {
display="$(get_display)"
if [[ -z "${display}" ]]; then
error_print "Unable to get a display"
exit 1
fi
for _ in {1..${WAIT_FOR_DISPLAY_TIMEOUT}}; do
user="$(get_user_using_display "${display}")"
if [[ -n "${user}" ]]; then
break
else
sleep 1
fi
done
if [[ -z "${user}" ]]; then
error_print "No user found to be using display in time - skipping notification"
exit 1
fi
if [[ "$(whoami)" == "${user}" ]]; then
notify-send.sh "$@"
else
sudo -u "${user}" \
"DISPLAY=${display}" \
"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u "${user}")/bus" \
notify-send.sh "$@"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
run_main "$@"
fi