From 655bb93698ccc74218924efcaa9063a32ecffb65 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 12 Mar 2019 05:16:49 +0000 Subject: [PATCH] etc/profile --- etc/profile | 113 +++++++++++++++++++++------------------- etc/profile.d/functions | 26 ++++----- 2 files changed, 74 insertions(+), 65 deletions(-) diff --git a/etc/profile b/etc/profile index 632ec942..c4f7e728 100755 --- a/etc/profile +++ b/etc/profile @@ -15,12 +15,19 @@ [ "$1" = "--nb-debug" ] && export NB_DEBUG="yes" && shift [ "$1" = "--nb-no-repos" ] && export NB_NO_REPOS="yes" && shift #[ "$NB_DEBUG" = "yes" ] || return 0 + nb_debug() { [ -n "$NB_DEBUG" -a "$NB_DEBUG" = "yes" ] || return 0 echo "DEBUG: $*" 1>&2 return 0 } +_nb_unset() { + unset i tmp NB_ETC_DIR NB_DEBUG NB_LOOP + unset -f _nb_unset _nb_file_has_tag 2> /dev/null + return 0 +} + _nb_file_has_tag() { [ "$(head -1 "$1")" = "# " ]; } # @@ -30,9 +37,10 @@ if [ -n "$NB_LOOP" ]; then nb_debug "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" echo "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0 | Won't load !" NB_LOOP=$(($NB_LOOP+1)) - unset NB_LOOP - exit - return 2>/dev/null || exit # if executable + _nb_unset; return 70 +# NB 12.03.19 unset NB_LOOP +# NB 12.03.19 exit +# NB 12.03.19 return 2>/dev/null || exit # if executable fi nb_debug "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" @@ -40,28 +48,33 @@ nb_debug "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" export NB_LOOP=1 # -# SEARCH ROOT +# Search NB_ETC_DIR # unset NB_ETC_DIR #[ "${BASH-no}" != "no" ] && echo Is BASH #echo BASH_SOURCE=$BASH_SOURCE ZSH_VERSION=$ZSH_VERSION KSH_VERSION=$KSH_VERSION + if [ -n "$BASH_SOURCE" ]; then + # Bash NB_ETC_DIR="${BASH_SOURCE%/*}" elif [ -n "$ZSH_VERSION" ]; then + # ZSH eval 'NB_ETC_DIR=${(%):-%N}' NB_ETC_DIR=${dir%/*} elif [ "$KSH_VERSION" != "" ]; then + # KSH #elif eval '[[ -n ${.sh.file} ]]' 2>/dev/null; then eval 'NB_ETC_DIR="$(dirname "${.sh.file}")"' elif [ -r /etc/profile.d/nb.sh ]; then + # Already installed # NB 18.02.19 NB_ETC_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh)) NB_ETC_DIR=$(dirname "$(awk -F'&& . ' '/ \. /{print $2;exit}' /etc/profile.d/nb.sh)") else - # Alpine, busybox, ... + # Others # We guess by looking for file !! tmp=$PWD for i in /*/*/etc/profile.d/nb.sh /*/*/*/etc/profile.d/nb.sh; do @@ -79,60 +92,54 @@ else fi -_nb_unset() { - unset i tmp NB_ETC_DIR NB_DEBUG NB_LOOP - unset -f _nb_file_has_tag 2> /dev/null - return 0 -} - +# +# Check directory +# if [ -z "$NB_ETC_DIR" -o "$NB_ETC_DIR" = "/" ]; then export NB_UNSUPORTED=1 nb_debug "ERR: Unsupported shell. Please use bash, ksh93 or zsh." + _nb_unset; return 71 -else - unset NB_UNSUPORTED - #echo "+$NB_ETC_DIR" - #echo $NB_ETC_DIR; #return - - # Load source file "functions" and avoid system crash ! - nb_debug ". $NB_ETC_DIR/profile.d/functions" - if [ ! -e "$NB_ETC_DIR/profile.d/functions" ] || ! . "$NB_ETC_DIR/profile.d/functions"; then - echo "ERR: $NB_ETC_DIR: Wrong directory (no profile.d/functions)" 1>&2 - _nb_unset - return 1 - fi - - export NB_ROOT=$(realpath $NB_ETC_DIR/.. 2>/dev/null) - nb_debug "NB_ROOT=$NB_ROOT" - - # - # Repo's Profiles - # +fi +unset NB_UNSUPORTED + +# +# Load sources +# + +# Load source file "functions" and avoid system crash ! +nb_debug ". $NB_ETC_DIR/profile.d/functions" +if [ ! -e "$NB_ETC_DIR/profile.d/functions" ] || ! . "$NB_ETC_DIR/profile.d/functions"; then + echo "ERR: $NB_ETC_DIR: Wrong directory (no profile.d/functions)" 1>&2 + _nb_unset; return 72 +fi + +export NB_ROOT=$(realpath $NB_ETC_DIR/.. 2>/dev/null) +nb_debug "NB_ROOT=$NB_ROOT" + +# +# Repo's Profiles +# # NB 09.01.18 $(true || nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | awk '!/^\/etc\// && $0 != "'${NB_ROOT}/etc/profile'"') \ - if [ -n "$NB_ROOT" ]; then - - alias which=which_cache - for i in \ - ${NB_ROOT}/etc/profile.d/envs \ - ${NB_ROOT}/etc/profile.d/aliases \ - ${NB_ROOT}/etc/profile.d/*.sh \ - $([ "$NB_NO_REPOS" = "yes" ] || nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | grep -Ev "^($NB_ROOT/|/etc/profile)") \ - ; do - case "$i" in - # no multiple include from other branch - */etc/profile) _nb_file_has_tag "$i" && nb_debug "IGNORE $i" && continue ;; - esac - nb_debug ". $i" - [ -r "$i" -a -f "$i" ] && . "$i" - done - unalias which - - fi +if [ -n "$NB_ROOT" ]; then + + alias which=which_cache + for i in \ + ${NB_ROOT}/etc/profile.d/envs \ + ${NB_ROOT}/etc/profile.d/aliases \ + ${NB_ROOT}/etc/profile.d/*.sh \ + $([ "$NB_NO_REPOS" = "yes" ] || nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | grep -Ev "^($NB_ROOT/|/etc/profile)") \ + ; do + case "$i" in + # no multiple include from other branch + */etc/profile) _nb_file_has_tag "$i" && nb_debug "IGNORE $i" && continue ;; + esac + nb_debug ". $i" + [ -r "$i" -a -f "$i" ] && . "$i" + done + unalias which fi #export NB_LAST_LOAD_TIME=$(date +"%Y-%M-%d %T") -_nb_unset -unset -f _nb_unset 2> /dev/null - -return 0 +_nb_unset; return 0 diff --git a/etc/profile.d/functions b/etc/profile.d/functions index e95eda6c..eb29ea78 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -21,14 +21,15 @@ which timeout > /dev/null || timeout() { fi } -which realpath2 > /dev/null || realpath2() { +which realpath > /dev/null || realpath() { + #perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" local pwd_bak=$PWD [ -e "$1" ] || return if [ -d "$1" ]; then cd "$1" || return - readlink "$PWD" || echo $PWD + pwd -P else local BASENAME="$(basename "$1")" @@ -40,10 +41,11 @@ which realpath2 > /dev/null || realpath2() { LINK=$(readlink "$BASENAME") done - if [ "$PWD" = "/" ]; then - echo "$PWD$BASENAME" + local DIR="$(pwd -P)" + if [ "$DIR" = "/" ]; then + echo "$DIR$BASENAME" else - echo "$PWD/$BASENAME" + echo "$DIR/$BASENAME" fi fi @@ -51,13 +53,13 @@ which realpath2 > /dev/null || realpath2() { cd "$pwd_bak" } -if ! which realpath > /dev/null; then - if which grealpath > /dev/null; then - realpath() { grealpath $@; } - else - realpath() { perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"; } - fi -fi +# NB 12.03.19 if ! which realpath > /dev/null; then +# NB 12.03.19 if which grealpath > /dev/null; then +# NB 12.03.19 realpath() { grealpath $@; } +# NB 12.03.19 else +# NB 12.03.19 realpath() { perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"; } +# NB 12.03.19 fi +# NB 12.03.19 fi which seq > /dev/null || seq() { local begin=1 -- 2.47.3