#!/bin/bash

# Author: Weitian Leung <weitianleung@gmail.com>
# Version: 2.1
# License: GPL-3.0
# Description: set a picture as xfce4 wallpaper

# xfce4-desktop requires an absolute path.
wallpaper="$(realpath "$1")"

# check image
mime_type=`file --mime-type -b "$wallpaper"`
if [[ ! "$mime_type" == image/* ]]; then
        echo "Invalid image"
        exit 1
fi

# set to every monitor that contains image-path/last-image
properties=$(xfconf-query -c xfce4-desktop -p /backdrop -l | grep -e "screen.*/monitor.*image-path$" -e "screen.*/monitor.*/last-image$")

for property in $properties; do
        xfconf-query -c xfce4-desktop -p $property -s "$wallpaper"
done
