#!/bin/sh main() { # Load custom configuration if available if [ -f "$HOME/.local/etc/x/chwp" ] then log main "Sourcing $HOME/.local/etc/x/chwp" . "$HOME/.local/etc/x/chwp" fi directory=${CHWP_BASEDIR:-$HOME/pictures/wallpapers} size=${CHWP_SIZE:-1920x1080} timeout=${1:-0} walldir="$directory/$size" log main "Checking $walldir for wallpapers" # Fail early if there's no wallpaper directory if [ ! -d "$walldir" ] then log main "No such directory: $walldir" exit 1 fi CACHEFILE="$(mktemp)" log main "Using $CACHEFILE as CACHEFILE" find "${walldir}" -type f > "$CACHEFILE" log main "Found $(wc -l "$CACHEFILE" | awk '{ print $1 }') wallpapers" # If no timeout was set, just change it once if [ "$timeout" -eq 0 ] then log main "No timeout set, only updating once" set_papes exit fi # Otherwise, change it every so often while : do set_papes sleep "${timeout}" done } set_papes() { log set_papes "Gathering wallpapers" pape="$(make_pape)" log set_papes "Setting wallpaper to $pape" feh --bg-fill "$pape" 2>&1 | log_pipe "feh" log set_papes "Cleaning up temporary wallpaper image" rm -f -- "$pape" } make_pape() { log make_pape "Creating wallpaper" output="$(mktemp)" imagelist="$(mktemp)" monitorlist="$(mktemp)" if [ -z "$CHWP_MONITORS" ] then log make_pape "Trying to determine screens" xrandr -q \ | grep -F ' connected ' \ | grep -Eo '[[:digit:]]+x[[:digit:]]+' \ > "$monitorlist" else log make_pape "Using \$CHWP_MONITORS for monitorlist" printf "%s" "$CHWP_MONITORS" \ | tr " " "\n" \ | grep -v "^$" \ > "$monitorlist" fi log make_pape "Found $(wc -l "$monitorlist" | awk '{ print $1 }') screen(s)" while read -r resolution do log make_pape "Adding wallpaper for $resolution sized screen" current_pape=$(get_pape "$resolution") if [ "$current_pape" = "" ] then log make_pape "No wallpapers for $resolution" current_pape="$(mktemp --tmpdir XXXXX.png)" convert -size "$resolution" canvas:gray "$current_pape" else log make_pape "Using $current_pape" fi printf '"%s"\n' "$current_pape" >> "$imagelist" unset resolution unset current_pape done < "$monitorlist" # shellcheck disable=SC2046 # shellcheck disable=SC2086 log make_pape "Combine all resolutions into a single image" eval convert +append $(cat $imagelist) "$output" 2>&1 | log_pipe "convert" printf "%s" "$output" rm -f -- "$imagelist" } get_pape() { if [ ! -d "$directory/$1" ] then log get_pape "$directory/$1 does not exist" return 1 fi printf "%s\n" "$(find "$directory/$1" -type f | shuf | head -n 1)" } log() { printf "[%s] %s: %s\n" "$(date +%FT%T)" "$1" "$2" >&2 } log_pipe() { pipe_log="$(mktemp)" cat - > "$pipe_log" while read -r line do log "$1" "$line" done < "$pipe_log" rm -fr -- "$pipe_log" unset pipe_log } main "$@"