#!/usr/bin/env bash
#Eric David Bosne
#ericbosne@ua.pt
#14/03/2013
#update 08/09/2015, pjms

#change the following line for different version
export FLUXVERSION=flux7.9.2

#Installing according to the tutorial in
#http://pjms.nl/FLUXBIN/flux7.9.install.html

#There are no safety nets on this program and if you already have tried to
#install another flux this can mess things up in the ~/.profile as it adds
#all the necessary lines regardless if they are already there.
#A manual clean up can be done for these cases.

#To run this you just need to type in the folder where you put the
#installation file
#chmod +x Install_FLUX_2_Ubuntu.bash
#./Install_FLUX_2_Ubuntu.bash

#then insert your password and when its finished type "y" for reboot.

#It should work well after that.

##-----Obtaining gcc and gfortran and other preparations-----
sudo apt-get update
sudo apt-get --yes install gcc
sudo apt-get --yes install gfortran 
sudo apt-get --yes install xorg-dev 
sudo apt-get --yes install wget 

#-----Downloading FLUX (version $FLUXVERSION) -----
mkdir ~/Downloads/FLUXBIN
cd ~/Downloads/FLUXBIN
wget -r -np -nH --cut-dirs=1 --reject "index.html*,contrib" http://pjms.nl/FLUXBIN/
#Download a 
#cd ~/Downloads/FLUXBIN
#tar -jxvf flux7.9.1.tar.bz2
#mv flux7.9.1 ~/

cd
tar -jxvf ~/Downloads/FLUXBIN/${FLUXVERSION}.tar.bz2

#Setting up environment variables
echo '#FLUX' | tee -a ~/.profile
echo 'export PGPLOT_DIR=${HOME}/PGPLOT' | tee -a ~/.profile
echo 'export PGPLOT_DEV=/xwin' | tee -a ~/.profile
echo 'export PGPLOT_BACKGROUND=white' | tee -a ~/.profile
echo 'export PGPLOT_FOREGROUND=black' | tee -a ~/.profile
echo 'export PGPLOT_FOREGROUND' | tee -a ~/.profile
echo 'export PGPLOT_XW_WIDTH=0.6' | tee -a ~/.profile
echo "export FLUX=\${HOME}/${FLUXVERSION}/FLUX7" | tee -a ~/.profile
echo 'PATH=.:$FLUX/BIN:$PATH' | tee -a ~/.profile
source ~/.profile

#-----Installing PGPLOT-----
#Download and unpack
cd ~/Downloads
#use one of the following two. The downloaded files are identical
#wget ftp://ftp.astro.caltech.edu/pub/pgplot/pgplot5.2.tar.gz #ftp not working
wget http://mirror.alfredstate.edu/ubuntu/pool/multiverse/p/pgplot5/pgplot5_5.2.2.orig.tar.gz
cd ~/
tar -zxvf ~/Downloads/pgplot5*tar.gz
cd pgplot
#Copy the 2 PostScript files of FLUX
cd drivers
mv psdriv.f psdriv.f.orig
cp -a $FLUX/PGPLOT.PSDRIV/psdriv.f .
cd ../src
mv grtext.f grtext.f.orig
cp -a $FLUX/PGPLOT.PSDRIV/grtext.f .
#Make destination directory PGPLOT. 
cd ~
mkdir PGPLOT
cd PGPLOT
#copy drivers.list to the destination dir, and edit 
cp ~/pgplot/drivers.list .

#Uncoment the necessary drivers from drivers.list
cd ~/PGPLOT
mv drivers.list drivers.list.orig
touch drivers.list
while read -r a b; do
if [[ "$b" = PSDRIV* ]] || [[ "$b" = XWDRIV* ]]
then echo $b >> drivers.list
else
echo $a $b >> drivers.list
fi
done <drivers.list.orig

#Run makemake and edit the makefile
~/pgplot/makemake ~/pgplot linux g77_gcc_aout

mv makefile makefile.orig

old_IFS=$IFS      # save the field separator           
IFS=$'\n'     # new field separator, the end of line   
touch makefile
while read -r a; do
if [[ "$a" = "FCOMPL=g77" ]]
then echo "FCOMPL=gfortran" >> makefile
elif [[ "$a" = "FFLAGC=-Wall -O" ]]
then echo "FFLAGC=-Wall -O -fno-backslash" >> makefile
else
echo $a >> makefile
fi
done <makefile.orig
IFS=$old_IFS     # restore default field separator 

#Run make for PGPlot
make
make cpg
make clean

#Run make for FLUX
cd $FLUX

makemake makedefs.linux
make clean
make

#!/usr/bin/env bash
echo 'You need to restart your computer in order to use FLUX!'
read -p "Do you wich to restart now (N/y)? " RS
if [[ $RS = "y" ]] || [[ $RS = "Y" ]]
then sudo shutdown now -r
fi
