#!/bin/sh

xrandr | awk '
function addWallpaper(i) {
	split($3, a, "+");
	b[bi] = a[2];
	c[bi] = $1;
	bi++
}

$2 == "connected" {
	for(i=1;i<=NF;i++) {
		if($i ~ /[0-9]+x[0-9]+\+[0-9]+\+[0-9]+/) {
			addWallpaper(i);
		}
	}
}

END {
	PROCINFO["sorted_in"] = "@val_num_asc";
	d = "feh --bg-center";
	for(i in b) {
		p="$HOME/.local/share/le_wallpaper/." c[i] ".png";
		if(system("test -f " p)) {
			p = lp;
		}
		lp = p;
		d=d " " p;
	}
	system(d);
}
'
