#!/bin/bash
# Note you must have git, cmake, and gfortran installed
# currently this installed only works with the bash shell
#   and the zsh shell
# This script has been tested on Ubuntu and Arch linux
#  CentOS almost works? But there seems to be some problem
#  with dynamic/static libries on that OS (something to do
#  with the fPIC flag). That problem does not show up in
#  the other linux distros I have worked with and I have 
#  yet to track down what the root cause is. 
if [ "$SHELL" == "/bin/zsh" ]; then
	PROFILE_PATH="$HOME/.zshrc"
elif [ "$SHELL" == "/bin/bash" ]; then
	PROFILE_PATH="$HOME/.bashrc"
fi

echo "Profile: $PROFILE_PATH"

case `egrep -q "^export DSEPRoot" $PROFILE_PATH > /dev/null; echo $?` in
	0)
		echo "DSEPRoot Enviromental Variable Found"
		;;
	1)
		echo "DSEPRoot Enviromental Variable Not Set!"
		echo "Do you wish to set the DSEPRoot Enviromental Variable?"
		select yn in Yes No
		do
			case $yn in
				Yes )
				   read -p "Enter DSEPRoot: " DSEPRoot
			   	   [ ! -d "$DSEPRoot" ] && mkdir -p $DSEPRoot
				   if [ -d "$DSEPRoot" ]
				   then
					   echo "DSEPRoot Directory Created!"
					   echo "export DSEPRoot=$DSEPRoot" >> $PROFILE_PATH
				   else
					   echo "Error! Unable to create directory $DSEPRoot, exiting"
					   exit 1
				   fi
				   break
		  		   ;;
				No )
		 		   echo "DSEP Cannot be installed without the DSEPRoot variable set"
	   		       echo "Exiting, please export the variable in your profile and try the installer again"
				   exit 1
				   ;;
		   esac
	   done
esac	   

LAPACK_PREFIX=$HOME/.local/lapack
FREE_EOS_PREFIX=$HOME/.local/free_eos
echo "Moving Into $DSEPRoot"

cd $DSEPRoot

# Clone the DSEP Repositories
git clone git@github.com:tboudreaux/dsepX.git
git clone git@github.com:tboudreaux/DSEPmisc.git
git clone git@github.com:tboudreaux/DSEPDoc.git

# Clone LAPACK
git clone https://github.com/Reference-LAPACK/lapack.git

# install lapack
cd lapack
mkdir build
cd build
cmake -DCMAKE_INSTALL_LIBDIR=$LAPACK_PREFIX ..

case `man cmake | grep -q "\-j" > /dev/null; echo $?` in
	0)
		echo "Compuling lapack on multiple cores"
		cmake --build . -j --target install
		;;
	1)
		echo "Unable to compile on multiple cores, falling back to compiling lapack on a single core."
		cmake --build . --target install
		;;
esac

case `egrep -q "^export CMAKE_LIBRARY_PATH=$LAPACK_PREFIX" $PROFILE_PATH > /dev/null; echo $?` in
	0)
		echo "CMAKE_LIBRARY_PATH already pointing to lapack install directory"
		;;
	1)
		echo "Adding lapack install directory to CMAKE_LIBRARY_PATH"
		echo "# Auto added CMAKE_LIBRARY_PATH by DSEP installer" >> $PROFILE_PATH
		echo "export CMAKE_LIBRARY_PATH=$LAPACK_PREFIX:\$CMAKE_LIBRARY_PATH" >> $PROFILE_PATH
		export CMAKE_LIBRARY_PATH=$LAPACK_PREFIX:$CMAKE_LIBRARY_PATH
		;;
esac

cd $DSEPRoot
cd DSEPmisc/libs/free_eos-2.2.1_nonIntel
[ -d "build" ] && rm -r build
mkdir build
cd build

GCCVERSION=$(gcc --version | grep ^gcc | sed 's/^.* //g')
GCCMAJORVERSION=$(echo $GCCVERSION | awk '{split($0,a,".");print a[1]}')
if [ $GCCMAJORVERSION -lt 10 ]
	then
		export FC="gfortran"
	else
		export FC="gfortran -fallow-argument-mismatch"
fi

cmake -DCMAKE_INSTALL_PREFIX=$FREE_EOS_PREFIX -DCMAKE_VERBOSE_MAKEFILE=ON ..
make 
make install

export LD_LIBRARY_PATH="$FREE_EOS_PREFIX/lib:$LD_LIBRARY_PATH"
echo "export LD_LIBRARY_PATH=\"$FREE_EOS_PREFIX/lib:\$LD_LIBRARY_PATH\"" >> $PROFILE_PATH

cd $DSEPRoot
cd dsepX
make
make install
