]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Add tinyos.sh, a utility for changing the active tree (TOSROOT).
authorR. Steve McKown <rsmckown@gmail.com>
Thu, 3 Dec 2009 20:45:13 +0000 (13:45 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Thu, 4 Nov 2010 14:24:52 +0000 (08:24 -0600)
tinyos.sh [new file with mode: 0644]

diff --git a/tinyos.sh b/tinyos.sh
new file mode 100644 (file)
index 0000000..6886286
--- /dev/null
+++ b/tinyos.sh
@@ -0,0 +1,142 @@
+#!/usr/bin/env bash
+#
+# Here we setup the environment variables needed by the tinyos make system.
+# Users source this script from their shell to setup tinyos for their login,
+# and may source again at any time to change which installed TinyOS source
+# tree their code shall be built against.
+
+TOSCFG="$HOME/.tosrc"
+TOSBASE=/opt/tinyos
+RCFILES="$HOME/.bashrc $HOME/.shrc $HOME/.kshrc $HOME/.cshrc $ENV"
+
+# $1 is a directory or tos version number returns a fully rooted and valid
+# TOSROOT, or nothing.
+gettosroot()
+{
+    if [ -d "$1" -a -d "$1/tos" ]; then
+       echo $(cd $1 && pwd)
+    elif [ -d "$TOSBASE/$1" -a -d "$TOSBASE/$1/tos" ]; then
+       echo "$TOSBASE/$1"
+    fi
+}
+
+# Update CLASSPATH by replacing the element value in $1 with the element value
+# in $2.  An empty $2 removes element $1, if present.  An empty $1, or $1 not
+# present, adds $2.
+updclasspath()
+{
+    local jpath="support/sdk/java/tinyos.jar"
+    if [ -n "$1" -a -n "$2" ] && \
+           echo "$CLASSPATH" | grep -q "$1/$jpath"; then
+       CLASSPATH=$(echo $CLASSPATH | sed -e "s|:$1/$jpath|:$2/$jpath|")
+    elif [ -n "$2" ]; then
+       CLASSPATH="$CLASSPATH:$2/$jpath"
+    elif [ -n "$1" ]; then
+       CLASSPATH=$(echo $CLASSPATH | sed -e "s|:$1/$jpath||")
+    fi
+    export CLASSPATH
+}
+
+# Install a tinyos.sh invocation in user shell rc files.
+installshrc()
+{
+    for rc in $RCFILES; do
+       if [ -f $rc ] && ! grep -q "$TOSBASE/tinyos.sh" $rc; then
+           echo "[ -d $TOSBASE ] && . $TOSBASE/tinyos.sh" >> $rc 2>/dev/null
+       fi
+    done
+}
+
+# Uninstall the tinyos.sh invocation in user shell rc files.
+uninstallshrc()
+{
+    for rc in $RCFILES; do
+       if [ -f $rc ] && grep -q "$TOSBASE/tinyos.sh" $rc; then
+           grep -v "$TOSBASE/tinyos.sh" $rc > $rc.$$
+           mv -f $rc.$$ $rc
+       fi
+    done
+}
+
+# List available TinyOS source versions
+listsources()
+{
+    echo -n "Available sources:"
+    find $TOSBASE -maxdepth 2 -name tos -type d 2>/dev/null | sort | \
+           while read dir; do
+       echo -n "  $(basename -- $(dirname -- $dir))"
+    done
+    echo
+}
+
+# MAIN
+
+if [ "$1" = "-t" ]; then
+    # Display TOS environment
+    if [ -z "$TOSROOT" ]; then
+       echo "tinyos: NOT configured"
+    else
+       echo "TOSROOT=$TOSROOT"
+    fi
+elif [ "$1" = "-l" ]; then
+    listsources
+elif [ "$1" = "-h" ]; then
+    cat <<EOF1
+Each TinyOS user must type the following to configure their login:
+
+    . $TOSBASE/tinyos.sh <version>
+
+<version> is the version of an installed source tree or the filesystem path to
+a TinyOS source tree
+
+EOF1
+    listsources
+    cat <<EOF2
+
+Run this command at any time to change the TinyOS source tree for the current
+and all future shell invocations.  The tree setting is persistent across
+logins.
+EOF2
+elif [ "$(basename -- $0)" = tinyos.sh ]; then
+    # Operations below here cannot be executed in a sub-shell
+    echo "tinyos: must be sourced"
+elif [ "$1" = "-u" ]; then
+    if [ -f "$TOSCFG" ]; then
+       uninstallshrc
+       updclasspath "$TOSROOT" ""
+       unset TOSROOT TOSDIR MAKERULES
+       rm -f "$TOSCFG"
+       echo "tinyos: environment uninstalled for $(whoami)"
+    else
+       echo "tinyos: environment not installed for $(whoami)"
+    fi
+else
+    # Get TOSROOT, either from command line or cached in $TOSCFG
+    if [ -n "$1" ]; then
+       newroot=$(gettosroot "$1")
+       if [ -n "$newroot" ]; then
+           echo "$newroot" > "$TOSCFG"
+           installshrc
+           echo "tinyos: now using $newroot"
+       else
+           echo "tinyos: invalid version; try -h option"
+       fi
+    else
+       newroot=$(cat "$TOSCFG" 2>/dev/null)
+       if [ -z "$newroot" ]; then
+           echo "tinyos: NOT configured; try -h option"
+       fi
+    fi
+
+    # Set TOS environment
+    if [ -n "$newroot" ]; then
+       updclasspath "$TOSROOT" "$newroot"
+       TOSROOT="$newroot"
+       TOSDIR="$TOSROOT/tos"
+       MAKERULES="$TOSROOT/support/make/Makerules"
+    fi
+fi
+export TOSROOT TOSDIR MAKERULES
+unset TOSBASE RCFILES
+
+# vi: sw=4