#!/bin/bash

# determine platform architecture
ARCH=`uname -m`

## prerequisites

# get Arduino package, depending on JRE, JDK and RXTX library
apt-get -y install arduino

# download and unpack software for Sanguino as Arduino variant
wget -N http://sanguino.googlecode.com/files/Sanguino-0018r2_1_4.zip
unzip -qo Sanguino-0018r2_1_4.zip Sanguino/* -d /usr/share/arduino/hardware

# get 64-bit Java3D JNI library, if relevant
if [ $ARCH="x86_64" ]; then
	sudo apt-get -y install libjava3d-jni
fi

## download and unpack architecture relevant RepRap bundle; adjust it
if [ $ARCH="x86_64" ]; then
	wget -N --content-disposition http://sourceforge.net/projects/reprap64bit/files/reprap%2064bit-mendel-20100719.zip/download
	unzip -qo reprap\ 64bit-mendel-20100719.zip -d /usr/local
	mkdir -p /usr/local/reprap
	cp -auf /usr/local/Reprap/* /usr/local/reprap
	rm -rf /usr/local/Reprap
	cd /usr/local/reprap
	ln -sf /usr/lib/jni/libj3dcore-ogl.so .
	ln -sf /usr/lib/jni/librxtxParallel.so .
	ln -sf /usr/lib/jni/librxtxSerial.so .
	chmod +x reprap
else
	wget -N --content-disposition http://sourceforge.net/projects/reprap/files/reprap-mendel-20100806.zip/download
	unzip -qo reprap-mendel-20100806.zip -d /usr/local
	mkdir -p /usr/local/reprap
	cp -auf /usr/local/reprap-mendel-20100806/* /usr/local/reprap
	rm -rf /usr/local/reprap-mendel-20100806
fi

## create startup script
echo '#!/bin/sh' > /usr/local/bin/reprap && echo "cd /usr/local/reprap\n./reprap &" >> /usr/local/bin/reprap && chmod +x /usr/local/bin/reprap
