2020-01-14 10:11:36 +01:00
|
|
|
#!/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
|
|
|
|
|
|
|
|
2020-11-30 08:11:15 +01: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
|
|
|
|
2020-01-14 10:11: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
|
2020-03-12 22:07:18 +01:00
|
|
|
#echo "interactive shell" >&2
|
2020-01-14 10:11:36 +01:00
|
|
|
|
|
|
|
# define variables
|
2020-09-22 09:08:16 +02:00
|
|
|
# Base Path
|
2020-12-10 09:31:38 +01:00
|
|
|
[ -z "${MSC_SUBPATH+x}" ] && export MSC_SUBPATH=".local/myshellconfig"
|
|
|
|
[ -z "${MSC_BASE+x}" ] && export MSC_BASE="${HOME}/${MSC_SUBPATH}"
|
2020-12-10 09:41:23 +01:00
|
|
|
|
2020-09-22 09:08:16 +02:00
|
|
|
# hardcoded paths
|
2020-12-10 09:31:38 +01:00
|
|
|
export MSC_BASE_PARENT="$(dirname $MSC_BASE)"
|
|
|
|
export MSC_BASH_COMPLETION="${HOME}/${MSC_SUBPATH}/bash_completion.d"
|
2020-12-23 12:51:04 +01:00
|
|
|
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"
|
2020-09-22 09:08:16 +02:00
|
|
|
|
2020-12-10 10:35:54 +01:00
|
|
|
# Load DEFAULTS
|
2020-12-10 16:32:49 +01:00
|
|
|
[ -e ${MSC_BASE}/defaults.conf ] && . ${MSC_BASE}/defaults.conf
|
2020-12-10 10:35:54 +01:00
|
|
|
|
|
|
|
|
2020-09-22 09:08:16 +02:00
|
|
|
# Userdata
|
2020-12-23 12:51:04 +01:00
|
|
|
[ -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)"
|
2020-09-22 09:08:16 +02:00
|
|
|
|
|
|
|
# GIT
|
2020-12-23 12:51:04 +01:00
|
|
|
[ -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
|
|
|
|
|
2020-12-23 13:16:32 +01:00
|
|
|
# [ -z "${MSC_GIT_TIMEOUT+x}" ] && export MSC_GIT_TIMEOUT=5s
|
2020-12-23 12:51:04 +01:00
|
|
|
[ -z "${MSC_GIT_CHECKOUT_TIMEOUT+x}" ] && export MSC_GIT_CHECKOUT_TIMEOUT=20s
|
|
|
|
[ -z "${MSC_GIT_SYNC+x}" ] && export MSC_GIT_SYNC=true
|
|
|
|
|
2020-09-22 09:08:16 +02:00
|
|
|
# hardcoded git
|
2020-12-10 09:31:38 +01:00
|
|
|
export SGIT="git -C ${MSC_BASE}"
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-09-21 13:00:29 +02:00
|
|
|
|
2020-09-22 09:08:16 +02:00
|
|
|
|
|
|
|
# git-repo-url for vim plugin
|
|
|
|
# TODO: check how to use
|
2020-12-10 09:31:38 +01:00
|
|
|
export MSC_VIM_PLUGINS
|
2020-09-21 13:00:29 +02:00
|
|
|
|
2020-09-22 09:08:16 +02:00
|
|
|
|
|
|
|
# Logging
|
2020-12-23 12:51:04 +01:00
|
|
|
[ -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}"
|
2020-09-22 09:08:16 +02:00
|
|
|
# Create necessary directories for logging
|
2020-12-23 12:51:04 +01:00
|
|
|
[ -n "${MSC_LOGDIR-x}" ] && mkdir -p "$MSC_LOGDIR"
|
2020-09-22 09:08:16 +02:00
|
|
|
# load logging functions
|
2020-12-10 09:31:38 +01:00
|
|
|
. ${MSC_BASE}/logging
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2021-05-06 00:56:01 +02:00
|
|
|
# P11M for etoken/smartcard authentification
|
2021-07-07 16:25:44 +02:00
|
|
|
|
|
|
|
# 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)"
|
|
|
|
|
2021-07-07 16:36:50 +02:00
|
|
|
[ -z "${PKCS11_MODULE:+x}" ] && [ -n "$(whereis p11-kit-proxy.so|awk '{print $2}')" ] && export PKCS11_MODULE=$(whereis p11-kit-proxy.so|awk '{print $2}' || { echo "p11-kit-proxy not installed, unset KCS11_MODULE"; unset PKCS11_MODULE; })
|
|
|
|
[ -z "${P11M:+x}" ] && [ -n "$(whereis p11-kit-proxy.so|awk '{print $2}')" ] && export P11M=$(whereis p11-kit-proxy.so|awk '{print $2}' || { echo "p11-kit-proxy not forwarded by ssh or installed, unset P11M"; unset P11M; })
|
2020-10-27 11:15:30 +01:00
|
|
|
|
2020-10-30 11:51:41 +01:00
|
|
|
# Basics
|
|
|
|
[ -z "${VISUAL+x}" ] && export VISUAL=vim
|
|
|
|
[ -z "${EDITOR+x}" ] && export EDITOR=vim
|
|
|
|
|
2020-12-10 09:31:38 +01:00
|
|
|
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
|
|
|
|
2020-09-22 00:31:18 +02:00
|
|
|
SCRIPTENTRY
|
2020-01-14 10:11:36 +01:00
|
|
|
# define functions
|
2020-12-10 08:57:30 +01:00
|
|
|
|
2020-01-14 10:11:36 +01:00
|
|
|
ckrepo () {
|
|
|
|
# check if remote repo is reachable
|
2020-12-23 13:31:05 +01:00
|
|
|
if ${MSC_GIT_SYNC-$MSC_GIT_REPO_UPDATE}; then
|
|
|
|
|
|
|
|
if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \
|
2020-12-23 13:27:31 +01:00
|
|
|
-lt $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ]; then
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) reached"
|
|
|
|
logwarning "check if reachable"
|
2022-10-13 16:20:26 +02:00
|
|
|
# git ls-remote
|
|
|
|
# if $( timeout --foreground "${MSC_GIT_TIMEOUT-$MSC_GIT_TIMEOUT_DEFAULT}" $SGIT ls-remote >/dev/null 2>&1) ;then
|
|
|
|
# git fetch --dry-run --verbose
|
|
|
|
if $( timeout --foreground "${MSC_GIT_TIMEOUT-$MSC_GIT_TIMEOUT_DEFAULT}" $SGIT fetch --dry-run >/dev/null 2>&1) ;then
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "reachable"
|
2020-12-23 13:31:05 +01:00
|
|
|
return 0
|
|
|
|
else
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "not reachable"
|
2020-12-23 13:31:05 +01:00
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
else
|
2020-12-23 14:13:18 +01:00
|
|
|
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") ))
|
2022-09-14 14:43:45 +02:00
|
|
|
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."
|
2020-12-23 13:31:05 +01:00
|
|
|
return 2
|
2020-02-25 12:12:05 +01:00
|
|
|
fi
|
2020-03-04 11:10:18 +01:00
|
|
|
else
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "no sync allowed from config" >&2
|
2020-12-23 13:31:05 +01:00
|
|
|
return 1
|
2020-01-14 10:11:36 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:59:03 +01:00
|
|
|
lastupdatesubmodules () {
|
2021-01-12 16:46:26 +01:00
|
|
|
touch ${MSC_GIT_UPD_SUBMOD_STATFILE}
|
2020-12-14 13:59:03 +01:00
|
|
|
}
|
|
|
|
export -f lastupdatesubmodules
|
|
|
|
|
|
|
|
lastupdaterepo () {
|
2020-12-23 12:51:04 +01:00
|
|
|
touch ${MSC_GIT_UPD_REPO_STATFILE}
|
2020-12-14 13:59:03 +01:00
|
|
|
}
|
|
|
|
export -f lastupdaterepo
|
|
|
|
|
2020-01-14 10:11:36 +01:00
|
|
|
sync_config () {
|
2020-12-10 09:31:38 +01:00
|
|
|
if $MSC_GIT_SYNC; then
|
2020-02-25 16:15:32 +01:00
|
|
|
local nok=""
|
|
|
|
local gco=""
|
|
|
|
if which git >/dev/null; then
|
2020-12-10 09:31:38 +01:00
|
|
|
loginfo -n "Sync config with ${MSC_GIT_SERVER}: "
|
|
|
|
# MSC_GITCHECKOUTSCRIPT_OPTIONS are options for bin/git-myshellconfig-checkout
|
2021-10-08 15:53:26 +02:00
|
|
|
${MSC_BASE}/bin/git-myshellconfig-checkout ${MSC_GIT_CHECKOUTSCRIPT_OPTIONS=${MSC_GIT_CHECKOUTSCRIPT_OPTIONS_DEFAULT}}|| nok="not " 1>>"${MSC_LOGFILE}" 2>&1
|
2020-11-14 00:03:26 +01:00
|
|
|
loginfo "$(printf '%s\n' "${nok}synced" )"
|
2020-12-10 09:31:38 +01:00
|
|
|
[ -z "$nok" ] && mkdir -pv "${MSC_LOGDIR}"
|
2020-02-25 16:15:32 +01:00
|
|
|
|
2020-01-14 10:11:36 +01:00
|
|
|
else
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "git not installed, no configuration possible, please install git" >&2
|
2020-01-14 10:11:36 +01:00
|
|
|
fi
|
|
|
|
else
|
2022-09-14 14:43:45 +02:00
|
|
|
logwarning "Not syncing profile in case of configuration"
|
2020-01-14 10:11:36 +01:00
|
|
|
fi
|
|
|
|
}
|
2020-10-22 08:48:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
pathmunge () {
|
|
|
|
ENTRY
|
|
|
|
case ":${PATH}:" in
|
|
|
|
*:"$1":*)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ "$2" = "after" ] ; then
|
|
|
|
PATH=$PATH:$1
|
|
|
|
else
|
|
|
|
PATH=$1:$PATH
|
|
|
|
fi
|
|
|
|
esac
|
|
|
|
EXIT
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:31:38 +01:00
|
|
|
if [ -d "${MSC_BASE}/bin" ] ; then
|
|
|
|
pathmunge "${MSC_BASE}/bin"
|
2020-10-22 08:48:47 +02:00
|
|
|
export PATH
|
|
|
|
fi
|
|
|
|
|
2020-11-15 22:19:57 +01:00
|
|
|
if [ -d "${HOME}/.local/bin" ] ; then
|
|
|
|
pathmunge "${HOME}/.local/bin"
|
|
|
|
export PATH
|
|
|
|
fi
|
|
|
|
|
2020-02-27 09:08:43 +01: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.
|
2020-09-21 21:14:18 +02:00
|
|
|
logdebug "Source bash-completion"
|
2020-02-27 09:08:43 +01:00
|
|
|
. /usr/share/bash-completion/bash_completion
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-02-05 12:18:12 +01:00
|
|
|
# source git-sync part of myshellconfig
|
2020-12-10 09:31:38 +01:00
|
|
|
. "${MSC_BASE}/myshell_git_sync"
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-02-05 12:18:12 +01:00
|
|
|
# source loading defaults part of myshellconfig
|
2020-12-10 09:31:38 +01:00
|
|
|
. "${MSC_BASE}/myshell_load_defaults"
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-09-16 21:42:56 +02:00
|
|
|
# source loading workaround for x2go to forward local etoken to remote machine with forwardagent
|
2020-12-10 09:31:38 +01:00
|
|
|
. "${MSC_BASE}/myshell_load_x2go_tokenforward"
|
2020-09-16 21:42:56 +02:00
|
|
|
|
2022-03-15 09:30:58 +01:00
|
|
|
_tmux_hook
|
|
|
|
_umask_hook
|
2020-11-28 18:00:17 +01:00
|
|
|
promptcommandmunge "_tmux_hook"
|
2020-11-28 17:42:42 +01:00
|
|
|
promptcommandmunge "_umask_hook"
|
|
|
|
which direnv >/dev/null && eval "$(direnv hook bash)"
|
2020-09-21 13:00:29 +02:00
|
|
|
|
2020-12-10 09:31:38 +01:00
|
|
|
cat << EOF >> "${MSC_LOGFILE}"
|
2020-11-30 09:43:27 +01:00
|
|
|
==8<=== finished configure new shell ================ $(date) ===============8<==
|
|
|
|
|
|
|
|
EOF
|
|
|
|
#else
|
|
|
|
# echo "non-interactive shell" >&2
|
|
|
|
SCRIPTEXIT
|
|
|
|
|
2020-11-30 08:11:15 +01:00
|
|
|
# Start tmux if not defined as STARTTMUX in bashrc
|
2021-02-07 21:26:14 +01:00
|
|
|
logdebug "start tmux ${STARTTMUXDEFAULT} - ${STARTTMUXREMOTEDEFAULT} - ${STARTTMUX-$STARTTMUXDEFAULT}"
|
2021-02-02 09:48:40 +01:00
|
|
|
if ${STARTTMUX-$STARTTMUXDEFAULT} ; then
|
2020-11-30 08:07:17 +01:00
|
|
|
cat << EOF
|
|
|
|
|
|
|
|
"User: $USER - $UID"
|
|
|
|
|
|
|
|
Starting or resuming screen session
|
|
|
|
Press CTRL+C to cancel screen startup
|
|
|
|
|
|
|
|
EOF
|
|
|
|
sleep 1
|
|
|
|
#screen -UxR
|
|
|
|
tmux attach-session
|
2020-11-30 09:43:27 +01:00
|
|
|
logdebug "started tmux"
|
2020-11-30 08:07:17 +01:00
|
|
|
fi
|
2020-11-30 09:43:27 +01:00
|
|
|
logdebug "after"
|
2020-01-22 10:20:53 +01:00
|
|
|
|
2020-02-05 07:36:58 +01:00
|
|
|
fi
|