#!/bin/sh -

# uses last match else default
imgdir="$HOME/l/imgbg/tiles"
bgkey="	redblack	#200000	NONE
	leatherR	#200000	$imgdir/leatherR.gif;style=tiled
	indiana		#200000	$imgdir/leatherR.gif;style=tiled
	silo		#200000	$imgdir/leatherR.gif;style=tiled
	gh		#200000	$imgdir/leatherR.gif;style=tiled
	kj		#200000	$imgdir/leatherR.gif;style=tiled
	greenblack	#002000	NONE
	leatherG	#002000	$imgdir/leatherG.gif;style=tiled
	leatherG2	#002000	$imgdir/leatherG2.gif;style=tiled
	kinzler		#002000	$imgdir/leatherG2.gif;style=tiled
	kinzler.com	#002000	$imgdir/leatherG.gif;style=tiled
	blueblack	#000020	NONE
	leatherB	#000020	$imgdir/leatherB.gif;style=tiled
	umich		#000020	$imgdir/leatherB.gif;style=tiled
	athanor		#000020	$imgdir/leatherB.gif;style=tiled
	greatlakes	#000020	$imgdir/leatherB.gif;style=tiled
	lighthouse	#000020	$imgdir/leatherB.gif;style=tiled
	armis2		#000020	$imgdir/leatherB.gif;style=tiled
	dataden		#000020	$imgdir/leatherB.gif;style=tiled
	isilon		#000020	$imgdir/leatherB.gif;style=tiled
	build		#000020	$imgdir/leatherB.gif;style=tiled
	utility		#202020	$imgdir/sworl.gif;style=tiled"
dfltbg="black	$imgdir/wood.gif;style=tiled"
NONE="$imgdir/pixel.gif;0x0+0+0"

# Something like this could be used in cases where urxvt backgroundPixmap's
# would crash, like RDP desktops.  But it's very hard to determine those
# contexts since this script is run in ssh environments and on servers
# without X.  So it's currently handled by just not using urxvt there --
# see xinitrc.
#bgkey=` echo "$bgkey"  | sed 's/	[^	]*$//'`
#dfltbg=`echo "$dfltbg" | sed 's/	[^	]*$//'`
#NONE=`  echo "$NONE"   | sed 's/	[^	]*$//'`

# xtbg - set or choose background color for xterm
# Steve Kinzler, steve@kinzler.com, Mar 14/Dec 15
# https://kinzler.com/me/home.html#x11

nodo=; quiet=; bad=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-q)	quiet=t;;
		-n)	nodo=t;;

		--)	shift; break;;
		-h)	bad=t; break;;
		-*)	bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)	break;;
		esac
		shift;;
	esac
done

case "$bad" in
?*)	cat << EOF 1>&2
usage: $0 [ -n ] [ -q ] [ color | @key ]
	-n	nodo mode, don't actually set color
	-q	quiet mode, don't report settings
With no argument, assumes a key of the current host.
color may be prepended with a tab and an urxvt-style background pixmap
specifier.  Use a specifier of "NONE" to suppress an already set pixmap.
EOF
		exit 1;;
esac

case "$#" in
0)	host=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
	case "$host" in
	'')	host=`uname -n`;;
	esac
	set @${host-localhost};;
esac

color="$*"
case "$color" in
@*)	color=` echo "$dfltbg"
		echo "$bgkey" | perl -ne 's/^\s*//; @F = split(/\t+/);
					  next if /^$/;
					  $F[1] =~ s/[a-z]/\[$&\u$&\]/g;
					  print join("\t", @F), "\n"' |
		while read patt clr
		do
			case "$color" in
			*$patt*)	echo "$clr";;
			esac
		done`
	color=`echo "$color" | tail -1`;;
esac

case "$color" in
*'	NONE')	color=`echo "$color" | perl -pe "s|NONE$|$NONE|"`;;
esac

case "$quiet" in
'')	echo "$color";;
esac

case "$nodo" in
'')	image=
	case "$color" in
	*'	'*)	
		image=`echo "$color" | sed 's/.*	//'`
		color=`echo "$color" | sed 's/	.*//'`;;
	esac
	case "$color" in
	?*)	xtctl -q -b "$color";;
	esac
	case "$image" in
	?*)	xtctl -q -i "$image";;
	esac;;
esac
