#!/bin/bash
# Download and install RepRap software and additional components on Debian/Ubuntu/derivatives

# Determine platform architecture
ARCH=`uname -m`

# Get Arduino package and JRE, RXTX library, JDK as dependencies
apt-get -y install arduino

# Download and unpack software for Sanguino as Arduino variant
wget --timestamping --directory-prefix=/tmp http://sanguino.googlecode.com/files/Sanguino-0018r2_1_4.zip
unzip -o /tmp/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
if [ $ARCH="x86_64" ]; then
	wget --timestamping --content-disposition --directory-prefix=/tmp http://sourceforge.net/projects/reprap64bit/files/reprap%2064bit-mendel-20100719.zip/download
	unzip -o /tmp/reprap\ 64bit-mendel-20100719.zip -d /tmp
	mkdir -p /usr/local/reprap
	cp -auf /tmp/Reprap/* /usr/local/reprap
	rm -rf /tmp/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 --timestamping --content-disposition --directory-prefix=/tmp http://sourceforge.net/projects/reprap/files/reprap-mendel-20100806.zip/download
	unzip -o /tmp/reprap-mendel-20100806.zip -d /tmp
	mkdir -p /usr/local/reprap
	cp -auf /tmp/reprap-mendel-20100806/* /usr/local/reprap
	rm -rf /tmp/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

# Announce finish
echo "RepRap install finished"
