#!/bin/sh -

widcmd='xwinid XTerm'

# xtitletag - set a tag string in the title of an X11 terminal window
# Steve Kinzler, steve@kinzler.com, Jan 02/Feb 04
# https://kinzler.com/me/home.html#x11

# NOTE: setup SendEnv/AcceptEnv WINDOWID in ssh to avoid more widcmd's
WINDOWID="${WINDOWID-`$widcmd`}"

case "$1" in
-h)	echo "$WINDOWID"
	echo "usage: $0 [ tag | | [',./;] | - | = ]" 1>&2; exit 1;;
esac

title=`xprop -id "$WINDOWID" | sed -n 's/^WM_NAME([^"]*"\(.*\)"$/\1/p'`

case "$*" in
=)		char=;       tag=;        patt=;;
-)		char=;       tag=;        patt='s/^.) *//';;
[\',./\;])	char="$*) "; tag=;        patt='s/^.) *//';;
'')		char=;       tag=;        patt='s/ *\[.*\]//';;
*)		char=;       tag=" [$*]"; patt='s/ *\[.*\]//';;
esac

xtitle -q "$char"`echo "$title" | sed "$patt"`"$tag" > /dev/tty

echo "$WINDOWID"
