#!/bin/bash

scriptdir=`pwd`

apt-get update
if [ "$?" != "0" ]
then
    echo Error running apt-get update
    exit $?
fi

apt-get -y install php5 libapache2-mod-php5 php5-cli php5-cgi php5-mcrypt php5-gd apache2 minicom screen autoconf libusb-1.0-0-dev libtool libftdi-dev texinfo ser2net libudev-dev libusb-1.0-0-dev libfox-1.6-dev autoconf automake libtool telnet
if [ "$?" != "0" ]
then
    echo Error fetching required packages
    exit $?
fi

if [  -d "/var/www/codiad" ]; then
    echo Codiad already exists, skipping...
else
    git clone https://github.com/Codiad/Codiad.git /var/www/codiad
    if [ "$?" != "0" ]
    then
        echo Error fetching Codiad
        exit $?
    fi
fi

if [  -d "/tmp/hidapi" ]; then
    rm -R /tmp/hidapi
fi

git clone https://github.com/signal11/hidapi /tmp/hidapi
if [ "$?" != "0" ]
then
    echo Error fetching hidapi
    exit $?
fi

cd /tmp/hidapi
./bootstrap
if [ "$?" != "0" ]
then
    echo Error building hidapi
    exit $?
fi

./configure
if [ "$?" != "0" ]
then
    echo Error building hidapi
    exit $?
fi
make

if [ "$?" != "0" ]
then
    echo Error building hidapi
    exit $?
fi

make install
if [ "$?" != "0" ]
then
    echo Error installing hidapi
    exit $?
fi
cd $scriptdir
rm -R /tmp/hidapi

PROCESSOR=`cat /proc/cpuinfo | grep Hardware | awk '{ print $3 }'`

if [ "$PROCESSOR" == "BCM2708" ]
then
	echo "$PROCESSOR found: Pi 1"
	ARMINARM=arm-toolchain-bin-2014-06-20-raspbian.tar.gz
elif [ "$PROCESSOR" == "BCM2709" ]
then
	echo "$PROCESSOR found: Pi 2"
	ARMINARM=arm-toolchain-bin-2014-06-20-raspbian.tar.gz
else
	echo "unknown processor"
	ARMINARM=arm-toolchain-bin-2014-06-20-raspbian.tar.gz
	exit
fi

URL=https://raw.githubusercontent.com/ARMinARM/arminarm-bin/master/$ARMINARM

if [ ! -f $ARMINARM ]; then
	echo "toolchain tar.gz not found. Getting it from github."
	wget $URL
fi

if [ -f $ARMINARM ]; then
	echo "Found toolchain tar.gz"
	mkdir arminarm
	cd arminarm
	tar -xzvf ../$ARMINARM || { echo "Error extracting "$ARMINARM; exit; }
	cd ..
	sudo rm -rf /opt/arminarm/
	sudo mv arminarm/ /opt/
	export PATH=$PATH:/opt/arminarm/bin
	echo "Installed OK"
fi

if [ "$?" != "0" ]
then
    echo Error installing arminarm
    exit $?
fi


cd /var/www
rm /var/www/index.html
wget www.io-expert.com/portabledevtool/www/index.html
wget www.io-expert.com/portabledevtool/www/css.zip
unzip -u css.zip
rm -R __MACOSX
rm css.zip
cd $scriptdir

cd /var/www/codiad/plugins
wget www.io-expert.com/portabledevtool/codiad/plugins/Codiad-CompileEmbedded-master.zip
rm -R /var/www/codiad/plugins/Codiad-CompileEmbedded-master
unzip -u Codiad-CompileEmbedded-master.zip
rm Codiad-CompileEmbedded-master.zip
rm -R __MACOSX
cd $scriptdir

#cd /var/www/codiad/data
#rm /var/www/codiad/data/projects.php
#wget www.io-expert.com/portabledevtool/codiad/data/projects.php
#cd /var/www/codiad/workspace/
#wget www.io-expert.com/portabledevtool/codiad/bulbboard_template.zip
#unzip -u bulbboard_template.zip
#rm bulbboard_template.zip
#rm -R __MACOSX
#cd $scriptdir

cd /var/www/codiad/plugins/Codiad-CompileEmbedded-master/template
rm index.csv
wget www.io-expert.com/portabledevtool/template/index.csv
IFSold=$IFS
IFS='
'
for i in `cat index.csv`
do
   filename=`echo $i | awk -F ';' '{print $1}'`
   description=`echo $i | awk -F ';' '{print $2}'`
   wget wget www.io-expert.com/portabledevtool/template/$filename
done
IFS=$IFSold
cd $scriptdir

chown -R www-data:www-data /var/www
chmod -R 777 /var/www/codiad

apt-get -y install dnsmasq
if [ "$?" != "0" ]
then
    echo Error fetching dnsmasq
    exit $?
fi
cd /tmp
wget www.io-expert.com/portabledevtool/dnsmasq/dnsmasq.conf
if [ "$?" != "0" ]
then
    echo Error fetching dnsmasq
    exit $?
fi
wget www.io-expert.com/portabledevtool/dnsmasq/default/dnsmasq
if [ "$?" != "0" ]
then
    echo Error fetching dnsmasq
    exit $?
fi
if [ -e /tmp/dnsmasq.conf ]; then
    rm /etc/dnsmasq.conf
    mv /tmp/dnsmasq.conf /etc/dnsmasq.conf
    rm /etc/default/dnsmasq
    mv /tmp/dnsmasq /etc/default/dnsmasq
fi

apt-get -y install udhcpd
if [ "$?" != "0" ]
then
    echo Error fetching udhcpd
    exit $?
fi
cd /tmp
wget www.io-expert.com/portabledevtool/udhcpd/udhcpd.conf
if [ "$?" != "0" ]
then
    echo Error fetching udhcpd
    exit $?
fi
wget www.io-expert.com/portabledevtool/udhcpd/default/udhcpd
if [ "$?" != "0" ]
then
    echo Error fetching udhcpd
    exit $?
fi
if [ -e /tmp/udhcpd.conf ]; then
    rm /etc/udhcpd.conf
    mv /tmp/udhcpd.conf /etc/udhcpd.conf
    rm /etc/default/udhcpd
    mv /tmp/udhcpd /etc/default/udhcpd
fi

cd /tmp
wget www.io-expert.com/portabledevtool/hexdwl/bin/raspberrypi/hexdwl
if [ "$?" != "0" ]
then
    echo Error fetching hexdwl
    exit $?
fi
wget www.io-expert.com/portabledevtool/hexdwl/spansion-cmsis-dap.rules
if [ "$?" != "0" ]
then
    echo Error fetching hexdwl
    exit $?
fi
if [ -e /tmp/hexdwl ]; then
    rm /usr/local/bin/hexdwl
    mv /tmp/hexdwl /usr/local/bin/hexdwl
    chown root:root /usr/local/bin/hexdwl
    chmod 755 /usr/local/bin/hexdwl
    rm /etc/udev/rules.d/spansion-cmsis-dap.rules
    mv /tmp/spansion-cmsis-dap.rules /etc/udev/rules.d/spansion-cmsis-dap.rules
fi
cd $scriptdir


cd /tmp
wget www.io-expert.com/portabledevtool/ser2net/ser2net.conf
if [ "$?" != "0" ]
then
    echo Error fetching ser2net configuration
    exit $?
fi
if [ -e /tmp/ser2net.conf ]; then
    rm /etc/ser2net.conf
    mv /tmp/ser2net.conf /etc/ser2net.conf
    wget www.io-expert.com/portabledevtool/ser2net/init.d/ser2net
    rm /etc/init.d/ser2net
    mv ser2net /etc/init.d/ser2net
    chmod 755 /etc/init.d/ser2net
    update-rc.d -f ser2net remove
    update-rc.d ser2net defaults
fi
cd $scriptdir

apt-get -y install hostapd
if [ "$?" != "0" ]
then
    echo Error fetching hostapd
    exit $?
fi

cd /tmp

wget www.io-expert.com/portabledevtool/hostapd/hostapd.conf
wget www.io-expert.com/portabledevtool/hostapd/bin/hostapd
if [ "$?" != "0" ]
then
    echo Error fetching hostapd configuration
    exit $?
fi
if [ -e /tmp/hostapd.conf ]; then
    rm /etc/hostapd.conf
    mv /tmp/hostapd.conf /etc/hostapd.conf
    rm /usr/sbin/hostapd
    mv hostapd /usr/sbin/hostapd
    chown root:root /usr/sbin/hostapd
    chmod 755 /usr/sbin/hostapd
    wget www.io-expert.com/portabledevtool/hostapd/init.d/hostapd
    rm /etc/init.d/hostapd
    mv hostapd /etc/init.d/hostapd
    chmod 755 /etc/init.d/hostapd
    update-rc.d hostapd defaults
fi
cd $scriptdir

cd /tmp
wget www.io-expert.com/portabledevtool/network/interfaces
if [ -e /tmp/interfaces ]; then
    rm /etc/network/interfaces
    cp /tmp/interfaces /etc/network/interfaces
fi

cd /tmp
wget www.io-expert.com/portabledevtool/etc/hosts
if [ -e /tmp/hosts ]; then
    rm /etc/hosts
    cp /tmp/hosts /etc/hosts
fi
cd $scriptdir


apt-get -y install python-pip python-dev
if [ "$?" != "0" ]
then
    echo Error fetching pip
    exit $?
fi

cd /tmp
wget https://github.com/downloads/liftoff/GateOne/python-tornado_2.4-1_all.deb
if [ "$?" != "0" ]
then
    echo Error fetching tornado
    exit $?
fi
if [ -e /tmp/python-tornado_2.4-1_all.deb ]; then
    dpkg -i python-tornado_2.4-1_all.deb
    rm python-tornado_2.4-1_all.deb
fi
cd $scriptdir

pip install pil

cd /tmp
wget www.io-expert.com/portabledevtool/gateone/init.d/gateone
wget https://github.com/downloads/liftoff/GateOne/gateone_1.1-1_all.deb
if [ -e /tmp/gateone_1.1-1_all.deb ]; then
    dpkg -i gateone_1.1-1_all.deb
    rm /tmp/gateone_1.1-1_all.deb
    rm /etc/init.d/gateone
    mv /tmp/gateone /etc/init.d/gateone
    chmod 755 /etc/init.d/gateone
    update-rc.d -f gateone remove
    update-rc.d gateone defaults
    mkdir /opt/gateone/logs
    /opt/gateone/gateone.py &
    echo please wait 4 minutes...
    sleep 240
    kill `cat /var/run/gateone.pid`
    rm /opt/gateone/server.conf
    cd /opt/gateone
    wget www.io-expert.com/portabledevtool/gateone/server.conf
fi
cd $scriptdir

reboot




