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

75 lines
2.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
set -eu
SOURCE_DIR="$HOME/Cloud/Null-Tech/Backgrounds"
TARGET_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/backgrounds"
STATE_FILE="${XDG_STATE_HOME:-$HOME/.local/state}/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 (e.g., for start, wrap-around, or missing state file)
last=$(cat "$STATE_FILE" 2>/dev/null || true)
chosen=$(printf '%s\n' "$candidates" | awk -F '\t' -v last="$last" '$1 > last { print; exit }')
[ -n "$chosen" ] || chosen=$(printf '%s\n' "$candidates" | head -n 1)
dark=$(printf '%s' "$chosen" | cut -f 1)
light=$(printf '%s' "$chosen" | cut -f 2)
echo "Chosen: $(basename "$dark")"
mkdir -p "$TARGET_DIR" "$(dirname "$STATE_FILE")"
cp -- "$dark" "$TARGET_DIR/dark.jpg"
cp -- "$light" "$TARGET_DIR/light.jpg"
printf '%s\n' "$dark" > "$STATE_FILE"
echo "Installed as: $TARGET_DIR/{dark,light}.jpg"