X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tinyos.sh;h=a41c7e53cc263101d96660a28d99b0023e460178;hb=refs%2Fheads%2Fdebian%2F2.1.0;hp=20814c4eca631b46c7b3c0b23692709c18ce7063;hpb=4845da46c1bc60e3421abf4badf38abd4559a036;p=tinyos-2.x.git diff --git a/tinyos.sh b/tinyos.sh index 20814c4e..a41c7e53 100644 --- a/tinyos.sh +++ b/tinyos.sh @@ -1,43 +1,142 @@ -#! /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="$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 -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 < + + 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 [ | ]" >&2 - export TOSROOT TOSDIR MAKERULES +EOF1 + listsources + cat < ~/.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 "$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 -unset TOSBASE tmp +export TOSROOT TOSDIR MAKERULES +unset TOSBASE RCFILES + +# vi: sw=4