#!/bin/bash ## Functions to setup BDAP user account for bash & git (only if pending). # # Call this script with the name of the action-function as arg[1]. set -e mydir=$(realpath "${0%/*}") cd "$mydir" conda_env="uds-scripts" ipython_startup_ustore_vars="$HOME/.ipython/profile_default/startup/50-ustor-vars.py" function _mute { "${@}" &> /dev/null } 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 # Effective only if never used conda before. echo "UPDATED" fi } function upd_ipython_ustore_vars { if [ -f "$ipython_startup_ustore_vars" ]; then echo "${FUNCNAME[0]}: already ok" else echo -n "${FUNCNAME[0]}: updating..." mkdir -p $(dirname "$ipython_startup_ustore_vars") cat > "$ipython_startup_ustore_vars" <<- EOF ## BDAP Storage locations PSTORE="/eos/jeodpp/data/projects/LEGENT" USTORE="/eos/jeodpp/home/users/${USER}" UTRANS="/eos/jeodpp/home/users/${USER}/transfer" PTRANS="/eos/jeodpp/data/projects/LEGENT/transfer" EOF echo "UPDATED" fi } ## This function must be the 1st to run after all `upd_xxx` actions. # The last command of this action, activating conda-env, # will fail if not re-logged in after `conda init bash`. function create_conda_env { if conda env list | _mute grep "$conda_env"; then echo "${FUNCNAME[0]}: already ok" else echo -n "${FUNCNAME[0]}: updating..." conda env create -f environment.yml ## Setup any conda-env variables: # - PYTHONPATH pointing to project-root, to run scripts from there. conda env config vars set -n $conda_env PYTHONPATH="$mydir/" echo "${FUNCNAME[0]}: echo ACTIVATE YOUR ENVIRONMENT with: conda activate $conda_env" fi } function _list_all_actions { declare -F|grep -oE ' [a-zA-Z]\w+$' } function all { for cmd in $(_list_all_actions); do if [ "$cmd" != "${FUNCNAME[0]}" ]; then $cmd fi done } if [[ $# -eq 1 && ( "$1" = "-h" || "$1" = "--help" ) ]]; then echo -e "SYNTAX:\n $0\n $0 ACTION\nWHERE:\n ACTION is one of:" _list_all_actions echo -e '\nIf run without any args, launches `all` actions, in order.' echo -e '\nNOTE that `upd_git_user` action awaits interactively input from the user' elif [[ $# -eq 0 ]]; then all else ## execute the asked action. "${@}" fi