# needs to be sourced from .bashrc and .bash_profile
#:~/bin/prompt-colors+title.bash
#or /etc/profile.d/prompt-colors+title.sh
#
# prompt-colors+title.bash
# set prompt colors and window title (workdir or running cmd) in bash
# (c) 2004,2008 by Matej Sustr
#
#

# only if interactive:
if [[ $- = *i* ]]; then

  # color prompt (without the leading \[):
  if [ `id -u` -eq 0 ]; then
	endpart="\[\033[1;31m\]\\\$\[\033[0m\] "
  	PS2="\[\033[1;30m\]>\[\033[0m\] "
  else
	endpart="\[\033[0m\]\\\$ "
  	PS2="> "
  fi
  PS1="\033[1;36m\]\u@\h\[\033[0m\]:\[\033[33m\]\w$endpart"
  unset endpart
  case $TERM in
	linux|vt*)
		# color prompt set above, no window title
		PS1="\[$PS1"
		;;
	xterm*)
		PS1="\[\033]0;\u@\h: \w\007$PS1"
		# get rid of command-line escape chars in $BASH_COMMAND
		tit="\e]0;`whoami`@`hostname -s`: \${BASH_COMMAND//\\\\/}\007"
		;;
	screen*)
		if [ "$SHLVL" = "1" ]; then
			# screen running on different server,
			# want this host name in title:
			PS1="\[\033k\h:\w\033\134$PS1"
			tit="\ek`hostname -s`:\${BASH_COMMAND//\\\\/}\e\134"
		else
			# screen running here, this will be enough:
			PS1="\[\033k\w\033\134$PS1"
			tit="\ek\${BASH_COMMAND//\\\\/}\e\134"
		fi
		;;
	*)
		# no color nor title in other terminals:
		PS1='\u@\h:\w\$ '
  esac
  if [ -n "$tit" ]; then
	# there is no easy way to get the tty device name in bash
	#t="/dev/$(tcsh -c 'echo $tty')"
	# but we can use the coreutils 'tty' (most systems should have one):
	t="`tty`"
	if [ -n "$t" -a -w "$t" ]; then
		trap "echo -en \"$tit\" > \"$t\"" DEBUG
	fi
	unset t tit
  fi

fi
