]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Improved tinyos.sh
authorR. Steve McKown <rsmckown@gmail.com>
Fri, 23 Apr 2010 16:25:39 +0000 (10:25 -0600)
committerR. Steve McKown <rsmckown@gmail.com>
Fri, 23 Apr 2010 19:32:17 +0000 (13:32 -0600)
* Automatically installs tinyos.sh into user shell rc files when sourced.
* Adds an uninstall option if a user no longer wants to do TinyOS dev.
* Smarter handling of CLASSPATH.
* tinyos.jar is added to CLASSPATH instead of support/sdk/java.  The latter is
  a TOS 1.x idiom.
* Better error and help messages.

tinyos.sh

index 20814c4eca631b46c7b3c0b23692709c18ce7063..f7e1368f061121d0451e8c3ba346bb50da997dbe 100644 (file)
--- a/tinyos.sh
+++ b/tinyos.sh
-#! /usr/bin/env bash
-# Here we setup the environment
-# variables needed by the tinyos 
-# make system
+#!/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=~/.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
-getdir()
+# $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 "$1"
+       echo $(cd $1 && pwd)
     elif [ -d "$TOSBASE/$1" -a -d "$TOSBASE/$1/tos" ]; then
        echo "$TOSBASE/$1"
     fi
 }
 
-# Clear TOS environment
-if [ -n "$TOSROOT" ]; then
-    CLASSPATH=$(echo $CLASSPATH | sed -e "s|:$TOSROOT/support/sdk/java||")
-fi
-unset TOSDIR MAKERULES
+# 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
+}
 
-# Set TOS environment
-TOSROOT=$(getdir "$1")
-if [ -z "$TOSROOT" ]; then
-    TOSROOT=$(cat ~/.tosrc 2>/dev/null)
-fi
+# 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
 
-if [ -z "$TOSROOT" ]; then
-    echo "No TinyOS source directory found.  Usage:" >&2
-    echo "source /opt/tinyos/tinyos.sh [<version> | <directory>]" >&2
-    export TOSROOT TOSDIR MAKERULES
+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
-    echo "Setting up for TinyOS source in $TOSROOT"
-    echo "$TOSROOT" > ~/.tosrc
-    TOSDIR="$TOSROOT/tos"
-    CLASSPATH=$CLASSPATH:$TOSROOT/support/sdk/java
-    MAKERULES="$TOSROOT/support/make/Makerules"
-    export TOSROOT TOSDIR CLASSPATH MAKERULES
+    # Get TOSROOT, either from command line or cached in $TOSCFG
+    if [ -n "$1" ]; then
+       newroot=$(gettosroot "$1")
+       if [ -n "$newroot" ]; then
+           echo "$TOSROOT" > "$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
-unset TOSBASE tmp
+export TOSROOT TOSDIR MAKERULES
+unset TOSBASE RCFILES
+
+# vi: sw=4