#!/bin/sh
# Very dumb script for automatically downloading, patching and compiling the
# necessary TinyOS 1.x tools for Debian
# (tested with TinyOS 1.1.15 and NESC 1.1.3)
# Requirements: lynx, wget, grep, echo, awk, fakeroot, alien, rm, gperf,
#               patch, tar
# (@) rusu@cs.tum.edu - 06.02.2006

# Change the web address if you need another distribution
TOSTOOLS_WEB='http://www.tinyos.net/dist-1.1.0/tools/linux'
TINYOS_WEB='http://tinyos.net/dist-1.1.0/tinyos/linux/'
XBOW_WEB='http://www.xbow.com/Support/Support_pdf_files/Crossbow.tgz'

# Change the SF mirror if Switch doesn't work (eg. mesh, ovh, kent, surfnet, etc)
# (see http://prdownloads.sourceforge.net/nescc/nescc-1.1.3.tar.gz)
NESCC_WEB='http://switch.dl.sourceforge.net/sourceforge/nescc/nesc-1.1.3.tar.gz'

# Patches for NESCC (Note: NESCC 1.2.x doesn't work yet with TinyOS - 06.02.2006)
NESCC_PATCHES_WEB='http://www9.cs.tum.edu/people/rusu/files/nescc-1.1.3/'

# JavaX Comm API 2.0 (Sparc) + RXTX = winning combination
JAVAXCOMM_WEB='http://www9.cs.tum.edu/people/rusu/files/javaxcomm/'

PATH=/usr/bin:/bin/:$PATH

# AVR
for i in `lynx -dump $TOSTOOLS_WEB | grep 'http:.*avr' | grep -v insight | awk {'print $2'}`
do
	filename=`echo $i | awk -F '/' {'print $7'}`

	wget -c $i
	fakeroot alien -d $filename
	rm -f $filename
done

# TinyOS
for i in `lynx -dump $TINYOS_WEB | grep -E '(http:.*tinyos).*((1.1.15)|(tools))' | awk {'print $2'}`
do
	filename=`echo $i | awk -F '/' {'print $7'}`

	wget -c $i
	fakeroot alien -d $filename
	rm -f $filename
done

wget -c $NESCC_WEB
NESCC_FILE=`echo $NESCC_WEB | awk -F '/' {'print $6'}`
tar xvfz $NESCC_FILE
cd nesc*
#wget -q -erobots=off -R "*index*" -r -nd -np $NESCC_PATCHES_WEB
wget -q -erobots=off -R "*index*" -r -nd -np $NESCC_PATCHES_WEB/builtin_offset.patch.nesc
wget -q -erobots=off -R "*index*" -r -nd -np $NESCC_PATCHES_WEB/noinline.patch.nesc
wget -q -erobots=off -R "*index*" -r -nd -np $NESCC_PATCHES_WEB/static-clex.patch.nesc
patch -p1 < noinline.patch.nesc
patch -p1 < builtin_offset.patch.nesc
patch -p0 < static-clex.patch.nesc
cd ..

# JavaX Comm API 2.0 + RXTX
#wget -q -erobots=off -R "*index*" -r -nd -np $JAVAXCOMM_WEB
wget -q -erobots=off -R "*index*" -r -nd -np $JAVAXCOMM_WEB/javax_comm-2_0_2-solsparc.tar.Z
wget -q -erobots=off -R "*index*" -r -nd -np $JAVAXCOMM_WEB/rxtx-bins.1.tar.gz
tar xvfz rxtx-bins.1.tar.gz
tar xvfz javax_comm-2_0_2-solsparc.tar.Z

# Crossbow's TinyOS resources
wget -c $XBOW_WEB
XBOW_FILE=`echo $XBOW_WEB | awk -F '/' {'print $6'}`
tar xvfz $XBOW_FILE

echo "driver=gnu.io.RXTXCommDriver" > javax.comm.properties
echo "serpath0=/dev/ttyS0" >> javax.comm.properties

echo "---"
echo "That's it. Login as root and type: "
echo "  -> 'dpkg -i *.deb' for installing AVR and TINYOS;"
echo "  -> 'cd nesc*; ./configure; make; make install' for compiling and installing NESC;"
echo ""
echo " * set JAVAHOME to point to your JDK/JRE."
echo ""
echo "  -> 'cp ./rxtx-bins.1/1.4/i386-pc-linux/* \$JAVAHOME/jre/lib/i386/' to install the serial libs;"
echo "  -> 'cp ./rxtx-bins.1/1.4/jcl.jar \$JAVAHOME/jre/lib/ext/' for installing the RXTX Comm jar;"
echo "  -> 'cp ./commapi/comm.jar \$JAVAHOME/jre/lib/ext/' for installing the JavaX Comm jar;"
echo "  -> 'cp ./javax.comm.properties \$JAVAHOME/jre/lib/' for installing the JavaX Comm properties;"
echo ""
echo " * Copy xbow/* to \$TOSROOT/contrib"
echo "---"
