myshellconfig/bashrc_add

238 lines
9.4 KiB
Text
Raw Normal View History

#!/bin/bash
#debug
2020-09-21 18:02:07 +02:00
#failure() {
# local lineno=$1
# local msg=$2
# echo "Failed at $lineno: $msg"
#}
#trap 'failure ${LINENO} "$BASH_COMMAND"' ERR
2020-09-21 17:03:48 +02:00
# set some variables to defaults, if not set in .bashrc
2020-09-22 09:08:16 +02:00
[ -z ${SSHS+x} ] && export SSHS=false
2020-11-30 08:03:36 +01:00
# check if we are a interactive shell
2020-01-15 18:09:34 +01:00
# https://guide.bash.academy/expansions/
# https://www.tldp.org/LDP/abs/html/refcards.html#AEN22473
if [[ $- = *i* ]] ; then
2023-05-02 08:37:19 +02:00
if [ "${bashrctmp:-x}" == "x" ]
2023-05-02 08:18:49 +02:00
then
#echo "interactive shell" >&2
# define variables
# Base Path
[ -z "${MSC_SUBPATH+x}" ] && export MSC_SUBPATH=".local/myshellconfig"
[ -z "${MSC_BASE+x}" ] && export MSC_BASE="${HOME}/${MSC_SUBPATH}"
# hardcoded paths
export MSC_BASE_PARENT="$(dirname $MSC_BASE)"
export MSC_BASH_COMPLETION="${HOME}/${MSC_SUBPATH}/bash_completion.d"
export MSC_GIT_UPD_SUBMOD_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules"
export MSC_GIT_UPD_REPO_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_repo"
# Load DEFAULTS
[ -e ${MSC_BASE}/defaults.conf ] && . ${MSC_BASE}/defaults.conf
# Userdata
[ -z "${USERNAME+x}" ] && export USERNAME="$USER"
[ -z "${USEREMAIL+x}" ] && export USEREMAIL="$USER@$(domainname -d)"
[ -z "${FULLNAME+x}" ] && export FULLNAME="$(getent passwd $USER | cut -d ":" -f 5 | cut -d ',' -f 1)"
# GIT
[ -z "${GIT_AUTHOR_NAME+x}" ] && export GIT_AUTHOR_NAME=$FULLNAME
[ -z "${GIT_AUTHOR_EMAIL+x}" ] && export GIT_AUTHOR_EMAIL=$USEREMAIL
[ -z "${GIT_COMMITTER_NAME+x}" ] && export GIT_COMMITTER_NAME=$FULLNAME
[ -z "${GIT_COMMITTER_EMAIL+x}" ] && export GIT_COMMITTER_EMAIL=$USEREMAIL
# [ -z "${MSC_GIT_TIMEOUT+x}" ] && export MSC_GIT_TIMEOUT=5s
[ -z "${MSC_GIT_CHECKOUT_TIMEOUT+x}" ] && export MSC_GIT_CHECKOUT_TIMEOUT=20s
[ -z "${MSC_GIT_SYNC+x}" ] && export MSC_GIT_SYNC=true
# hardcoded git
export MSC_GIT="git -C ${MSC_BASE}"
# git-repo-url for vim plugin
# TODO: check how to use
export MSC_VIM_PLUGINS
# Logging
[ -z "${MSC_LOGDIR+x}" ] && export MSC_LOGDIR="${HOME}/logs"
[ -z "${MSC_LOGFILE+x}" ] && export MSC_LOGFILE="${MSC_LOGDIR}/myshellconfig.log"
[ -z "${SCRIPT_LOG+x}" ] && export SCRIPT_LOG="${MSC_LOGFILE}"
# Create necessary directories for logging
[ -n "${MSC_LOGDIR-x}" ] && mkdir -p "$MSC_LOGDIR"
# load logging functions
. ${MSC_BASE}/logging
# P11M for etoken/smartcard authentification
# Use PKCS11_MODULE or P11M (comes from ssh, when connected from remote) only in context of startagent/loadagent
# which dnf >/dev/null 2>&1 [ -z "${P11M+x}" ] && P11M="$(dnf repoquery --installed -l p11-kit | grep \/p11-kit-proxy.so\$ | head -1)"
# which dpkg >/dev/null 2>&1 && [ -z "${P11M+x}" ] && P11M="$(dpkg -L p11-kit-modules|grep \/p11-kit-proxy.so\$ | head -1)"
# Basics
[ -z "${VISUAL+x}" ] && export VISUAL=${VISUAL_DEFAULT}
[ -z "${EDITOR+x}" ] && export EDITOR=${EDITOR_DEFAULT}
cat << EOF >> "${MSC_LOGFILE}"
2020-10-22 08:56:07 +02:00
==8<=== start configure new shell ============== $(date) ===============8<==
2020-01-22 10:20:53 +01:00
EOF
2020-09-22 02:01:30 +02:00
2023-05-02 08:18:49 +02:00
SCRIPTENTRY
# define functions
ckrepo () {
# check if remote repo is reachable
if ${MSC_GIT_SYNC-$MSC_GIT_REPO_UPDATE}; then
if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \
-lt $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ]; then
logwarning "sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) reached"
loginfo "check if remote »$($MSC_GIT rev-parse --abbrev-ref @{u} 2>/dev/null | cut -d'/' -f1)« is reachable"
if $MSC_GIT fetch --dry-run >/dev/null 2>&1 ;then
loginfo "reachable"
return 0
else
logwarning "$($MSC_GIT rev-parse --abbrev-ref @{u} 2>/dev/null | cut -d'/' -f1) not reachable"
return 3
fi
else
local seconds=$(( $( date --date="@$(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 )" "+%s") - $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ))
logwarning "repo sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) not reached yet. $((seconds/86400)) days $(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds") left."
return 2
2020-12-23 13:31:05 +01:00
fi
else
2023-05-02 08:18:49 +02:00
logwarning "no sync allowed from config" >&2
return 1
2020-02-25 12:12:05 +01:00
fi
2023-05-02 08:18:49 +02:00
}
lastupdatesubmodules () {
touch ${MSC_GIT_UPD_SUBMOD_STATFILE}
}
export -f lastupdatesubmodules
lastupdaterepo () {
touch ${MSC_GIT_UPD_REPO_STATFILE}
}
export -f lastupdaterepo
sync_config () {
if $MSC_GIT_SYNC; then
local nok=""
local gco=""
if which git >/dev/null; then
loginfo -n "Sync config with ${MSC_GIT_SERVER}: "
# MSC_GITCHECKOUTSCRIPT_OPTIONS are options for bin/git-myshellconfig-checkout
${MSC_BASE}/bin/git-myshellconfig-checkout ${MSC_GIT_CHECKOUTSCRIPT_OPTIONS=${MSC_GIT_CHECKOUTSCRIPT_OPTIONS_DEFAULT}}|| nok="not " 1>>"${MSC_LOGFILE}" 2>&1
loginfo "$(printf '%s\n' "${nok}synced" )"
[ -z "$nok" ] && mkdir -pv "${MSC_LOGDIR}"
2023-05-02 08:18:49 +02:00
else
logwarning "git not installed, no configuration possible, please install git" >&2
fi
else
2023-05-02 08:18:49 +02:00
logwarning "Not syncing profile in case of configuration"
fi
2023-05-02 08:18:49 +02:00
}
pathmunge () {
ENTRY
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
EXIT
}
if [ -d "${MSC_BASE}/bin" ] ; then
pathmunge "${MSC_BASE}/bin"
export PATH
fi
2023-05-02 08:18:49 +02:00
if [ -d "${HOME}/.local/bin" ] ; then
pathmunge "${HOME}/.local/bin"
export PATH
fi
2023-05-02 08:18:49 +02:00
# Check for interactive bash and that we haven't already been sourced.
if [ -n "${BASH_VERSION-}" -a -n "${PS1-}" -a -z "${BASH_COMPLETION_VERSINFO-}" ]; then
# Check for recent enough version of bash.
if [ ${BASH_VERSINFO[0]} -gt 4 ] || \
[ ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 ]; then
[ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \
. "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion"
if shopt -q progcomp && [ -r /usr/share/bash-completion/bash_completion ]; then
# Source completion code.
logdebug "Source bash-completion"
. /usr/share/bash-completion/bash_completion
fi
2020-02-27 09:08:43 +01:00
fi
2023-05-02 08:18:49 +02:00
fi
2023-05-02 08:18:49 +02:00
# source git-sync part of myshellconfig
. "${MSC_BASE}/myshell_git_sync"
2023-05-02 08:18:49 +02:00
# source loading defaults part of myshellconfig
. "${MSC_BASE}/myshell_load_defaults"
2023-05-02 08:18:49 +02:00
# source loading workaround for x2go to forward local etoken to remote machine with forwardagent
. "${MSC_BASE}/myshell_load_x2go_tokenforward"
2023-05-02 08:18:49 +02:00
_tmux_hook
_umask_hook
#_pkcs11module_hook
2024-01-10 21:22:12 +01:00
[[ $(type -t _umask_hook) == function ]] && promptcommandmunge "_umask_hook"
[[ $(type -t _tmux_hook) == function ]] && promptcommandmunge "_tmux_hook"
#[[ $(type -t _pkcs11module_hook) == function ]] && promptcommandmunge "_pkcs11module_hook"
[[ $(type -t _bashrc_tmp_hook) == function ]] && promptcommandmunge "_bashrc_tmp_hook"
[[ $(type -t _remote_ssh_agent_hook) == function ]] && promptcommandmunge "_remote_ssh_agent_hook"
#[[ $(type -t _ps1_hook) == function ]] && promptcommandmunge "_ps1_hook"
2023-05-02 08:18:49 +02:00
which direnv >/dev/null && eval "$(direnv hook bash)"
cat << EOF >> "${MSC_LOGFILE}"
==8<=== finished configure new shell ================ $(date) ===============8<==
EOF
2023-05-02 08:18:49 +02:00
SCRIPTEXIT
2023-05-02 08:18:49 +02:00
# Start tmux if not defined as STARTTMUX in bashrc
logdebug "start tmux ${STARTTMUXDEFAULT} - ${STARTTMUXREMOTEDEFAULT} - ${STARTTMUX-$STARTTMUXDEFAULT}"
if ${STARTTMUX-$STARTTMUXDEFAULT} ; then
cat << EOF
2023-01-20 09:54:35 +01:00
"User: $USER - $UID - $SSH_CONNECTION"
Starting or resuming screen session
Press CTRL+C to cancel screen startup
EOF
2023-05-02 08:18:49 +02:00
sleep 1
#screen -UxR
tmux attach-session
logdebug "started tmux"
fi
logdebug "after"
else
2023-05-02 08:37:19 +02:00
[ -e "${bashrctmp}" ] && exec bash --rcfile "${bashrctmp}"
fi
2020-01-22 10:20:53 +01:00
fi