#!/bin/bash
#
# Aliases for Tranalyzer (tested with Bash and ZSH)
#
# Known issues:
#   [ZSH]
#       - alias lsx is already defined
#         fix: type 'unalias lsx' before sourcing this file
#
# To activate these aliases, append the following lines to ~/.bashrc or
# ~/.bash_aliases (make sure to replace $T2HOME with the actual path, e.g.,
# $HOME/int_tranalyzer/trunk):
#
#      if [ -f "$T2HOME/scripts/t2_aliases" ]; then
#          . "$T2HOME/scripts/t2_aliases"             # Note the leading '.'
#      fi
#
# TODO
#   - Improve ZSH/Bash completion
#       - tawk: ignore '$' when completing variable names
#       - describe options

# To force deactivation of the completion, set HAS_COMPLETION to 0
HAS_COMPLETION=1

# Private functions
_printerr() {
    printf "\e[0;31m$1\e[0m\n" >&2  # Red
}

_printok() {
    printf "\e[0;32m$1\e[0m\n"      # Green
}

_printwrn() {
    printf "\e[0;33m$1\e[0m\n" >&2  # Orange
}

_printinf() {
    printf "\e[0;34m$1\e[0m\n"      # Blue
}

if  [ $HAS_COMPLETION -eq 1 ]; then
    if hash complete 2> /dev/null || type compdef &> /dev/null; then
        HAS_COMPLETION=1
    else
        _printwrn "No valid completion program found."
        _printinf "Try installing 'bash-completion' with your package utility"
        HAS_COMPLETION=0
    fi
fi

# Required programs
if [ "$(uname)" = "Darwin" ]; then
    for i in gawk greadlink gsed pidof watch; do
        if ! type $i &> /dev/null; then
            deps="bash-completion coreutils gawk gnu-sed pidof watch"
            _printerr "t2_aliases: missing dependencies: $deps"
            _printinf "You may use homebrew to install them: brew install $deps"
            return 1
        fi
    done
    if [ $HAS_COMPLETION -eq 1 ]; then
        if [ -n "$ZSH_VERSION" ] && [[ $(zsh --version | awk '{print $2}') < 5.2 ]]; then
            _printwrn "Your zsh version does not support tab completion"
            HAS_COMPLETION=0
        fi
    fi
    AWK="$(which gawk)"
    READLINK="$(which greadlink)"
    SED="$(which gsed)"
    OPEN="open"
else
    for i in gawk readlink sed; do
        deps="coreutils gawk sed"
        if ! type $i &> /dev/null; then
            if hash apt-get 2> /dev/null; then
                pgrmname="apt-get"
                pgrmcmd="apt-get install"
            elif hash pacman 2> /dev/null; then
                pgrmname="pacman"
                pgrmcmd="pacman -S"
            elif hash yum 2> /dev/null; then
                pgrmname="yum"
                pgrmcmd="yum install"
            else
                pgrmname="your package utility"
            fi
            [ -n "$pgrmcmd" ] && pgrmcmd=": $pgrmcmd $deps"
            _printerr "t2_aliases: missing dependencies: $deps"
            _printinf "You may use $pgrmname to install them$pgrmcmd"
            return 1
        fi
    done
    AWK="$(which gawk)"
    READLINK="$(which readlink)"
    SED="$(which sed)"
    OPEN="$(which xdg-open)"
fi

# Check for completion
if [ -z "$ZSH_VERSION" ] && [ $HAS_COMPLETION -eq 1 ]; then
    if [ ! -f "/usr/share/bash-completion/bash_completion" ] ||
       [ -z "$(grep "^_init_completion()" /usr/share/bash-completion/bash_completion 2> /dev/null)" ]
    then
        if hash _get_comp_words_by_ref 2> /dev/null; then
            #_printwrn "Providing a simplistic implementation of _init_completion"
            _init_completion() {
                COMPREPLY=()
                _get_comp_words_by_ref cur prev words cword
            }
        else
            _printwrn "Deactivating completion as your bash-completion version does not provide the '_init_completion()' function."
            HAS_COMPLETION=0
        fi
    fi
fi

# Set T2HOME
if [ -n "$ZSH_VERSION" ]; then
    T2HOME="$(dirname "$($READLINK -f "${(%):-%x}")")/.."
else
    T2HOME="$(dirname "$($READLINK -f "$BASH_SOURCE")")/.."
fi
export T2HOME="$($READLINK -f "$T2HOME")"

# Tranalyzer
alias t2="$T2HOME/tranalyzer2/src/tranalyzer"
alias gt2="gdb --args $T2HOME/tranalyzer2/src/tranalyzer"
alias st2="sudo $T2HOME/tranalyzer2/src/tranalyzer"
alias tranalyzer="$T2HOME/tranalyzer2/src/tranalyzer"

alias tran="cd $T2HOME"
alias .tran="cd $HOME/.tranalyzer/plugins/"

# Access every plugin folder by its name,
# e.g., tcpFlags instead of cd $T2HOME/tcpFlags
for i in $T2HOME/*; do
    b=$(basename $i)
    if [ -d $i ] && [ "$b" != "doc" ]; then
        alias $b="cd $i"
    fi
done

alias awkf="$AWK -F'\t' -v OFS='\t'"
alias sortu="sort | uniq -c | sort -rn"

# Make some scripts globally accessible
alias protStat="$T2HOME/scripts/protStat"
alias rrdmonitor="$T2HOME/scripts/rrdmonitor"
alias rrdplot="$T2HOME/scripts/rrdplot"
alias t2build="$T2HOME/autogen.sh"
alias t2caplist="$T2HOME/scripts/t2caplist"
alias t2conf="$T2HOME/scripts/t2conf/t2conf"
alias t2dmon="$T2HOME/scripts/t2dmon"
alias t2fm="$T2HOME/scripts/t2fm/t2fm"
alias t2plot="$T2HOME/scripts/t2plot"
alias t2timeline="$T2HOME/scripts/t2timeline"
alias t2wizard="$T2HOME/scripts/t2wizard"
alias tawk="$T2HOME/scripts/tawk/tawk"

tcol() {
    local ubuntu
    if hash apt-get 2> /dev/null; then
        ubuntu="-n -e"
    fi

    if [ $# -eq 0 ]; then
        eval "column -t -s $'\t' $ubuntu | less -S"
    else
        eval "column -t -s $'\t' $ubuntu \"$@\" | less -S"
    fi
}

# ZSH already defines an lsx alias
if ! type lsx &> /dev/null; then
    lsx() {
        local WIDTH=40
        if [ $# -gt 0 ] && [ -z "$(echo "$1" | $SED 's/^[0-9]\+$//')" ]; then
            WIDTH=$1
            shift
        fi
        less -RSx${WIDTH} "$@"
    }
fi

t2stat() {
    local cmd
    if [ -n "$1" ]; then
        cmd="watch -n $1"
    fi
    local pid="$(pidof tranalyzer)"
    if [ -z "$pid" ]; then
        echo "No running instance of Tranalyzer found"
    else
        $cmd kill -USR1 $pid
    fi
}

t2edit() {
    local editor="${EDITOR:-vim}"
    if [ -z "$1" ] || [ ! -f "$T2HOME/$1" ]; then
        echo "Usage: t2edit <file>"
        return
    fi
    "$editor" "$T2HOME/$1"
}

t2doc() {
    local cmd="${OPEN}"
    : ${cmd:="$(which evince)"}
    if [ -z "$cmd" ]; then
        _printerr "No PDF viewer found"
        return
    fi
    if [ -z "$1" ]; then
        if [ ! -f "$T2HOME/doc/documentation.pdf" ]; then
            printf "Tranalyzer documentation does not exist... build it (Y/n)? "
            read ans
            case $ans in
                N|n) return
            esac
            make -C "$T2HOME/doc/" || {
                _printerr "Failed to build Tranalyzer documentation"
                return
            }
        fi
        $cmd "$T2HOME/doc/documentation.pdf" 2>/dev/null &
    elif [ "$1" = "tawk" ]; then
        $cmd "$T2HOME/scripts/$1/doc/$1.pdf" 2>/dev/null &
    elif [ ! -d "$T2HOME/$1" ]; then
        _printerr "Plugin '$1' not found"
    elif [ ! -f "$T2HOME/$1/doc/$1.pdf" ]; then
        printf "Documentation for plugin '$1' does not exist... build it (Y/n)? "
        read ans
        case $ans in
            N|n) return
        esac
        make -C "$T2HOME/$1/doc/" || {
            _printerr "Failed to build '$1' documentation"
            return
        }
        $cmd "$T2HOME/$1/doc/$1.pdf" 2>/dev/null &
    else
        $cmd "$T2HOME/$1/doc/$1.pdf" 2>/dev/null &
    fi
}

# fextractor
if [ -d "$T2HOME/findexer/fextractor" ]; then
    alias fextractor="$T2HOME/findexer/fextractor/fextractor"
fi

# Bash/ZSH completion
if [ $HAS_COMPLETION -eq 1 ]; then
    source "$T2HOME/scripts/tawk/completions/_tawk"
    for i in "$T2HOME/scripts/completions/"*; do
        source "$i"
    done
fi

# Cleanup
unset ans
unset b
unset deps
unset i
unset pgrmcmd
unset pgrmname
unset HAS_COMPLETION
unset READLINK

# Do not unset those variables: used in functions or completions
#unset AWK
#unset OPEN
#unset SED
