#!/bin/sh

# Copies $* to the titlebar of the window.  If $1 is "-xxx",
# then /dev/xxx is the target window, and tset is used on /dev/xxx
# to determine the terminal type.

prog=`basename $0`

case "$#$1" in
1-h ) cat <<END
    Use: `basename $prog` [-dev] title...
    Copies \$* to the titlebar of the window.  If the first argument
    is of the form -xxx, then /dev/xxx is the target window, and tset
    is used on /dev/xxx to determine the terminal type.

    Examples:
    	titlebar blah blah blah
    sets your current window's titlebar to "blah blah blah".

	titlebar -pts/7 hello
    sets the titlebar of /dev/pts/7 to "hello".
END
    exit 0
    ;;
esac

x=`echo 'x\c'`
if [ "$x" = x ] ; then X1= ; X2='\c' ; else X1='-n' ; X2= ; fi

case "$1" in
    -* ) tty=`expr $1 : '.\(.*\)'`
	    shift
	    case "$tty" in
	    /dev/* ) ;;
	    * ) tty="/dev/$tty" ;;
	    esac
	    TERM=`tset - < $tty`
	    export TERM
	    ;;
    * ) tty="" ;;
esac

case "$TERM" in
    sun )
	prefix1="]l" suffix1="\\"
	prefix2="]L" suffix2="\\"
	;;
    *xterm | xterm* )
	prefix1="]1;" suffix1=""
	prefix2="]2;" suffix2=""
	;;
    hpterm* )
	l=`expr length "$*"`
	prefix1="&f0a0k${l}D" suffix1=""
	prefix2="&f0a-1k${l}D" suffix2=""
	;;
esac

if [ "$prefix1$prefix2$suffix1$suffix2" != "" ] ; then
    if [ "$tty" != "" ] ; then
	echo $X1 "$prefix1$*$suffix1$prefix2$*$suffix2$X2" > $tty
    else
	echo $X1 "$prefix1$*$suffix1$prefix2$*$suffix2$X2"
    fi
else
    echo "$prog: don't know what escape sequences to use for TERM $TERM"
fi
