#!/bin/bash

# Arduino-IDE-Version (ggf. anpassen)
AVERSION="arduino-1.0.1"

# Username als Parameter
if [ $# -ne 1 ]
then
  echo "Usage: $0 USERNAME"
  exit 1
fi

# are we root?
if ! id | grep -q root
  then
  echo "Du musst root sein, um das zu machen"
  exit 1
  fi

NAME=$1
echo "Processing $NAME ..."

echo "installing putty"
apt-get -y install putty putty-doc
cat > /home/$NAME/Arbeitsfläche/Putty.desktop <<EOF1
#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[de_DE]=putty
Name[de_DE]=putty
Exec=/usr/bin/putty
Name=putty
Icon=putty
Comment=Putty
EOF1
chmod 755 /home/$NAME/Arbeitsfläche/Putty.desktop
chown $NAME.$NAME /home/$NAME/Arbeitsfläche/Putty.desktop
echo "Putty done ..."

# Arduino-Zeug
apt-get -y install openjdk-6-jre libjna-java libjna-java-doc
apt-get -y install extra-xdg-menus
apt-get -y install gcc-avr avr-libc avrdude avrdude-doc binutils-avr gdb-avr ponyprog

cd /home/$NAME/
wget http://arduino.googlecode.com/files/${AVERSION}-linux.tgz
tar xzvf ${AVERSION}-linux.tgz
ln -s $AVERSION arduino
cd $AVERSION
test -f arduino.orig || cp arduino arduino.orig
sed -e 's/^java.*$/java processing.app.Base/' < arduino.orig > arduino

cat <<EOF2 > /home/$NAME/Arbeitsfläche/Arduino.desktop
#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[de_DE]=/home/$NAME/arduino/reference/img/logo.png
Name[de_DE]=Arduino
Exec=/home/$NAME/arduino/arduino
Name=Arduino
Icon=/home/$NAME/arduino/reference/img/logo.png
Comment=Arduino
EOF2
chmod 755 /home/$NAME/Arbeitsfläche/Arduino.desktop
chown $NAME.$NAME /home/$NAME/Arbeitsfläche/Arduino.desktop

cat <<EOF3 >> /etc/udev/rules.d/50-local.rules 
#Arduino FTDI + NANO
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", RUN+="chmod 555 /dev/ttyUSB0"
#Arduino UNO
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idProduct}=="0043", ATTRS{idVendor}=="2341", RUN+="chmod 555 /dev/ttyACM0"
EOF3
udevadm control --reload-rules
echo "Arduino done ..."
echo "Ready"

