diff --git a/pkgs/additional/sane-scripts/default.nix b/pkgs/additional/sane-scripts/default.nix index 7fa5036a..ab4107be 100644 --- a/pkgs/additional/sane-scripts/default.nix +++ b/pkgs/additional/sane-scripts/default.nix @@ -196,7 +196,7 @@ let which = static-nix-shell.mkBash { pname = "sane-which"; srcRoot = ./src; - pkgs = [ "coreutils-full" "file" ]; + pkgs = [ "coreutils-full" "file" "gnugrep" ]; }; wipe = static-nix-shell.mkBash { pname = "sane-wipe"; diff --git a/pkgs/additional/sane-scripts/src/sane-which b/pkgs/additional/sane-scripts/src/sane-which index 4f016ecb..ea5c5b17 100755 --- a/pkgs/additional/sane-scripts/src/sane-which +++ b/pkgs/additional/sane-scripts/src/sane-which @@ -1,25 +1,47 @@ #!/usr/bin/env nix-shell -#!nix-shell -i bash -p coreutils-full -p file +#!nix-shell -i bash -p coreutils-full -p file -p gnugrep # traces a PATH lookup by printing the source, resolution, and any symlinks traversed # finally, prints the content of the file -echo $1 -v=$(which $1) -# this probably doesn't handle paths with spaces -while [ "$(readlink $v || echo $v)" != "$v" ] -do - # TODO: this doesn't handle relative symlinks - echo '->' "$v" - v=$(readlink "$v") +cur="$1" +next="$(which "$cur")" + +getSymlinked() { + # test if $cur is a symlink + # TODO: handle relative symlinks too! + local next_ + next_="$(readlink "$cur")" + [ "$?" -eq 0 ] && echo "$next_" +} + +getWrapped() { + # test if $cur is a wrapper around a patch matching $1 template + local dir="$(dirname "$cur")" + local base="$(basename "$cur")" + local wrapped="$(printf "$1" "$dir" "$base")" + [ -e "$wrapped" ] && grep -q "$wrapped" "$cur" && echo "$wrapped" +} + +getNext() { + getSymlinked \ + || getWrapped "%s/.sandboxed/%s" \ + || getWrapped "%s/.%s-wrapped" +} + + +echo "$cur" +while [ -n "$next" ]; do + cur="$next" + echo "-> $cur" + next="$(getNext "$cur")" done +echo -echo '->' "$v" -echo '' -case $(file --brief --mime "$v") in +case $(file --brief --mime "$cur") in (*text*) - cat "$v" + cat "$cur" ;; (*) - echo $(file "$v") + echo $(file "$cur") ;; esac