Watch
1
0
Fork
You've already forked dotfiles
0
dotfiles/.local/bin/randomize-background

141 lines
4.2 KiB
Shell
Executable file

#!/bin/sh
# Determine the next wallpaper by round robin from the cloud
# and install it as the background files referenced in
# ~/.config/gnome-settings/{misc,shell-extensions}.ini
#
# Candidates:
# - Pairs => Both "<name>-d.jpg" and "<name>-l.jpg" exist
# - Singles => Any other "*.jpg" not following the -d/-l scheme,
# used as dark and light at the same time
#
# The live `gsettings` URIs point at uniquely named copies
# because `gnome-shell` caches background images by file URI;
# `restore-gnome` cleans up that temporary state during the next login
set -eu
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
SOURCE_DIR="$HOME/Cloud/Null-Tech/Backgrounds"
TARGET_DIR="$XDG_DATA_HOME/backgrounds"
LOCAL_INI="$XDG_CONFIG_HOME/gnome-settings/local/temporary_background.ini"
STATE_FILE="$XDG_STATE_HOME/current-background.txt"
[ -d "$SOURCE_DIR" ] || { echo "No such folder: $SOURCE_DIR" >&2; exit 1; }
# One candidate per line, tab-separated: <dark-file> <light-file>
candidates=$(
for jpg in "$SOURCE_DIR"/*.jpg; do
[ -e "$jpg" ] || continue # because the glob does not expand if empty
case "$jpg" in
*-d.jpg) # Complete pairs only
stem="${jpg%-d.jpg}"
[ -f "$stem-l.jpg" ] && printf '%s\t%s\n' "$jpg" "$stem-l.jpg"
;;
*-l.jpg) # Already handled via its "-d" counterpart above
;;
*) # A single file to be used for both modes
printf '%s\t%s\n' "$jpg" "$jpg"
;;
esac
done
)
if [ -z "$candidates" ]; then
echo "No usable *.jpg files in: $SOURCE_DIR" >&2
exit 1
fi
echo "Found $(printf '%s\n' "$candidates" | wc -l) candidate(s)"
# The glob expands alphabetically => "$candidates" is already sorted
# => Pick the first candidate sorting strictly after the last-used one,
# falling back to the first one if necessary
# (e.g., for start, wrap-around, or missing state file)
last_background=$(cat "$STATE_FILE" 2>/dev/null || true)
next_background=$(printf '%s\n' "$candidates" | awk -F '\t' -v last_background="$last_background" '$1 > last_background { print; exit }')
[ -n "$next_background" ] || next_background=$(printf '%s\n' "$candidates" | head -n 1)
dark_source=$(printf '%s' "$next_background" | cut -f 1)
light_source=$(printf '%s' "$next_background" | cut -f 2)
echo "Chosen background: $(basename "$dark_source")"
dark_target="$TARGET_DIR/dark.jpg"
light_target="$TARGET_DIR/light.jpg"
# Create a unique name per run
# so that the URIs below never repeat
timestamp=$(date +%s)
dark_live_target="$TARGET_DIR/dark-tmp-$timestamp.jpg"
light_live_target="$TARGET_DIR/light-tmp-$timestamp.jpg"
mkdir -p \
"$TARGET_DIR" \
"$(dirname "$LOCAL_INI")" \
"$(dirname "$STATE_FILE")"
# Fixed-name copies, referenced as login-time defaults in the *.ini files
cp -- "$dark_source" "$dark_target"
cp -- "$light_source" "$light_target"
# Uniquely named copies for the live `gsettings` URIs
# to ensure `gnome-shell` uses the correct files right away
cp -- "$dark_source" "$dark_live_target"
cp -- "$light_source" "$light_live_target"
printf '%s\n' "$dark_source" > "$STATE_FILE"
Background=org.gnome.desktop.background
Screensaver=org.gnome.desktop.screensaver
gsettings set $Background picture-uri-dark "file://$dark_live_target"
gsettings set $Background picture-uri "file://$light_live_target"
gsettings set $Screensaver picture-uri "file://$dark_live_target"
# Persist the unique URIs for `restore-gnome`,
# which loads local/*.ini after the base *.ini files
cat > "$LOCAL_INI" <<EOF
# Written by $(basename "$0") - do not edit
[org/gnome/desktop/background]
picture-uri-dark='file://$dark_live_target'
picture-uri='file://$light_live_target'
[org/gnome/desktop/screensaver]
picture-uri='file://$dark_live_target'
EOF
# Remove the uniquely named copies of previously chosen wallpapers
find "$TARGET_DIR" -name 'dark-tmp-*.jpg' ! -name "dark-tmp-$timestamp.jpg" -delete
find "$TARGET_DIR" -name 'light-tmp-*.jpg' ! -name "light-tmp-$timestamp.jpg" -delete
echo "Installed as: $TARGET_DIR/{dark,light}.jpg"