#!/bin/sh
# Depends on sxiv, dmenu and hsetroot
# Will display background chooser
# and set it using hsetroot

img=$(find ~/Pictures/Backgrounds/* | shuf | sxiv -itoq | tail -1)

# Exit if none chosen
[ -z "$img" ] && exit

# Find previous startup background command
prev=$(grep hsetroot "$XINITRC")

# Define wether or not hsetroot should
# set per monitor or treat multiple monitors as one
option=$(printf "Set per monitor\nTreat multiple monitors as one" | dmenu -i -p "Layout:")
case "$option" in
	"Set per monitor") new="hsetroot -cover $img" ;;
	"Treat multiple monitors as one") new="hsetroot -root -cover $img" ;;
	*) exit 1 ;;
esac

# Set background and change
# startup configuration with new one
$new && sed -i "s|$prev|$new|g" "$XINITRC"