-
Notifications
You must be signed in to change notification settings - Fork 6
/
sctw
executable file
·99 lines (87 loc) · 3 KB
/
sctw
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
#!/bin/sh
# Copyright 2018 Moviuro <moviuro+git@gmail.com>
# Uses https://sunrise-sunset.org/api
# Uses https://www.tedunangst.com/flak/post/sct-set-color-temperature
# sctw is a sct wrapper
# takes $SCTW_LAT $SCTW_LNG for position, and calculates a comfortable screen
# temperature at the time it's run.
# requires curl(1), jq(1), xrandr(1)
myname="$(basename "$0")"
today="$(date "+%Y-%m-%d")"
: "${DISPLAY=":0"}"
: "${SCTW_CONFIG:="${XDG_CONFIG_HOME:=$HOME/.config}/$myname.rc"}"
: "${TMPDIR:="${XDG_RUNTIME_DIR:="/tmp"}"}"
if ! xrandr > /dev/null 2>&1 ; then
echo "Can't find a display: $DISPLAY; bailing out" >&2
exit 2
fi
_values="sunrise sunset civil_twilight_begin civil_twilight_end"
if [ -r "$SCTW_CONFIG" ]; then
. "$SCTW_CONFIG"
fi
if [ -n "$SCTW_LAT" ] && [ -n "$SCTW_LNG" ]; then
:
else
echo "Using 'Notre Maison' as coordinates; excellent restaurant BTW" >&2
SCTW_LAT="45.764457"
SCTW_LNG="4.827656"
fi
umask 077
mkdir "$TMPDIR/$myname" >/dev/null 2>&1
[ -d "$TMPDIR/$myname" ] || exit 3
myfile="$TMPDIR/$myname/$today.$SCTW_LAT-$SCTW_LNG"
mytmpfile="$myfile.tmp"
if [ -r "$myfile" ]; then
:
else
# Remove previous data: it's not needed anymore
rm "${TMPDIR:?WOW}/${myname:?DANGER}/"*
# Store and generate today's data
curl -s \
"https://api.sunrise-sunset.org/json?lat=${SCTW_LAT}&lng=${SCTW_LNG}&formatted=0&date=${today}" \
> "$mytmpfile"
for item in $_values; do
printf '%s=%s\n' "$item" "$(jq .results.$item < "$mytmpfile")"
done > "$myfile"
. "$myfile"
if [ -z "$sunrise" ] || [ -z "$sunset" ] || [ -z "$civil_twilight_begin" ] ||
[ -z "$civil_twilight_end" ]; then
echo "Missing value(s) in $myfile!" >&2
mv "$myfile" "$myfile".broken
exit 4
fi
if man date 2>&1 | grep -q GNU; then
printf '%s=%s\n' \
civil_twilight_begin "$(date -d "$civil_twilight_begin" +%s)" \
sunrise "$(date -d "$sunrise" +%s)" \
sunset "$(date -d "$sunset" +%s)" \
civil_twilight_end "$(date -d "$civil_twilight_end" +%s)" > "$myfile"
else
echo "Unsupported date(1), help me fix it." 2>&1
exit 5
fi
fi
. "$myfile"
: "${SCTW_DAY_K:=6500}"
: "${SCTW_NIGHT_K:=4500}"
# We're doing something linear from NIGHT_K to DAY_K from civil_twilight_begin
# to sunrise; and from sunset to civil_twilight_end.
Kdiff="$(( SCTW_DAY_K - SCTW_NIGHT_K ))"
now="$(date "+%s")"
night="$(( now < civil_twilight_begin ))" # It's dark
dawn="$(( now < sunrise ))" # It's getting brighter
day="$(( now < sunset ))" # It's day
dusk="$(( now < civil_twilight_end ))" # It's getting darker
if [ "$night" -eq 1 ]; then
sct "$SCTW_NIGHT_K"
elif [ "$dawn" -eq 1 ]; then
timediff="$(( now - civil_twilight_begin ))"
sct "$(( SCTW_NIGHT_K + Kdiff * timediff / ( sunrise - civil_twilight_begin ) ))"
elif [ "$day" -eq 1 ]; then
sct "$SCTW_DAY_K"
elif [ "$dusk" -eq 1 ]; then
timediff="$(( civil_twilight_end - now ))"
sct "$(( SCTW_NIGHT_K + Kdiff * timediff / ( civil_twilight_end - sunset ) ))"
else
sct "$SCTW_NIGHT_K"
fi