-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_wallpaper_by_geo
executable file
·55 lines (44 loc) · 2.07 KB
/
set_wallpaper_by_geo
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
#!/bin/bash
# This script is expected to be executed on each startup
# In the end it would probably be nice to set a user symlink to the wallpaper
# any dynamic changing in /usr would not work on immutable systems anyway
# Get the current timezone offset using the date command
current_offset=$(date +%z)
wallpaper_dir="file:///usr/share/wallpapers/geo-wallpapers"
if [ -d ".git" ]; then
scriptdir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd );
wallpaper_dir="file://$scriptdir/png"
fi
# Declare an associative array with timezone as keys and wallpaper paths as values
declare -A timezones_wallpapers=(
["Indian/Mauritius"]="$wallpaper_dir/lemorne.png"
["Asia/Taipei"]="$wallpaper_dir/taipei.png"
# Add more timezones with their respective wallpaper paths
)
function get_utc_offset() {
local timezone=$1
echo $(TZ=$timezone date +%z)
}
closest_timezone=""
closest_wallpaper=""
min_offset_diff=99999 # A large initial value
# Convert offset to an integer value for comparison
current_offset_hours=$(echo $current_offset | sed 's/^+//;s/^0*//')
for timezone in "${!timezones_wallpapers[@]}"; do
place_offset=$(get_utc_offset "$timezone")
place_offset_hours=$(echo $place_offset | sed 's/^+//;s/^0*//')
# The absolute difference between current and place offset
offset_diff=$(( current_offset_hours - place_offset_hours ))
offset_diff=${offset_diff#-} # Absolute value
# Check if this is the smallest difference
if (( offset_diff < min_offset_diff )); then
min_offset_diff=$offset_diff
closest_timezone=$timezone
closest_wallpaper=${timezones_wallpapers[$timezone]}
fi
done
# Best would be to choose day/night for dark/light scheme ... not sure if we'll have enough time to do all variants :-)
#echo "The closest timezone to the current timezone offset ($current_offset) is: $closest_timezone"
echo "The corresponding wallpaper file is: $closest_wallpaper"
gsettings set org.gnome.desktop.background picture-uri $closest_wallpaper
gsettings set org.gnome.desktop.background picture-uri-dark $closest_wallpaper