alias ls="/bin/ls -F --color"
alias l="/bin/ls -Fl --color"
alias ll="ls -laF --color"
alias grep="/bin/grep --color"
alias pso="ps waw o pid,euid,ppid,start,stat,pcpu,pmem,args"
alias psoo="ps waw o pid,euid,ppid,start,stat,size,vsz,rss,args"
alias pstop='ps axuww | sort -g -r -k 3,1 -k 4,1 -k 5,1 -k 6,1'
alias psown='pso -C sshd | gawk "{ if ( \$_ ~ /sshd:.*pts/ ) { print \$_ } }" | grep -v gawk'
alias psotask='ps -o ppid,pid,tty,stat,start,vsz,rsz,pcpu,pmem,args -C'
alias realtop="LINES=300 top -H"
alias nodlines='grep -vE '\''^[\ \t]*(#|$)'\'''
alias listall="find -maxdepth 3 -type d \! -wholename '\./\.*'"

function mark () {
    perl -e 'print "="x80'
  	if [ -z "$1" ]; then
        echo
    else
        echo " $@"
    fi
}

function pulld () {
    pulidx=${pulidx:=1}
    len=$(( ${#DIRSTACK[*]} - 1 ))
    (( pulidx = ( pulidx ) % ( len ) + 1))
    declare -x pulld_idx
    rep="${DIRSTACK[$pulidx]}"
    cd "$rep"
}

# GIT
function Git_whichbranch () {
    branch=$( git status | awk '{print $NF; exit;}' )
    echo $branch
}
function Git_newbranch () {
    git pull
    git checkout -b "$1"
}
function Git_switchbranch () {
    git checkout "$1"
}
function Git_bpull () {
    git pull $( Git_whichbranch )
}
function Git_rebase() {
    git rebase -i master
    git status
}
function Git_setremote() {
    if [ -z "$1" ]; then
        echo "$0 <host> <path_to_source>"
        return
    fi
    HOST="$1"
    shift
    SRCPATH="$1"
    shift
    git remote add $HOST ssh://$HOST:$SRCPATH
    git pull ssh://$HOST:$SRCPATH
    git branch --set-upstream-to=$HOST/master settings
}

# DIVERS
# thanks cuonglm on https://unix.stackexchange.com/questions/218323/keep-bash-input-on-top-line-of-screen
prompt_on_top() {
  tput cup 0 0
  tput el
  tput el1
}

pre_cmd() {
  if [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] || [ -n "$COMP_LINE" ]; then
    return
  fi
  printf "\33[2J"
}

movieterm() {
        export PROMPT_COMMAND="prompt_on_top"
        trap 'pre_cmd' DEBUG
}

unmovie() {
        trap "" DEBUG
}

lsfile () {
  PREFIX=""
  processed=0
  Ncur=0
  while [ ! -z "$1" ]; do
    case "$1" in
      "-c")
        PREFIX="#"
        ((processed++))
        ;;
      "--")
        ;;
      *)
        if [ -e "$1" ]; then
          ((Ncur++))
          NAMES[$Ncur]="$1"
        else
          echo "doesn't exists $1"
        fi
        ((processed++))
        ;;
    esac
    shift
  done
  if [ $processed -eq 0 ]; then
    echo "no file, no time!"
    return 1
  fi

  i=0
  while (( Ncur != 0 )); do
    NAME=${NAMES[$Ncur]}
    echo -n $PREFIX"- Normal -- "
    ll --full-time $NAME
    for EACH in "atime " access "use   " "ctime " status ; do
      echo -n "-- $EACH -- "
      ll -L --full-time --time $EACH $NAME
    done
    unset NAMES[$Ncur]
    ((Ncur--))
  done
}

# if running in normal, do screen! (screen.xterm-256color)
if [ "${TERM/screen*/}" != "" ]; then
  if [ $( screen -ls | grep Attached | wc -l ) -eq 0 ]; then
    screen
  fi
fi

PS1='-\[\033[01;34m\]$?\[\033[00m\][\t]\[\033[01;32m\]\h\[\033[00m\]\w\[\033[01;31m\]\$\[\033[00m\] '
export PS1

export HISTTIMEFORMAT="Y%m%d-%H%M%S"
export HISTFILE="$HOME/.history/log-`date '+%Y%m%d-%H%M%S'`-$$"
