#### related to bash history ####
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=5000
HISTFILESIZE=100000


#### related to key bindings ####

# search history with "control" + "cursor up" or "cursor down"
# this depends on the keyboard settings
bind '"\e[1;5A":history-search-backward'
bind '"\e[1;5B":history-search-forward'

# delete word with control + y (german keyboard)
bind -r "\C-y"
bind '"\C-y":kill-word'


#### related to bash display ####

# set a useful prompt
PS1='\w \$ '

# disable ^C in shell, when you press control + c
stty -echoctl


#### related to short commands (alias) ####
alias ..='cd ..'
alias e='evince'
alias c='cd'
alias s='soffice'
alias l='ls -ltra'

# mplayer:
alias m='mplayer -idx -fs -vf pp=lb'
alias m4='mplayer -idx -fs -vf pp=lb -aspect 4:3'
alias m16='mplayer -idx -fs -vf pp=lb -aspect 16:9'


# sets an alias for the last command
# quick and useful for often used commands
setAliasForLastCommand () {
  if [ $# -eq 1 ]; then

    # get last command from the history
    lastCommand=$(history | tail -n 2 | head -n 1 | sed -e 's|^\s*[0-9]*\s*||' -e 's|\x27|\x27"\x27"\x27|g')
    aliasName=$1

    # repeat until available alias found
    while true; do
      # check if alias exists
      type $aliasName >/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "$aliasName already exists - enter another alias name."
        read aliasName
      else
        break
      fi
    done

    # set last command as alias and write to file
    echo "alias $aliasName='$lastCommand'" >> ~/.bash_aliases

    # load the new alias
    . ~/.bash_aliases

  else
    # display usage information
    echo "Usage: setAliasForLastCommand <aliasName>"
    echo "short: a <aliasName>"
    echo
    echo "Sets an alias for the last command."
  fi
}

alias a='setAliasForLastCommand'


# load alias file if available
if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi


# handles the newest file or directory
# e.g. open newest file or go into the newest directory
handleNewFileOrDirectory () {
  lastFile=$(ls -tr | tail -n 1)
  fileType=$(file "$lastFile" | sed -e 's|.*: ||g' -e 's| *$||g')
  fileExtension=$(echo "$lastFile" | sed 's|.*\.||g' | tr [:upper:] [:lower:])

  # go into the newest directory
  if [ "$fileType" == "directory" ]; then
    cd "$lastFile"

  # open newest text file with text editor kate
  elif [ "$(echo "$fileType" | grep text)" != "" ]; then
    kate -u "$lastFile" > /dev/null 2>&1 &

  # open newest pdf file with evince
  elif [ "$(echo "$fileType" | grep PDF)" != "" ]; then
    evince "$lastFile" > /dev/null 2>&1 &

  # open doc or xls with open office
  elif [ "$fileExtension" == "doc" ] || [ "$fileExtension" == "xls" ]; then
    soffice "$lastFile" > /dev/null 2>&1 &

  # open png or jpg with eye of gnome
  elif [ "$fileExtension" == "png" ] || [ "$fileExtension" == "jpg" ]; then
    eog "$lastFile" > /dev/null 2>&1 &

  fi
}

# g like go
alias g='handleNewFileOrDirectory'
