#!/bin/sh

wallDir="$XDG_CONFIG_HOME/wallpapers"

# Acquire the list of connected displays
displays=$(xrandr | grep " connected" | sed 's/^\([a-Z,0-9,-]*\).*/\1/')

# If there is a wallpaper with a filename matching the display
# name, set it as the wallpaper for that display.
# ex: DP-1.jpg and DP-1.png are both valid filenames
# Otherwise, set the wallpaper to the default wallpaper
# (default.png or default.jpg)
setDisplay() {
  case $(ls $wallDir) in
    *"$1"*)
      wall=$(ls $wallDir | grep "$1" | head -n 1)
      xwallpaper --output $1 --zoom $wallDir/$wall ;;
    *)
      wall=$(ls $wallDir | grep "default" | head -n 1)
      xwallpaper --output $1 --zoom $wallDir/$wall ;;
  esac
}

# Set the wallpaper for each display
for display in $displays; do
  setDisplay $display
done