-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckbg
executable file
·74 lines (65 loc) · 1.75 KB
/
checkbg
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
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
# Check sunrise/sunset and set desktop background accordingly
has() {
command -v "$1" >/dev/null 2>&1
}
if ! has setbg; then
echo "Error: setbg is not installed" >&2
exit 1
fi
# If `sunwait` is installed, use it to determine if it's day or night
if has sunwait; then
# Get latitude and longitude from ip address using `curl` or `wget`
# coords format is "latitude,longitude"
if has curl; then
coords=$(curl -s ipinfo.io/loc)
elif has wget; then
coords=$(wget -qO- ipinfo.io/loc)
fi
if [ -n "$coords" ]; then
# Extract latitude and longitude from coords
latitude=${coords%,*}
longitude=${coords#*,}
# Convert latitude and longitude format from [+-]0.0000 to
# 0.0000[NS] (latitude) or 0.0000[EW] (longitude)
case "$latitude" in
-*)
latitude="${latitude#-}S"
;;
*)
latitude="${latitude}N"
;;
esac
case "$longitude" in
-*)
longitude="${longitude#-}W"
;;
*)
longitude="${longitude}E"
;;
esac
if [ "$(sunwait poll "$latitude" "$longitude")" = "DAY" ]; then
setbg light
else
setbg dark
fi
exit
fi
# Let `sunwait` detect the location itself if we cannot get the coordinates
# from ipinfo.io
if [ "$(sunwait poll)" = "DAY" ]; then
setbg light
else
setbg dark
fi
exit
fi
# Else use current time to determine if it's day or night
current_hour=$(date +%H)
current_hour="${current_hour#0}"
if [ "$current_hour" -gt 6 ] && [ "$current_hour" -lt 18 ]; then
setbg light
else
setbg dark
fi