############ start of gprofile #######################
# gprofile: graphterm bash setup file
# Execute as
#   source $GTERM_DIR/bin/gprofile
# or append to .bash_profile

if [[ -z "$GTERM_COOKIE" ]]; then
   if [[ "$LC_GRAPHTERM" =~ "GTERM_EXPORT=" ]]; then
      gterm_vars="$LC_GRAPHTERM"
   elif [[ "$LC_TELEPHONE" =~ "GTERM_EXPORT=" ]]; then
      gterm_vars="$LC_TELEPHONE"
   else
      gterm_vars=""
   fi

   if [[ -n "$gterm_vars" ]]; then
      vardefs=(${gterm_vars//|/ })
      for vardef in "${vardefs[@]}"; do
         comps=(${vardef//=/ })
         export ${comps[0]}="${comps[1]}"
      done

      if [[ -n "$LC_PROMPT_COMMAND" ]] && [[ -n "$LC_GTERM_PROMPT" ]]; then
         # Set prompt command
         export GTERM_PROMPT="$LC_GTERM_PROMPT"
         export PROMPT_COMMAND="$LC_PROMPT_COMMAND"
      else
         export GTERM_PROMPT='\h:\W$ '
         export PROMPT_COMMAND='export PS1=$GTERM_PROMPT; echo -n `printf "\033"`"[?1150;0h$PWD"`printf "\033"`"[?1150;l"'
      fi
   fi
fi

## To access the graphterm toolchain across SSH, install graphterm on the remote machine and then
## uncomment the following line and set GTERM_DIR to the graphterm installation directory (if need be)

# export GTERM_DIR=graphterm_directory

if [[ -z "$GTERM_DIR" ]]; then
   if [ -d "$HOME/graphterm" ]; then
      export GTERM_DIR="$HOME/graphterm"
   else
      export GTERM_DIR=$(python -c 'import graphterm, os; print os.path.dirname(graphterm.__file__)' 2>/dev/null)
   fi
fi

if [[ -n "$GTERM_DIR" ]]; then
   [[ "$PATH" != */graphterm/* ]] && PATH="$GTERM_DIR/bin:$PATH"
fi

############# end of gprofile #####################
