#!/bin/bash
#Crespi Floriana
#CSP UHD-GNURadio 3.6.2 Installer
#17 July 2013

SRC_PATH=$HOME/src
CURRENT_DIR=$(pwd)
sudo apt-get update
sudo apt-get install cmake libcppunit-dev libboost1.46-all-dev libusb-1.0-0-dev libusb++-dev python libboost-all-dev cmake

UHD_FIND=$(dpkg -l | grep uhd)
if [ "$UHD_FIND" == "" ]; then	
	sudo bash -c 'echo "deb http://files.ettus.com/binaries/uhd_stable/repo/uhd/ubuntu/`lsb_release -cs` `lsb_release -cs` main" > /etc/apt/sources.list.d/ettus.list'
	sudo apt-get update
	sudo apt-get install -t `lsb_release -cs` uhd
else
	echo "UHD package is already installed."
fi
FIND_GNURADIO=$(find /usr/local/lib -iname \*libgnuradio-core\* 2>/dev/null && find /usr/lib -iname \*libgnuradio-core\* 2>/dev/null ) 
#echo "$FIND_GNURADIO"
if [ "$FIND_GNURADIO" != "" ]; then
Gnuradio_install=`zenity --title "GNURadio already installed!" --text "GNURadio already installed. Do you want to install it?: " --list --radiolist --column="" --column="" False "Yes-Install" True "No" --width=30 --height=220 `
if [ "$Gnuradio_install" = "No" ]; then 
exit;
fi
fi
if [ ! -e $SRC_PATH ]; then
   echo "*** Create src folder ***"
   mkdir $SRC_PATH
fi
echo "*** Extract GNURadio 3.6.2 package ***"
tar xvf $CURRENT_DIR/gnuradio-3.6.2.tar.gz -C $HOME/src

echo "*** Installing GNURadio dependencies ***"
sudo apt-get -y install git-core autoconf automake  libtool g++ python-dev swig pkg-config libboost1.46-all-dev libfftw3-dev libcppunit-dev libgsl0-dev libusb-dev sdcc libsdl1.2-dev python-wxgtk2.8 python-numpy python-cheetah python-lxml doxygen python-qt4 python-qwt5-qt4 libxi-dev libqt4-opengl-dev libqwt5-qt4-dev libfontconfig1-dev libxrender-dev

INSTALL_OK=0
if [ -e $SRC_PATH/gnuradio-3.6.2/build ];
  then
  rm -rf $SRC_PATH/gnuradio-3.6.2/build 
fi
cd $SRC_PATH/gnuradio-3.6.2 && mkdir build && cd build && cmake ../ > cmake_log.txt && make 2> Make_log.txt && sudo make install && INSTALL_OK=1 
if [ "$INSTALL_OK" == "1" ]; then
   sudo ldconfig
   echo "*** GNURadio 3.6.2 installed ***"
else
   echo "Error: GNURadio NOT installed"
   exit;
fi
