summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authortv <tv@iiso>2011-09-18 22:50:48 +0200
committertv <tv@iiso>2011-09-18 22:50:48 +0200
commit820e9babae95ad58b26f25983a4fd3aaebd4ac65 (patch)
treec16d3350989e39f70ca1bb0d35f02656bb2b3cd2 /util
parentdbc2b120fb03ebf3301ab843599d43f1636f46be (diff)
//util lgoinstall: initial commit
This script tries to goinstall local/$(basename $PWD)
Diffstat (limited to 'util')
-rwxr-xr-xutil/bin/lgoinstall125
1 files changed, 125 insertions, 0 deletions
diff --git a/util/bin/lgoinstall b/util/bin/lgoinstall
new file mode 100755
index 00000000..4cd0e852
--- /dev/null
+++ b/util/bin/lgoinstall
@@ -0,0 +1,125 @@
+#! /bin/sh
+#
+# usage: lgoinstall
+#
+# This script tries to goinstall $PWD
+#
+# target=local/$(basename $PWD)
+# ensure there's a symlink local/$target somewhere in $GOROOT or $GOPATH
+# goinstall local/$(dirname $PWD)
+#
+set -euf
+
+#fqtarget="$(readlink -f "$PWD")"
+fqtarget="$PWD"
+target="local/$(basename "$fqtarget")"
+
+#
+# resolve PATH [relpath [FS]] # ====> abspath...
+#
+# Resolve relpath to abspath for each component x separated by FS in PATH
+# where x/relpath is an existing file.
+#
+# relpath defaults to ""
+# FS defaults to ":"
+#
+# Example:
+#
+# resolve "/bin:/usr/bin" sh # ====> /bin/sh
+#
+resolve() {
+ set -- "$1" "${2+/$2}" "${3-:}" "$IFS"
+ IFS="$3"
+ for i in $1; do
+ ! test -x "$i$2" || echo "$i$2"
+ done
+ unset i
+ IFS="$3"
+}
+
+#
+# xargstest TEST_ARGS...
+#
+# Read filenames from stdin and
+# write each filename that satisfies test # $TEST_ARGS to stdout.
+#
+# Each %x in TEST_ARGS gets replaced by the readlink -f of the filename.
+#
+# Example:
+#
+# xargstest -d %x <<EOF
+# /bin/sh
+# /usr
+# EOF
+# # ====> /usr
+#
+xargstest() {
+ while read x; do
+ fqx="$(readlink -f "$x")"
+ if sub %x "$x" sub %fqx "$fqx" test "$@"; then
+ #if test "${@//%x/$fqx}"; then
+ echo "$x"
+ fi
+ done | grep .
+}
+
+sub() {
+ sub1="$1"
+ sub2="$2"
+ shift 2
+ set -- "${@//$sub1/$sub2}"
+ unset sub1 sub2
+ "$@"
+}
+
+path="${GOROOT-}${GOPATH+:$GOPATH}"
+
+#
+# if there's a src/$target that points to $PWD, then succeed
+#
+if x="`resolve "$path" src/$target | xargstest "$fqtarget" = %fqx`"
+then
+ echo "good $target: $x -> $PWD" >&2
+ exec goinstall "$target"
+fi
+
+#
+# if there's some other src/$target then die
+#
+if x="`resolve "$path" src/$target | xargstest ! -x %x -o "$fqtarget" != %fqx`"
+then
+ echo bad $target: $x >&2
+ echo check your GOROOT and/or GOPATH or eliminate that $target >&2
+ exit 23
+fi
+
+#
+# if we've no Go-source in $PWD then die to prevent clobbering $GOPATH & co.
+#
+if ! ls | grep '\.go$'; then
+ echo "Your \$PWD seems to contain no Go-source... abort." >&2
+ exit 23
+fi
+
+#
+# if we can write to some src/local then symlink $PWD and retry
+#
+if x="`resolve "$path" src/local | xargstest -w %x`"
+then
+ #echo writable src/local: $x
+ ln -vsnf $fqtarget $x
+ exec "$0" "$@"
+fi
+
+#
+# if we can write to some src then mkdir src/local and retry
+#
+if x="`resolve "$path" src | xargstest -w %x`"
+then
+ #echo writable src: $x
+ mkdir "$x/local"
+ exec "$0" "$@"
+fi
+
+echo "This script failed: good luck, you're on your own!" >&2
+exit 23