# -*- sh -*-  Personal bash settings
# vim: ft=sh
# Put this at the _end_ of your .bashrc or .bash_profile file
# # Source personal definitions
# if [ -f ~/.bash_personal ]; then
#         . ~/.bash_personal
# fi

############################################################################
# system specific settings
UNAME=`uname`
if [ "$UNAME" == "Linux" ]; then
    . ~/.bash_linux
elif [[ "$UNAME" == "Darwin" ]]; then
    . ~/.bash_mac
fi

# pyenv configuration
if type -t pyenv > /dev/null; then
    export PYENV_ROOT="$HOME/.pyenv"
    [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
fi

# Default Python
PYTHON_EXE=${PYTHON_EXE:-python3}

# Find Python user bin directory.
# Only expecting this to work on Unices (maybe including Cygwin)
if [ -z "$PY_USER_BIN" ]; then  # Allow preset in earlier files.
    export PY_USER_BIN=$($PYTHON_EXE -c 'import os,sysconfig;print(sysconfig.get_path("scripts",f"{os.name}_user"))')
fi

# Pager should be defined for mercurial
export PAGER=less
# Less needs to interpret 'raw' output apparently.  Otherwise ipython displays
# the strings mangled with ESCs
export LESS=-R

# some more ls aliases
alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'

# for virtualenvs
alias cdve='pushd ${VIRTUAL_ENV}'

# remote gvimming
alias gvr='$GUI_EDITOR --remote-silent'

# Build local wheels
alias mywhy='pip wheel --wheel-dir=$HOME/wheelhouse'

# Pip user install
alias pipu="pip install --user"

# Show numpy version
alias npv="$PYTHON_EXE -c 'import numpy; print(numpy.__version__)'"

# Move my pip config out of the way
alias unpip='mv $HOME/.pip/pip.conf $HOME/.pip/pip.conf.bak'
alias depip=unpip
alias repip='mv $HOME/.pip/pip.conf.bak $HOME/.pip/pip.conf'

# Move my nose config out of the way
alias unnose='mv $HOME/.noserc $HOME/.noserc.bak'
alias denose=unnose
alias renose='mv $HOME/.noserc.bak $HOME/.noserc'

# Delete, make a temporary virtualenv
alias test2ve='rmvirtualenv test2; mkvirtualenv --python=`which python2` test2'
alias test3ve='rmvirtualenv test3; mkvirtualenv --python=`which python3` test3'
alias testve='rmvirtualenv test; mkvirtualenv --python=`which python3` test'

# Remove .orig files
alias rmorig='find . -name "*.orig" -exec rm {} \;'
alias rmipynb='find . -name "*.ipynb" -exec rm {} \;'

# tmux
alias tma="tmux new -A -s 0"

# clockify-cli
alias clklin="clockify-cli clone last -i=0"
alias clkout="clockify-cli out"

# Finding downloads
alias recentd="ls -al1t $HOME/Downloads | head"

# gopass
GOPASS_PATH=$(gopass config mounts.path 2> /dev/null)
alias cdgopass="cd $GOPASS_PATH"
# Update git repo
alias pgopass="(cd $GOPASS_PATH && git pull --rebase )"

# Validate example key
alias gop="gopass go"
alias gos="gopass show -c"
alias gog="gopass generate -c -s"
alias goe="gopass edit"
alias goc="gopass edit -c"

# Add usr bins to PATH
PATH=$HOME/usr/bin:$HOME/usr/local/bin:$HOME/usr/local/sbin:$PATH

# Initialize $PATH with sbin locations so I can find system tools
PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH

# Add the standard Python --user executable path
PATH=$PY_USER_BIN:$PATH:$HOME/.local/bin

if [ -z "$START_SKIP_GEMS" ]; then
    # Add standard --user-install gem path
    command -v gem >& /dev/null && {
        gem_user_path=`gem environment paths | awk -F ":" '{print $1}'`
        if [ -n "$gem_user_path" ]; then
            PATH=${gem_user_path}/bin:$PATH
        fi
        }
fi

# Search paths for LaTeX (Dont' forget the final colons.  The null entry `::'
# denotes `default system directories' -- try finding that in the
# documentation.)  Note that these *must* go under ~/texmf, because that
# particular path is hardcoded in LaTeX and is not overridable by the user.
# While one could keep ~/texmf for default package installs and use other
# locations for {tex/bib/bst}inputs, I prefer to centralize all Tex stuff in
# one place.  Since I can't do it in ~/usr/tex, then I'll just keep everything
# TeX related in ~/texmf
export TEXINPUTS=.:$HOME/texmf/texinputs::
export BIBINPUTS=.:$HOME/texmf/bibinputs::
export BSTINPUTS=.:$HOME/texmf/bstinputs::

############################################################################

export MATLABPATH=$HOME/matlab:$HOME/matlab/m2html

# git completion
if [ -z "$START_SKIP_GITC" ]; then
    . $HOME/.git-completion
    PS1='[\u@\h \w$(__git_ps1 " (%s)")]\$ '
fi

# install virtualenvwrapper functions if available
export WORKON_HOME=$HOME/.virtualenvs
_VENV_WRAPPER=$(type -pf virtualenvwrapper_lazy.sh)
if [ -n "$_VENV_WRAPPER" ]; then
    source ${_VENV_WRAPPER}
fi

# pushd to directory for python package
function pushdp {
   pushd `python -c "import os; import $1; print(os.path.dirname($1.__file__))"`
}

# vi bash command line editing
# http://www.jukie.net/bart/blog/20040326082602
set -o vi
set editing-mode vi
set keymap vi
bind -m vi-insert "\C-p":previous-history
bind -m vi-insert "\C-n":next-history
bind -m vi-insert "\C-a":beginning-of-line
bind -m vi-insert "\C-e":end-of-line
bind -m vi-insert "\C-k":kill-line

# Go config
if type -t go > /dev/null; then
    PATH="$PATH:$(go env GOPATH)/bin"
fi


# Always export PATH
export PATH

# local::lib setup
# See: http://search.cpan.org/~haarg/local-lib-2.000019/lib/local/lib.pm
# Install by downloading and unpacking archive from that link and
#     perl Makefile.PL --bootstrap
#
# Use with: e.g.:
#   perl -MCPAN -Mlocal::lib -e 'CPAN::install(Math::Matrix)'
if [ -z "$START_SKIP_PERLP" ]; then
    perl_local_lib="perl -I$HOME/perl5/lib/perl5 -Mlocal::lib"
    "$perl_local_lib" >& /dev/null && {
        eval "$($perl_local_lib)"
        }
fi

# Make GPG sign when using Git
# https://stackoverflow.com/a/54809060
export GPG_TTY=$(tty)
