]> git.nbdom.net Git - nb.git/commitdiff
etc/profile.d/functions
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 11 Mar 2019 06:31:59 +0000 (06:31 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 11 Mar 2019 06:31:59 +0000 (06:31 +0000)
etc/profile.d/functions

index 82c05f4933d8d9785e336bc271e83e282e820584..e95eda6c1dfb3a79c763b09fd09d9a469dad7e1d 100755 (executable)
@@ -21,25 +21,43 @@ which timeout > /dev/null || timeout() {
        fi
 }
 
-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"
-       return
-       case "$OSTYPE" in
-               darwin*) [ -h "$1" ] && readlink "$1" || echo "$1";;
-               *) readlink -f "$1" ;;
-       esac
+which realpath2 > /dev/null || realpath2() {
+  local pwd_bak=$PWD
+
+  [ -e "$1" ] || return
+
+  if [ -d "$1" ]; then
+       cd "$1" || return
+       readlink "$PWD" || echo $PWD
+       else
+
+       local BASENAME="$(basename "$1")"
+       local LINK=$(readlink "$BASENAME")
+       cd "$(dirname "$1")"
+
+       while [ "$LINK" ]; do
+       cd "$(dirname "$LINK")"
+       LINK=$(readlink "$BASENAME")
+       done
+
+       if [ "$PWD" = "/" ]; then
+               echo "$PWD$BASENAME"
+       else
+               echo "$PWD/$BASENAME"
+       fi
+
+  fi
+
+  cd "$pwd_bak"
 }
 
-# NB 11.03.19 which realpath > /dev/null || realpath() {
-# NB 11.03.19  if which perl 1>/dev/null; then
-# NB 11.03.19          perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"
-# NB 11.03.19  else
-# NB 11.03.19          case "$OSTYPE" in
-# NB 11.03.19                  darwin*) readlink "$1" ;;
-# NB 11.03.19                  *) readlink -f "$1" ;;
-# NB 11.03.19          esac
-# NB 11.03.19  fi
-# NB 11.03.19 }
+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
 
 which seq > /dev/null || seq() {
        local begin=1
@@ -61,30 +79,6 @@ which seq > /dev/null || seq() {
  awk -v begin=$begin -v end=$end -v step=$step 'BEGIN{for(i=begin;i<=end;i=i+step) {print i}}'
 }
 
-nb_repos2() {
-       (
-               # Always !
-               echo "$NB_ROOT"
-
-               # Conf file
-               local conf="/etc/nb/repos.conf"
-               [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf"
-
-               # Realpath
-               eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null | while read dir; do
-
-                       # Accept directories or files (ex: use to tag a directory: .nb-install)
-                       [ -f "$dir" ] && dir=$(dirname "$dir")
-
-                       dir=$(realpath2 "$dir")
-                       echo "$dir"
-
-               done
-
-       # no doubles
-       # strong if awk failed
-       ) | awk \!'x[$0]++' || echo "$NB_ROOT"
-}
 nb_repos() {
        [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox
        (