#!/bin/sh
# 
# This script prepares avrdude for raspduino. 
#
# This script is copyright 2012 (C) BitWizard B.V. 
# You're granted the right to use this script with your 
# raspduino that you buy from BitWizard B.V. You have the
# right to look at this copyright message at the top and
# then decide that you're not allowed to run or copy it. 
# All other rights reserved. 
#
myid=`id -u`
if [ $myid -ne 0 ] ; then
   echo "You need to run this script as root. e.g. sudo $0"
   exit 1
fi

echo "---------- checking required packages ---------"
need_update=true
required_programs="avrdude arduino"
for i in $required_programs ; do
   if [ ! -f /usr/bin/$i ] ; then
      # $i is not installed, lets install it. 
      echo  "----- Installing $i -----" 
      if [ -z `which apt-get` ] ; then 
         # Lets hope this works! (on arch). 
         pacman -S --noconfirm $i
      else
         if $need_update ; then
            apt-get update
            need_update=false
         fi
         apt-get -y install $i
      fi
   else 
      echo "OK: You already have $i installed "
   fi
done

echo "----------- checking/updating avrdude -----------"
avrdudefile=`file /usr/bin/avrdude`

case "$avrdudefile" in 
   *"ELF 32-bit LSB executable, ARM"*) 
      #
      echo "Replacing avrdude with script to reset raspduino"
      if [ ! -f /usr/bin/avrdude.bin ] ; then
         mv /usr/bin/avrdude /usr/bin/avrdude.bin
      else 
         echo "Avrdude.bin already exists. Cannot continue."
         exit 1
      fi
      cat << EOF > /usr/bin/avrdude
#!/bin/sh
# This script replaces avrdude and resets the raspduino
# in case you don't have the software DTR patch. 
[ -f /etc/have_software_DTR ] || sudo reset_raspduino
avrdude.bin "\$@"
EOF
      chmod 755 /usr/bin/avrdude
      ;;
   *"POSIX shell script"*) 
      echo "OK: It seems this script has already been run. No changes. Done!"
      ;;
   *) 
      echo "Your avrdude is weird. Not an arm executeable but neither a shell script. "
      echo "cannot continue."
      exit 1
      ;;
esac

echo ---------- checking reset_raspduino ----------------
file=/usr/bin/reset_raspduino
if [ ! -f $file ] ; then
      echo "Creating reset_raspduino"
      cat << EOF > $file
#!/bin/sh
gpio_num=18
echo \$gpio_num > /sys/class/gpio/export
echo out       > /sys/class/gpio/gpio\$gpio_num/direction
echo 1         > /sys/class/gpio/gpio\$gpio_num/value
sleep 0.05
echo 0         > /sys/class/gpio/gpio\$gpio_num/value
echo \$gpio_num > /sys/class/gpio/unexport
EOF
      chmod 755 $file
else
   echo "OK: you allready have a reset_raspduino. "
fi


echo ------------- checking raspduino.rules -----------------
file=/etc/udev/rules.d/80-raspduino.rules
if [ ! -f $file ] ; then
   echo "creating 80 raspduino rules (link ttyAMA to ttyS0)." 
   echo 'KERNEL=="ttyAMA0", SYMLINK+="ttyS0",GROUP="dialout",MODE:=0666' \
     > $file
else 
   echo "OK: You already have a raspduino.rules."
fi

echo ------------- checking inittab -----------------
file=/etc/inittab
if grep -q ttyAMA0 $file ; then
   # ttyAMA0 is present in inittab...   
   if grep -q ^[^#].*ttyAMA0 $file ; then
      # ... and not commented out. Lets do that. 
      echo "removing getty entry for ttyAMA0 from /etc/inittab"
      cp -p $file "$file".orig
      sed -e 's/\(.*\)ttyAMA0/#\1ttyAMA0/' "$file".orig > $file
   else 
      echo "OK: You already have ttyAMA commented out. Good."
   fi
else
   # No ttyAMA in inittab
   echo "OK: You have no ttyAMA in your inittab. That's weird for a Raspberry pi system."
   # Lets assume that's ok. 
fi

echo ------------- checking upstart -----------------
file=/etc/init/ttyAMA0.conf
if [ -f $file ] ; then
   if ! grep -q manual $file ; then
      echo "Disable autostart of ttyAMA getty."
      # the .orig extension causes the file to be ignored by upstart.
      cp $file $file.orig
      echo manual >> $file
   else
      echo "OK: Your ttyAMA upstart config is already disabled. Good."
   fi
else
   echo "OK: You seem to not have ttyAMA0 upstart config. Fine."
fi
      


echo ------------- checking cmdline.txt -----------------
if grep -q ttyAMA0 /boot/cmdline.txt ; then
   echo "removing ttyAMA0 as console from cmdline."
   mv /boot/cmdline.txt /boot/cmdline.txt.orig
   # remove ttyAMA references from cmdline.txt. Usually console= and kgdboc= . 
   sed -e 's/[^ ]*ttyAMA0[^ ]*//g' /boot/cmdline.txt.orig > /boot/cmdline.txt  
else 
   echo "OK: Your ttyAMA was already removed from cmdline.txt"
fi

echo ------------- checking boards.txt -----------------
file=/usr/share/arduino/hardware/arduino/boards.txt
if ! grep -q raspduino $file ; then
   echo "... adding raspduino to arduino hardware list. " 
   cp $file $file.orig
   cat <<EOF >> $file

##############################################################

raspduino.name=Raspduino
raspduino.upload.protocol=arduino
raspduino.upload.maximum_size=30720
raspduino.upload.speed=57600
raspduino.bootloader.low_fuses=0xff
raspduino.bootloader.high_fuses=0xda
raspduino.bootloader.extended_fuses=0x05
raspduino.bootloader.path=atmega
raspduino.bootloader.file=ATmegaBOOT_328_diecimila.hex
raspduino.bootloader.unlock_bits=0x3F
raspduino.bootloader.lock_bits=0x0F
raspduino.build.mcu=atmega328p
raspduino.build.f_cpu=16000000L
raspduino.build.core=arduino
raspduino.build.variant=standard
EOF

else
   echo "OK:  your boards.txt already has raspduino"
fi