#!/bin/sh
TITLE="$1"
shift
COMMAND=$1

ttyprefix=/dev/; #for linux systems with /dev/pts
cmdcolumn=5 ;# where to find command in ps output
ttycolumn=2 ;# where to find tty name in ps output
psargs=aww; 
get_tty_from_command () {
  local t
  t=`ps $psargs|awk "\\$$cmdcolumn~/$1/ {print \"$ttyprefix\" \\$$ttycolumn;exit}"`
  if [ -z "$t" ]
     then
        return 1
     fi
  check_tty $t
}

check_tty () {
  if [ ! -c $1 ] 
     then
       echo "$i is not valid tty name" >&2
       exit 1
     fi
  if [ ! -w $1 ]
     then
       echo "$i is not writable for you" >&2
       exit 1
     fi
  tty=$1
}

if get_tty_from_command $COMMAND; then
	echo -n '[1t[5t' >$tty
else
	xterm -T "$TITLE" -name $COMMAND -e "$@" &
fi	
