#
# .profile: basic environment setup
#

# Set language
export LANG=pt_BR.UTF-8

# Source functions and variables from other places
if [ -e "$HOME/.aliases" ]; then
  . $HOME/.aliases
fi

# Funcoes ZZ
#if [ -e "/usr/bin/funcoeszz" ]; then
#  . /usr/bin/funcoeszz
#fi

# Set PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/games:/usr/local/bin:/usr/local/sbin:/usr/local/games

# Add all ~/apps folders into PATH
if [ -d "$HOME/apps" ]; then
  PATH=$HOME/apps:$PATH

  for folder in `ls $HOME/apps`; do
    PATH=$HOME/apps/$folder:$PATH
  done
fi

# Export PATH
export PATH=$PATH

# Where most projects live
export WORKPATH="~/file:~/data:~/code:~/apps"

# See http://www.caliban.org/bash/#bashtips
export CDPATH=".:~:$WORKPATH"

# Expand workpath
#for folder in `echo $WORKPATH | sed -e "s|:| |g" -e "s|~|$HOME|g"`; do
#  PROJECTS="$PROJECTS`ls -d $folder/*`"
#done

# Based on http://superuser.com/questions/90535/how-do-i-turn-of-auto-echo-in-bash-when-i-cd
cd() {
  if [ -z "$*" ]; then
    destination=~
  else
    destination=$*
  fi

  # Check if its a project under CPATH
  cdpath=`command cd "${destination}" 2> /dev/null`

  # Got to folder
  command cd "${destination}" > /dev/null

  # Update screen title
  #
  # Doesn't deal about getting out of a project path
  # or if going into a project subfolder
  #
  #if [ ! -z "$cdpath" ] && [ ! -z "$STY" ]; then
  #  folder="`basename $cdpath`"
  #
  #  if echo "$PROJECTS" | grep -q "/$folder$"; then
  #    # Got inside a project folder
  #    stitle `basename $folder`
  #  fi
  #fi
}

# Special cd: change folder and set title
scd() {
  cd "$*"
  stitle `basename $*`
}

mkday() {
  mkdir `today` && cd `today`
}

mkmonth() {
  mkdir `month` && cd `month`
}

# Default editor
#if [ ! -z "$DISPLAY" ]; then
#  # Avoid "Avoiding protocol specified" error
#  export EDITOR="vim -X"
#else
#  export EDITOR="vim"
#fi
export EDITOR="vim"

# Basic git configuration
# See http://stackoverflow.com/questions/4192014/git-ps1-extremely-slow-in-kernel-tree
export GIT_PS1_SHOWDIRTYSTATE=""
export GIT_PS1_SHOWUNTRACKEDFILES=""

# Load git completion if needed
if [ ! -z "$BASH" ]; then
  if ! type __git_ps1 2> /dev/null | head -1 | grep -q function; then
    if [ -e "/etc/bash_completion.d/git-prompt" ]; then
      . /etc/bash_completion.d/git-prompt
    elif [ -e "/etc/bash_completion.d/git" ]; then
      . /etc/bash_completion.d/git
    fi
  fi
fi

# Less configuration
# See http://seclists.org/fulldisclosure/2014/Nov/74
export LESSSECURE="1"
export LESSOPEN=""
export LESSCLOSE=""

# Temporary folder
export TMPDIR="$HOME/temp/misc"
export TMP="$HOME/temp/misc"
mkdir -p $TMP

# Dir colors
eval "`dircolors -b ~/.dircolorsrc`"

# SSH and GPG Agents
if [ -e "$HOME/.ssh/id_rsa" ]; then
  mkdir -p $HOME/.keychain
  /usr/bin/keychain -q $HOME/.ssh/id_rsa

  if [ -e "$HOME/.keychain/$HOSTNAME-sh" ]; then
    . $HOME/.keychain/$HOSTNAME-sh
  fi

  if [ -e "$HOME/.keychain/$HOSTNAME-sh-gpg" ]; then
    . $HOME/.keychain/$HOSTNAME-sh-gpg
  fi

  #if [ -e "$HOME/apps/scripts/ssh-agent-eval" ]; then
  #  $HOME/apps/scripts/ssh-agent-eval
  #fi
fi

# GPG Agent
#if [ -e "$HOME/apps/scripts/gpg-agent-eval" ]; then
#  $HOME/apps/scripts/gpg-agent-eval
#fi

# Remove lost found folder if empty
if [ -d "$HOME/lost+found" ]; then
  rmdir $HOME/lost+found &> /dev/null
fi

# Make sure basic folder structure exists
mkdir -p $HOME/apps $HOME/file $HOME/mail $HOME/temp

# Make sure data symlink exists
if [ -e "/var/data" ] && [ ! -e "$HOME/data" ]; then
  ln -s /var/data $HOME/data
fi

# Make sure code symlink exists
if [ -e "/var/data/code" ] && [ ! -e "$HOME/code" ]; then
  ln -s /var/data/code $HOME/code
fi

# Make sure download symlink exists
if [ -e "/var/data/load" ] && [ ! -e "$HOME/load" ]; then
  ln -s /var/data/load $HOME/load
fi

# Custom configuration
if [ -e "$HOME/.custom/profile" ]; then
  . $HOME/.custom/profile
fi

# Python
if [ -e "$HOME/apps/python" ]; then
  export PYTHONPATH="$HOME/apps/python"
fi

# Bash specifics
if [ -n "$BASH_VERSION" ]; then
  # Command prompt if no system-wide custom prompt is available
  if ! type command_prompt &> /dev/null && [ -e "$HOME/apps/bash-prompt/prompt" ]; then
    . $HOME/apps/bash-prompt/prompt
  fi
fi

# Make sure we start at home
cd