# Copyright (c) 2006 Chad Metcalf <chad@5secondfuse.com> 
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be 
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Java
export JDKROOT=/usr/lib/jvm/java-1.5.0-sun
export JAVAXROOT=$JDKROOT
if [ "$LD_LIBRARY_PATH" == "" ]; then
	# So Java can find libtoscomm.so and libgetenv.so
	export LD_LIBRARY_PATH=$JDKROOT/jre/lib/i386
fi

# Ubuntu comes with gcc 4.1 which is currently broken with TOSSIM so we'll
# use gcc/g++ 4.0
#export CC=gcc-4.0
#export CXX=g++-4.0

# Set your default version of TinyOS here.
if [ "$TINYOS_VER" == "" ]; then
        export TINYOS_VER=2.1.0
fi

# Preserve any non Tinyos related class paths that have been set prior 
# to this file being sourced. We check the for an empty OLD_CLASSPATH
# to ensure that OLD_CLASSPATH only gets set once. 
if [ "$OLD_CLASSPATH" == "" ]; then
        if [ "$CLASSPATH" == "" ]; then
                export OLD_CLASSPATH="empty"
        else
                export OLD_CLASSPATH=$CLASSPATH
        fi
fi

# Conditional environmental setup for 3 versions of TinyOS
if [ "$TINYOS_VER" == "1" ]; then

    export TOSROOT=/opt/tinyos-1.x
    export TOSDIR=$TOSROOT/tos
    export MAKERULES=$TOSROOT/tools/make/Makerules

    unset CLASSPATH

        # Restore the old non tinyos classpath if its not empty
        if [ "$OLD_CLASSPATH" != "empty" ]; then
                export CLASSPATH=$OLD_CLASSPATH
        fi

        export CLASSPATH=$CLASSPATH\:`$TOSROOT/tools/java/javapath`

        echo "Setting up for TinyOS 1.x"


        # These aliases only apply to a 1.x environment
        alias tinyviz="$TOSROOT/tools/java/net/tinyos/sim/tinyviz"
        alias Deluge="java net.tinyos.tools.Deluge"
elif [ "$TINYOS_VER" == "2.0.2" ]; then
    source /opt/tinyos-2.0.2/tinyos.sh
    export PYTHONPATH=:$TOSROOT/support/sdk/python/
else
    source /opt/tinyos-2.1.0/tinyos.sh
    export PYTHONPATH=:$TOSROOT/support/sdk/python/
#    export CLASSPATH=$CLASSPATH\:`$TOSROOT/support/sdk/java/tinyos.jar`
    export CLASSPATH=$CLASSPATH\:"$TOSROOT/support/sdk/java/tinyos.jar:."

fi

# Helpful aliases 
alias sf="java net.tinyos.sf.SerialForwarder"
alias listen="java net.tinyos.tools.Listen"

alias apps="cd $TOSROOT/apps"
alias tos="cd $TOSROOT/tos"

# Environment changing functions

# Change this shell's environment to support Tinyos 2.x development
function tos210 () {
        export TINYOS_VER=2.1.0
        . ~/.bash_tinyos
}

# Change this shell's environment to support Tinyos 2.x development
function tos202() {
        export TINYOS_VER=2.0.2
        . ~/.bash_tinyos
}

# Change this shell's environment to support Tinyos 1.x development
function tos1 () {
        export TINYOS_VER=1
        . ~/.bash_tinyos
}

function setMoteCom () {
    MOTE=`motelist -c| cut -d, -f2`

    TYPE="telosb"
    
    export MOTECOM="serial@$MOTE:$TYPE"
}

# Change ownership of the TinyOS directories to the TinyOS group
function tos-fix-permissions () {
    echo "sudo may prompt you for your password."
    sudo chown -R root:tinyos /opt/tinyos-*
    sudo chmod -R g+w /opt/tinyos-*
    sudo find /opt/tinyos-* -type d -exec chmod 2775 {} \;
}
