#!/bin/sh
homedir=`pwd`
echo " "
echo "Please, inform the ROOT System version you want download, compile and install (ex. 5.20.00): "
echo " "
read version
########################################
#
#   install first gsl-1.11
#
########################################
sudo cd /usr/lib
sudo mkdir gsl-1.11
cd $homedir
mkdir gsl-1.11
cd gsl-1.11
cp ../gsl-1.11.tar.gz .
tar -zxvf gsl-1.11.tar.gz
sudo ./configure --prefix=/usr/lib/gsl-1.11 --with-pic
sudo make
sudo make install
cd ../
sudo rm -rf gsl-1.11/
#######################################
#
#   now, install root
#
#######################################
cd /usr/local
	test -d root
	dir=$?
	if test $dir = 1
		then
		echo "Creating /usr/local/root"
		sudo mkdir root
	fi	
cd root
#
#  comment and uncomment the relevant line, depending if you have or 
#  not a local copy of the root source code
#
##echo "Ok! Downloading ROOT v"$version" from root.cern.ch"
##wget -c "ftp://root.cern.ch/root/root_v"$version".source.tar.gz"
cp $homedir/root_v"$version".source.tar.gz .
#
echo "ROOT System download concluded. Installing packages needed to compile the ROOT System in Ubuntu..."
sudo apt-get -y --force-yes install build-essential
sudo apt-get -y --force-yes install libx11-dev
sudo apt-get -y --force-yes install libxpm-dev
sudo apt-get -y --force-yes install libxft-dev
sudo apt-get -y --force-yes install libgl1-mesa-dev
sudo apt-get -y --force-yes install libglu1-mesa-dev
sudo apt-get -y --force-yes install libmysqlclient15-dev
sudo apt-get -y --force-yes install libsqlxx-dev
sudo apt-get -y --force-yes install libpng12-dev
sudo apt-get -y --force-yes install libgif-dev
sudo apt-get -y --force-yes install libungif4-dev
sudo apt-get -y --force-yes install python2.5-dev
sudo apt-get -y --force-yes install libxml2-dev
sudo apt-get -y --force-yes install libgsl0-dev
sudo apt-get -y --force-yes install libgmp3-dev
sudo apt-get -y --force-yes install libjpeg62-dev
sudo apt-get -y --force-yes install libtiff4-dev
sudo apt-get -y --force-yes install libffi4-dev   
sudo apt-get -y --force-yes install cernlib    
tar -zxvf "root_v"$version".source.tar.gz"
sudo mv root root_v$version
export ROOTSYS=`pwd`/root_v$version
export PATH=$PATH:$ROOTSYS/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
cd root_v$version
pwd
## for AMD 64 processor ONLY
./configure linuxx8664gcc --with-gsl-incdir=/usr/lib/gsl-1.11/include --with-gsl-incdir=/usr/lib/gsl-1.11/lib --enable-roofit
make
make install
cd ../
############################################
#
#   make this version the pro version
#
############################################
ln -s root_v$version pro
sudo rm -rf root_v$version.source.tar.gz
echo "ROOT System is compiled and installed! Enjoy!"
echo "set the environment variables to:"
echo "ROOTSYS --> /usr/local/root/pro"
echo "PATH --> $ROOTSYS/bin
echo "LD_LIBRARY_PATH --> $ROOTSYS/lib"

exit
