# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

if [ -n "$UTOOLS_BASH_PROMPT" ]; then
    # PS1
    if [ "$UNAME_SYSTEM" != "Linux" -o "$USER" == "$(id -un)" ]; then
        DEFAULT_PS1='\u@\h \w \$ '
    else
        DEFAULT_PS1="$USER"'(\u)@\h \w \$ '
    fi
    if [ "$UTOOLS_CHROOT" == "1" ]; then
        DEFAULT_PS1="[CHROOT] $DEFAULT_PS1"
    elif [ -n "$UTOOLS_CHROOT" ]; then
        DEFAULT_PS1="[CHROOT:$UTOOLS_CHROOT] $DEFAULT_PS1"
    fi
    GENERATE_PS1='function __genps1() {
local cvs svn git uinst woinst suffix p="$PWD"
if [ -n "$UTOOLS_AUTO" ]; then
  PS1=
  while [ "$p" != "$HOME" -a "$p" != "" ]; do
    if [ -z "$cvs" -a -d "$p/CVS" ]; then
      PS1="${PS1:+$PS1,}cvs$suffix"
      cvs=1
    fi
    if [ -z "$svn" -a -d "$p/.svn" ]; then
      if [ "$UTOOLS_AUTO" != "f" ]; then
        flag="?svn"
      elif [ -n "$(svn status -q --ignore-externals 2>/dev/null)" ]; then
        flag="*svn"
      else
        flag="svn"
      fi
      PS1="${PS1:+$PS1,}$flag$suffix"
      svn=1
    fi
    if [ -z "$git" -a -d "$p/.git" ]; then
      if [ "$UTOOLS_AUTO" != "f" ]; then
        flag="?git"
      elif [ -n "$(git status -s 2>/dev/null)" ]; then
        flag="*git"
      else
        flag="git"
      fi
      branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
      [ "$branch" != "master" ] && flag="$flag:$branch"
      if [ -n "$UTOOLS_NOPUSH" ]; then
        flag="$flag,nopush"
      fi
      PS1="${PS1:+$PS1,}$flag"
      git=1
    fi
    if [ -z "$uinst" ]; then
      if [ -f "$p/.uinst.conf" ]; then
        PS1="${PS1:+$PS1,}uinst$suffix"
        uinst=1
      fi
      if [ -f "$p/.udir" ]; then
        if grep -q "^udir_types=.*uinst" "$p/.udir"; then
          PS1="${PS1:+$PS1,}uinst$suffix"
          uinst=1
        fi
      fi
    fi
    if [ -z "$woinst" ]; then
      if [[ "${p##*/}" == *.framework ]]; then
        PS1="${PS1:+$PS1,}woinst$suffix"
        woinst=1
      fi
      if [[ "${p##*/}" == *.woa ]]; then
        PS1="${PS1:+$PS1,}woinst$suffix"
        woinst=1
      fi
    fi
    suffix="/..."; p="${p%/*}"
  done
  PS1="${PS1:+[$PS1] }$DEFAULT_PS1"
else
  PS1="$DEFAULT_PS1"
fi
}; __genps1; unset -f __genps1'

    # PROMPT_COMMAND
    if [ "$UNAME_SYSTEM" == "Darwin" ]; then
        # Cas particuliers
        [ "$TERM" == "vt100" ] && TERM=xterm
    fi

    case "$TERM" in
    xterm*|rxvt|Eterm|eterm)
        PROMPT_COMMAND='
if [ -n "$UTOOLS_AUTO" ]; then
  echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~} $(
  if [ -f build.properties ]; then
    if grep -q "^project\(_\|\.\)name=" build.properties; then
      if grep -q "^project\.name=" build.properties; then
        echo -n "[$(sed -ne "/^project\.name=/{s/^project\.name=//
p
}" build.properties)"
      elif grep -q "^project_name=" build.properties; then
        echo -n "[$(sed -ne "/^project_name=/{s/^project_name=//
p
}" build.properties)"
      fi
      if [ -f VERSION.txt ]; then
        echo -n "-$(sed -ne "1{s/-r.*\$//
p
}" VERSION.txt)"
      fi
      echo -n "]"
    fi
  fi)\007"
fi
eval "$GENERATE_PS1"
'
        ;;
    screen)
        PROMPT_COMMAND='
if [ -n "$UTOOLS_AUTO" ]; then
  echo -ne "\033_[screen] ${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~} $(if [ -f build.properties ]; then
    if grep -q "^project\(_\|\.\)name=" build.properties; then
      if grep -q "^project\.name=" build.properties; then
        echo -n "[$(sed -ne "/^project\.name=/{s/^project\.name=//
p
}" build.properties)"
      elif grep -q "^project_name=" build.properties; then
        echo -n "[$(sed -ne "/^project_name=/{s/^project_name=//
p
}" build.properties)"
      fi
      if [ -f VERSION.txt ]; then
        echo -n "-$(sed -ne "1{s/-r.*\$//
p
}" VERSION.txt)"
      fi
      echo -n "]"
    fi
  fi)\033\\"
fi
eval "$GENERATE_PS1"
'
        ;;
    *)
        PROMPT_COMMAND='
if [ -n "$UTOOLS_AUTO" ]; then
  if [ -f build.properties ]; then
    if grep -q "^project\(_\|\.\)name=" build.properties; then
      if grep -q "^project\.name=" build.properties; then
        echo -n "\033[34m[$(sed -ne "/^project\.name=/{s/^project\.name=//
p
}" build.properties)"
      elif grep -q "^project_name=" build.properties; then
        echo -n "\033[34m[$(sed -ne "/^project_name=/{s/^project_name=//
p
}" build.properties)"
      fi
      if [ -f VERSION.txt ]; then
        echo -n "-$(sed -ne "1{s/-r.*\$//
p
}" VERSION.txt)"
      fi
      echo -e "]\033[0m"
    fi
  fi
fi
eval "$GENERATE_PS1"
'
        ;;
    esac

    UDIR_VISITED_DIRS=:
    PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND
}"'if [ -f .udir -a "${UDIR_VISITED_DIRS//:$PWD:/}" == "$UDIR_VISITED_DIRS" ]; then
  udir --show-note
  UDIR_VISITED_DIRS="$UDIR_VISITED_DIRS$PWD:"
fi'

    export PS1 DEFAULT_PS1 GENERATE_PS1 PROMPT_COMMAND UDIR_VISITED_DIRS
fi