# 2016-02-22 20:20 CET


if [ -f "$HOME/.profile_debug" ]; then
    logger "$$ (parent: $PPID): .bash_profile triggered"
    debug=1
fi


if [ -z "$I_CAN_HAS_PROFILE" ]; then
    [ -n "$debug" ] && logger "$$: In .bash_profile, but .profile hasn't run -- rectifying..."
    source ~/.profile
    [ -n "$debug" ] && logger "$$: ...rectified, back in .bash_profile"
fi

if [ -n "$I_CAN_HAS_BASH_PROFILE" ]; then
    [ -n "$debug" ] && logger "$$: .bash_profile already activated, aborting..."
    return
fi



_ps1() {
   local exit=$?

   #local jobs=0
   #while IFS= read -r line <<<$(jobs); do
   #    jobs=$(($jobs+1))
   #done
   local jobs=$(jobs |wc -l |tr -d ' ')

   local BGRE='\[[1;32m\]'
   local BBLU='\[[1;34m\]'
   local BYEL='\[[1;33m\]'
   local BBLA='\[[1;30m\]'
   local BRED='\[[1;31m\]'
   local BGRA='\[[1;36m\]'
   local BWHI='\[[1;37m\]'

   local GRE='\[[0;32m\]'

   local RST='\[[0m\]'

   local ONMAG='\[[45m\]'
   local ONYEL='\[[43m\]'
   local ONRED='\[[41m\]'

   local BEEP='\[\]'


   PS1="$RST"

   if [ "$USER" = "root" ]; then
       PS1="$PS1$BWHI$ONRED \u $RST at "
   else
       PS1="$PS1$BGRE\u$RST at "
   fi

   if [ -n "$SSH_CLIENT" -a -z "$BOB_IS_LOCAL" ]; then
       PS1="$PS1$BBLA$ONYEL \h $RST"
   else
       PS1="$PS1$BBLU\h$RST"
   fi

   PS1="$PS1 in $BGRA\w$RST "

   if [ "$jobs" -ne 0 ]; then
       PS1="$PS1$GRE$ONMAG $jobs $RST"
   fi

   if [ "$exit" -ne 0 ]; then
       PS1="$PS1$BYEL$ONMAG $exit $RST"
   fi

   if [ -n "$CONDA_DEFAULT_ENV" ]; then
       PS1="$RST(conda:$CONDA_DEFAULT_ENV) $PS1"
   fi

   PS1="$BEEP$PS1\n$BRED#$RST "

   if [ -z "$INSIDE_EMACS" ]; then
       PS1="$BEEP$PS1"
   fi
}


export -f _ps1

if [ -f "$HOME/.bash_fun" ]; then
   . "$HOME/.bash_fun"

   Spadd MANPATH /usr/share/man
   Spadd MANPATH /usr/local/man

   Spadd PATH $HOME/.local/bin
   Spadd PATH $HOME/s
fi

export I_CAN_HAS_BASH_PROFILE=verily


[ -t 0 ] &&  {
    [ -f ~/.profile_debug ] && logger "$$: .bash_profile, interactive detected; triggering .bashrc"
    [ -f ~/.bashrc ] && . ~/.bashrc
} || {
    [ -f ~/.profile_debug ] && logger "$$: .bash_profile, non-interactive detected; not triggering .bashrc"
}
