Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
setup-account.sh 3.98 KiB
Newer Older
#!/bin/bash
## Functions to setup BDAP user account for bash & git (only if pending).

set -e
function mute {
    "${@}" 1> /dev/null 2>&1
}

function upd_inputrc {
    if mute grep -v ':history-search-forward' ~/.inputrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.inputrc <<- "EOF"
$include /etc/inputrc

## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
"\C-x\C-r": re-read-init-file

EOF
        echo "UPDATED"
    fi
}

function upd_bashrc_git_completions {
    if mute grep "/usr/share/bash-completion/completions/git" ~/.bashrc ; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- "EOF"

## Enable git completions
source /usr/share/bash-completion/completions/git

EOF
        echo "UPDATED"
    fi
}

function upd_bashrc_history {
    if mute grep "PROMPT_COMMAND=\"history -a\"" ~/.bashrc ; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- "EOF"

####
## HISTORY CONTROL:
## (more options may lie above))
##
## Save all lines of a multiple-line command in the same history entry.
shopt -s cmdhist
## Save multi-line commands to the history with embedded newlines.
shopt -s lithist
## Save each and EVERY cmd to history
#  (not to loose it if terminal closed abruptly!)
PROMPT_COMMAND="history -a"
## Don't put duplicate lines or lines starting with space in the history.
## Ignore 1-char cmds
HISTIGNORE=?:?

EOF
        echo "UPDATED"
    fi
}

function upd_bashrc_ustore_vars {
    if mute grep "export USTORE=" ~/.bashrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- EOF

## BDAP Storage locations
export PSTORE="/eos/jeodpp/data/projects/LEGENT"
export USTORE="/eos/jeodpp/home/users/${USER}"
export UTRANS="/eos/jeodpp/home/users/${USER}/transfer"
export PTRANS="/eos/jeodpp/data/projects/LEGENT/transfer"

EOF
        echo "UPDATED"
    fi
}

function upd_bash_aliases {
    if mute grep 'alias lg=' ~/.bash_aliases; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bash_aliases <<- "EOF"

## GIT ALIASES
alias lg='git lg'
alias lgg='git lgg'
alias status='git status'
alias show='git show'
alias log='git log'

EOF
        echo "UPDATED"
    fi
}

function upd_git_user {
    if mute  git config --global --get user.name; then
        echo "${FUNCNAME[0]}.name: already ok"
    else
        echo "${FUNCNAME[0]}.name: updating..."
        read -e -p "  +--Enter your git author-name:" inp
        git config --global user.name "inp"
        echo "UPDATED"
    fi

    if mute  git config --global --get user.email; then
        echo "${FUNCNAME[0]}.email: already ok"
    else
        echo "${FUNCNAME[0]}.email: updating..."
        read -e -p "  +--Enter your git author-email:" inp
        git config --global user.email "inp"
        echo "UPDATED"
    fi
}

function upd_git_lg {
    if mute  git config --global --get alias.lg; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
        git config --global alias.lgg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"
        git config --global credential.helper store
        echo "UPDATED"
    fi
}

function upd_bashrc_conda_init {
    if mute  grep "	>>> conda initialize >>>" ~/.bashrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        conda init bash
    fi
}


if [ $# -eq 0 ]; then
    echo -e "SYNTAX:\n   $0 UPD_FN\nWHERE UPD_FN one of:"
    declare -F|grep -oE " +upd_.+"
else
    "${@}"
fi