#!/bin/bash
# Sets a random wallpaper on each monitor; with --keep, redraws the last ones picked instead.

cache=${XDG_CACHE_HOME:-$HOME/.cache}/wallpapers

if [[ $1 != --keep || ! -s $cache ]]; then
    # One image per connected output, so --keep still has enough after an output was off
    count=$(xrandr --query | grep -c ' connected')
    shuf -n "$count" -e ~/.local/share/backgrounds/random/* > "$cache"
fi

mapfile -t walls < "$cache"
exec /usr/bin/feh --no-fehbg --bg-fill "${walls[@]}"
