#!/bin/bash

# http://reprap.org/wiki/SimulAVR
# (because I can't get the official SimulAVR at savannah to build a working executable)

# Stop on errors
set -e
if [ ! -f .prereq ];then ./prerequise.sh;fi

source avr-file-names

TIME_START=$(date +%s)

echo "Downloading sources..."
wget -c http://wareck.free.fr/cross_compilation/avr_toolchain/simulavr.tar.xz
tar xfJ simulavr.tar.xz

# Make simulavr
echo "Making simulavr in $NAME_SIMULAVR..."
cd $NAME_SIMULAVR/
git checkout traumflug

cp ../pack/simulavr_patch1.patch .
cp ../pack/simulavr_patch2.patch .
patch -p1 < simulavr_patch1.patch
patch -p1 < simulavr_patch2.patch

./bootstrap
./configure --enable-tcl --prefix=$PREFIX
make
sudo make install-strip

cd ..

TIME_END=$(date +%s)
TIME_RUN=$(($TIME_END - $TIME_START))

echo ""
echo "Done in $TIME_RUN seconds"

exit 0