#!/bin/sh
# Seed each new Xfce profile once, then leave all later wallpaper choices to the user.
state="${XDG_STATE_HOME:-$HOME/.local/state}/newworld/backdrop-v1"
[ ! -e "$state" ] || exit 0
wall=/usr/share/backgrounds/newworld/NewWorld.jpg
[ -r "$wall" ] || exit 1

i=0
while [ "$i" -lt 10 ]; do
    props=$(xfconf-query -c xfce4-desktop -l 2>/dev/null | grep '/last-image$' || true)
    [ -z "$props" ] || break
    sleep 1
    i=$((i + 1))
done
if [ -z "$props" ]; then
    # Brand-new profiles can have no property until their first explicit backdrop choice.
    props=$(xrandr --query | awk '$2 == "connected" {print "/backdrop/screen0/monitor" $1 "/workspace0/last-image"}')
    [ -n "$props" ] || exit 0
fi
printf '%s\n' "$props" | while IFS= read -r prop; do
    xfconf-query -c xfce4-desktop -p "$prop" -s "$wall" 2>/dev/null ||
        xfconf-query -c xfce4-desktop -p "$prop" --create -t string -s "$wall" || exit 1
done || exit 1
mkdir -p "$(dirname "$state")" && touch "$state"
