#!/usr/bin/zsh

# Easier navigation: .., ..., ...., ~
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias ~="cd ~"

alias tailf="tail -f"
# ls color
# Very long line
export LS_COLORS='no=00:fi=00:di=01;31:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
# GNU `ls`
colorflag="--color"


# List all files colorized in long format
alias l="ls -lF ${colorflag}"

# List all files colorized in long format, including dot files
alias la="ls -laF ${colorflag}"

# Always enable colored `grep` output
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

# Reload the shell (i.e. invoke as a login shell)
alias reload="exec ${SHELL} -l"

# Print each PATH entry on a separate line
alias path='echo -e ${PATH//:/\\n}'

# Lazy pip with -H option
alias piph='sudo -H pip3'

# Archives
function extract {
    if [ -z "$1" ]; then
        echo "Usage: extract <path_to_file>.<zip|rar|bz2|gz|tar|tbz2|tgz|7z|tar.bz2|tar.gz|tar.xz>"
    else
        if [ -f $1 ]; then
            case $1 in
                *.tar.bz2)  tar xvjf $1     ;;
                *.tar.gz)   tar xvzf $1     ;;
                *.tar.xz)   tar xvJf $1     ;;
                *.rar)      unrar x -ad $1  ;;
                *.bz2)      bunzip2 $1      ;;
                *.tar)      tar xvf $1      ;;
                *.gz)       tar xvjf $1     ;;
                *.zip)      unzip $1        ;;
                *.tbz2)     tar xvjf $1     ;;
                *.7z)       7z x $1         ;;
                *)          echo "extract: '$1' - unknow/unsupported archive method"
            esac
        else
            echo "$1 - file does not exist"
        fi
    fi
}

function compress {
    if [ "$#" -lt 2 ]; then
        echo "Usage: compress <file_name>.<zip|tar|tar.gz|tar.xz|rar|gzip> file1 file2 ..."
    else
        case $1 in
            *.zip)      zip $1 "${@:1}"       ;;
            *.tar)      tar cvf $1 "${@:2}"   ;;
            *.tar.gz)   tar zcvf $1 "${@:2}"  ;;
            *.tar.xz)   tar Jcvf $1 "${@:2}"  ;;
            *.rar)      rar a $1 "${@:2}"     ;;
            *.gzip)     gzip "${@:2}"         ;;
            *.bzip2)    bzip2 "${@:2}"        ;;
        esac
    fi
}

function extract_and_remove {
    extract $1
    rm -f $1
}

alias extr="extract "
alias extrr="extract_and_remove "

# Wget
alias wgetncc='wget --no-check-certificate'
alias wgetc='wget `getcb`'

function wget_archive_and_extract {
    URL=$1
    FILENAME=${URL##*/}
    wget $URL -O $FILENAME
    extract $FILENAME
    rm -i $FILENAME
}

alias wgetae="wget_archive_and_extract"

# Linux Ubuntu apt
alias aptuu="sudo apt update ; sudo apt upgrade -y"
alias aptu="sudo apt update"
alias aptar="sudo apt autoremove -y"
alias aptac="sudo apt autoclean -y"
function wttr {
    local location=$1
    curl wttr.in/~$location
}

# Remove none tag Docker images
alias drmi='docker rmi $(docker images -f "dangling=true" -q)'

function rmpyc {
    find $1 -name \*.pyc -delete
}

# Neovim
alias vim="nvim"

# Git review
function grw () {
	if [[ "$#" = 0 ]]
	then
		git review
	elif [[ "$#" = 1 ]]
	then
		git review -d "$1"
	else
		echo "Unknow args!"
	fi
}

function gclone() {
    # cd to git dir after clone
    local yes_cd=true
    while getopts "d:" OPTION
    do
        case $OPTION in
            d)
                local yes_cd=false
                shift
                ;;
        esac
    done
    if [[ "$1" =~ "openstack"  ]]; then
        # Clone and run git review -s
        git clone git://git.opendev.org/$1/$2.git $HOME/Workspace/$1/$2
        cd $HOME/Workspace/$1/$2
        git remote add gerrit ssh://kiennt@review.opendev.org:29418/$1/$2.git
        git review -s
        if $yes_cd; then echo "cd to $HOME/Workspace/$1/$2/"; else cd -; fi
    else
        if [[ $# -eq 1 ]]; then
            git clone $1
            repo_url=$1
            repo_name=${repo_url##*/}
            if $yes_cd; then cd $repo_name; fi
        elif [[ -z $2 ]]; then
            local repo_name=$1
            while [ "${repo_name%%/}" != "$repo_name" ]; do
                repo_name=${repo_name#*/}
            done
            repo_name=${repo_name%.*}
            git clone $1
            if $yes_cd; then cd $repo_name; fi
        elif [[ $# -eq 4 ]]; then
            if [[ "$1" == "ssh" ]]; then
                git clone git@github.com:$2/$3.git $4
            elif [[ "$1" == "https" ]]; then
                git clone https://github.com/$2/$3.git $4
            else
                echo "Usage: gclone [ssh|https] [username] [repo_name] [dir]"
            fi
            if $yes_cd; then cd $4; fi
        elif [[ $# -eq 3 ]]; then
            if [[ "$1" == "ssh" ]]; then
                git clone git@github.com:$2/$3.git
            elif [[ "$1" == "https" ]]; then
                git clone https://github.com/$2/$3.git
            else
                git clone git@github.com:$1/$2.git $3
            fi
            if $yes_cd; then cd $3; fi
        else
            git clone https://github.com/$1/$2.git
            if $yes_cd; then cd $2; fi
        fi
    fi
}

# Usage:
# git-sync-fork <upstream-remote> <target-remote>
# <upstream-remote> - default upstream
# <target-remote> - default origin
function git-sync-fork() {
    UPSTREAM=$1
    ORIGIN=$2
    if [[ -z "$UPSTREAM" ]]; then
        UPSTREAM="upstream"
    fi

    if [[ -z "$ORIGIN" ]]; then
        ORIGIN="origin"
    fi

    unset REPLY

    if [[ -n "$ZSH_VERSION" ]]; then
        vared -p "1. This will setup '$ORIGIN' to track all branches in '$UPSTREAM' - Are you sure ?" -c REPLY
    elif [[ -n "$BASH_VERSION" ]]; then
        read -p "1. This will setup '$ORIGIN' to track all branches in '$UPSTREAM' - Are you sure ?" -n 1 -r
    else
        echo "Unknow shell, only Zsh and Bash are supported"
        exit 1
    fi

    if [[ $REPLY =~ ^[Yy]$ ]]
    then
        echo "\n"
        for brname in `git branch -r | grep "$UPSTREAM" | grep -v master | grep -v HEAD | sed -e 's/.*\///g'`; do git branch --track $brname  $UPSTREAM/$brname ; done
        unset REPLY
    fi

    if [[ -n "$ZSH_VERSION" ]]; then
        vared -p "2. This will push all local branches and tags into '$ORIGIN' - Are you sure ?" -c REPLY
    elif [[ -n "$BASH_VERSION" ]]; then
        read -p "2. This will push all local branches and tags into '$ORIGIN' - Are you sure ?" -n 1 -r
    else
        echo "Unknow shell, only Zsh and Bash are supported"
        exit 1
    fi

    if [[ $REPLY =~ ^[Yy]$ ]]
    then
        echo "\n"
        git push --all $ORIGIN
        git push --tags $ORIGIN
        unset REPLY
    fi
}

# Prezto upgrade
function upgrade_prezto() {
    cd $ZPREZTODIR
    # Sync your fork
    # Requirement: Configure a remote for a fork
    # Follow https://help.github.com/articles/configuring-a-remote-for-a-fork/
    git stash
    git pull
    git submodule update --init --recursive
    git remote -v
    git ls-remote --heads upstream master &> /dev/null | wc -l
    if [[ "$?" == 0 ]]; then
        git remote add upstream https://github.com/sorin-ionescu/prezto.git 
    fi
    git fetch upstream
    git checkout master
    git merge upstream/master
    git push origin master
    git stash pop
}

#   Daniel Crisman's ANSI color chart script from
#   The Bash Prompt HOWTO: 6.1. Colours
#   http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
#
#   This function echoes a bunch of color codes to the 
#   terminal to demonstrate what's available.  Each 
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a 
#   test use of that color on all nine background 
#   colors (default + 8 escapes).
function termcol() {
    T='✖✖✖'   # The text for the color test

    echo -e "\n         def     40m     41m     42m     43m     44m     45m     46m     47m";

    for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
               '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
               '  36m' '1;36m' '  37m' '1;37m';

      do FG=${FGs// /}
      echo -en " $FGs \033[$FG  $T  "

      for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
        do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
      done
      echo;
    done
    echo
}
