clj: correctly find and use rlwrap & support clj as relative symlink
In reviewing ClojureX today on my commute home from work (in preparation for the rubylearning.org Clojure101 course,) I found and fixed a couple of small bugs in the 'clj' script.
First, clj didn't actually select rlwrap instead of jline like it promised it would. The problem was a typo in the avail function. Second, if clj is a relative symlink (e.g. bin/clj -> ../work/ClojureX/clj) it will not correctly determine the path because readlink fails to canonicalize the link.
diff --git a/clj b/clj
index b3000e7..5c188c5 100755
--- a/clj
+++ b/clj
@@ -14,7 +14,7 @@ USAGE="Usage: $PRG_NAME [java-opt*] [init-opt*] [main-opt] [arg*]"
# determine if $1 is an available program (eschew 'which', as it's unreliable)
avail() {
- type -P $1 $>/dev/null
+ type -P $1 &>/dev/null
}
# send the stock usage text to stderr
@@ -68,7 +68,7 @@ PRG="$0"
while [ -h "$PRG" ]; do
# if readlink is availble, use it; it is less fragile than relying on `ls` output
if avail readlink; then
- PRG=`readlink "$PRG"`
+ PRG=`readlink -f "$PRG"`
else
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
Applied.
Unfortunately the -f option breaks readlink on OSX. Since that always has been ClojureX's main platform, I had to remove it for the time being. Will have to implement some platform neutral way to determine the canonical path. The joys of cross-platform shell scripting...
Gahhh. And a casual google for an answer reveals ... there is no good answer :(
http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
How about a compromise? Don't support clj as a relative symlink unless we detect that readlink supports -f?
I know, I found the same link on Google, so probably will have to do something like what you mentioned in the last paragraph.