# Bash Aliases etc.; -*-Shell-script-*-
# dest=~/.bash_aliases     # Keep this as the 2nd line for mmf_init_setup

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# This content inserted by mmf_setup
# Add my mercurial configurations.
eval "$(mmf_setup -v -H)"

function _vcs_message {
    echo "Please make sure that you set the following variables on your personal"
    echo "computer, and forward them when you ssh to the project. This can be done"
    echo "with your SSH config file (~/.ssh/config) which might look like this"
    echo "(or use SendEnv if you define them in your environment):"
    echo
    echo "    # ~/.ssh/config"
    echo "    Host cc-project1"
    echo "      User ff1cb986f..."
    echo 
    echo "    Host cc-*"
    echo "        HostName ssh.cocalc.com"
    echo "        ForwardAgent yes"
    echo "        SendEnv LC_HG_USERNAME=Your Full Name <your.email.address+hg@gmail.com>"
    echo "        SendEnv LC_GIT_USERNAME=Your Gull Name"
    echo "        SendEnv LC_GIT_USEREMAIL=your.email.address+git@gmail.com"
    echo "        SetEnv LC_EDITOR=vi"
    echo
    echo "For details, see https://github.com/forbes-group/mmf-setup#cocalc"
    echo
}

# Mercurial config
if [ -z "${LC_HG_USERNAME}" ]; then
    _hg = "$(type -p hg)"
    echo "You have not set LC_HG_USERNAME!"
    echo "DO NOT USE HG to commit until you set LC_HG_USERNAME"
    echo
    _vcs_message
    
    function hg {
        echo "You have not set LC_HG_USERNAME!"
        echo "DO NOT USE HG to commit until you set LC_HG_USERNAME"
        echo
        _hg $*
    }
fi

# Git config
export GIT_AUTHOR_NAME="${LC_GIT_USERNAME}"
export GIT_AUTHOR_EMAIL="${LC_GIT_USEREMAIL}"
export GIT_COMMITTER_NAME="${LC_GIT_USERNAME}"
export GIT_COMMITTER_EMAIL="${LC_GIT_USEREMAIL}"

# Some nice aliases like git lg
git config --global alias.lg1 $'log --graph --abbrev-commit --decorate --date=relative --format=format:\'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)\' --all'
git config --global alias.lg2 $'log --graph --abbrev-commit --decorate --format=format:\'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n\'\'          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)\' --all'
git config --global alias.lg $'!git lg1'

if [ -z "${LC_GIT_USERNAME}" ]; then
    _git = "$(type -p hg)"
    echo "You have not set LC_GIT_USERNAME!"
    echo "DO NOT USE GIT to commit until you set both LC_GIT_USERNAME and LC_GIT_USEREMAIL"
    echo
    _vcs_message
    function git {
        echo "You have not set LC_GIT_USERNAME!"
        echo "DO NOT USE GIT to commit until you set both LC_GIT_USERNAME and LC_GIT_USEREMAIL"
        echo
        _git $*
    }
fi

# Bash Command History
# https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps
# https://superuser.com/questions/232885
export INPUTRC=~/.inputrc
export HISTSIZE=5000           # Commands in memory.
export HISTFILESIZE=500000     # Should keep file <~5MB
export HISTIGNORE='&:ls:[bf]g:exit:pwd:rm *;'
export HISTCONTROL=ignoreboth:erasedups
shopt -s histappend            # Ensure we keep history

export SCREENDIR=~/.screen

# CoCalc defaults to joe: https://joe-editor.sourceforge.io/.  Here I
# allow the user to customize the editor by passing the LC_EDITOR
# environmental variable.  Add SetEnv LC_EDITOR=vi to your ~/.ssh/config file.
if [ -n "${LC_EDITOR}" ]; then
    export EDITOR="${LC_EDITOR}"
fi

# pip bash completion start
_pip_completion()
{
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip
# pip bash completion end

export PATH="$HOME/.poetry/bin:$PATH"

# Finding stuff
function finda {
    find . \(                                                                    \
	 -name ".hg" -o -name ".ipynb_checkpoints" -o -name "*.sage-*" \) -prune \
	 -o -type f -print0 | xargs -0 grep -H "${*:1}"
}

function findf {
    find . \(                                                                    \
	 -name ".hg" -o -name ".ipynb_checkpoints" -o -name "*.sage-*" \) -prune \
	 -o -type f -name "*.$1" -print0 | xargs -0 grep -H "${*:2}"
}

# I used to use aliases, but they cannot easily be overrriden by
# personalzed customizations.
function findpy { findf py "${*}"; }
function findipy { findf ipynb "${*}"; }
function findjs { findf js "${*}"; }
function findcss { findf css "${*}"; }
