#!/bin/bash

#add here your username and password and uncomment
#username=fuid-xxxx
#pswd=


FLUKAOK=0
FLAIROK=0

if [ -n "$(which apt-get)" ]
then
    if sudo apt-get update
    then
	sudo apt-get install gfortran emacs python-imaging python-imaging-tk python-imaging-tk python-pil python-pil.imagetk python-numpy python-dicom gnuplot gnuplot-x11 libpython-dev python-dev libx11-dev tk-dev subversion
    else
	echo "It seems you don't have sudo privileges."
	exit
    fi
elif [ -n "$(which yum)" ]
then
if sudo yum update
then
    sudo yum -y install gcc gnuplot gfortran emacs xterm compat-gcc-34 compat-libf2c-34 libc.so.6 libg2c.so.0 libgcc_s.so.1 libm.so.6 tkinter perl-macros.x86_64 auto-buildrequires.x86_64 python-devel tk-devel tcl-devel gcc gnuplot emacs gfortran xterm gcc-c++ python-imaging python-imaging-tk compat-gcc-34-g77 glibc-devel.i686 glibc-devel.x86_64
    else
	echo "It seems you don't have sudo privileges."
	exit
    fi
else
    echo "Packege manager not recognized."
    exit
fi

if [ -z ${FLUPRO+x} ]
then echo "FLUPRO and other Fluka vars are not setted, setting them"
    
    export FLUFOR=gfortran    
    export FLUPRO=$HOME/fluka     
    export FLUKA=$FLUPRO     
    export PATH=$PATH:$HOME/flair
    alias ll='ls -ltrh'       
    
    
    
    echo "export FLUFOR=gfortran"         >> $HOME/.bashrc
    echo "export FLUPRO=$HOME/fluka"      >> $HOME/.bashrc
    echo "export FLUKA=\$FLUPRO"          >> $HOME/.bashrc
    echo "export PATH=\$PATH:$HOME/flair" >> $HOME/.bashrc
    echo "alias ll='ls -ltrh'"            >> $HOME/.bashrc
    
    source $HOME/.bashrc
    
    else
	echo "FLUPRO is set to '$FLUPRO' I'm not touching it"
    fi
    
    cd $HOME
    
    if [ -e "$FLUPRO/flukahp" ]
    then
	echo "Fluka is already installed. Skipping it"
    else
	if [ -e "$FLUPRO/fluka2011.2c-linux-gfor64bitAA.tar.gz" ]
	then
	    echo "Fluka already downloaded. Skipping the download"
	    cd fluka
	else
	    mkdir fluka
	    cd fluka
	    wget --user $username  --password $pswd https://www.fluka.org/packages/fluka2011.2c-linux-gfor64bitAA.tar.gz
	fi
	
	if tar -zxvf fluka2011.2c-linux-gfor64bitAA.tar.gz
	then
	    echo "Fluka correctly decompressed"
	else
	    echo "Fluka file corrupted. Trying to download it again"
	    rm -f fluka2011.2c-linux-gfor64bitAA.tar.gz
	    wget --user $username  --password $pswd https://www.fluka.org/packages/fluka2011.2c-linux-gfor64bitAA.tar.gz
	fi
	
	if make -f makefile
	then
            echo "FLUKA has been installed."
	    FLUKAOK=1
	else
	    echo "check the FLUKA installation"
	fi
    fi
    
    cd $HOME
    
    if [ -e "$HOME/flair/geoviewer.so" ]
    then
	echo "Flair is already installed. Skipping it"
    else
	
	mkdir flair
	wget http://www.fluka.org/flair/install.sh
	chmod +x install.sh
	if ./install.sh install flair
	then
	    echo "Flair has been installed."
	    FLAIROK=1
	else
	    echo "check the Flair installation"
	fi
	
	cd $HOME
	rm -f $HOME/install.sh
	
    fi
    
#    rm -f $HOME/installflukaflair.sh
    
    
if [ "$FLUKAOK" -eq "1" ]
then
    echo "FLUKA has been installed."
else
    echo "check the FLUKA installation"
fi
    

if [ "$FLAIROK" -eq "1" ]
then
    echo "Flair has been installed."
else
    echo "check the Flair installation"
fi
    
if [ -e "$HOME/fluka/flukahp" ]
then
    echo "flukahp correctly created"
else
    echo "flukahp has NOT been created. Trying again..."
    cd $HOME/fluka/
    make -f makefile || exit
fi


echo "checking the installation"
cd $HOME
wget http://www.fluka.org/content/tools/check_fluka.tgz
tar xzvf check_fluka.tgz
cd check_fluka
/bin/bash check_fluka.sh
