#!/bin/bash

echo "This program will help you configure XSHELLS to run on this machine."

if [ ! -f "shtns/shtns.h" ]; then
	echo "****************************************************"
	echo "* the SHTns library seems missing, let's get it!"
	read -p "*** press ENTER to continue ***" yn
	git submodule init && git submodule update
	if [ ! -f "shtns/shtns.h" ]; then
		shtns_tgz="https://bitbucket.org/nschaeff/shtns/get/master.tar.gz"
		wget $shtns_tgz || curl -f -O $shtns_tgz
		mkdir -p shtns
		tar xvzf master.tar.gz --strip-components=1 -C shtns
		rm master.tar.gz
	fi
fi

### try to infer the package manager of your system:
os=$(uname)
if [ "$os" = "Darwin" ]; then
	pkgcmd="brew install gcc fftw"
else
	# assume Linux
    if [[ -f /etc/debian_version ]]; then
		pkgcmd="sudo apt-get -y install gcc g++ make libfftw3-dev"
    elif [[ -f /etc/redhat-release ]]; then
		pkgcmd="sudo yum -y install gcc-c++ fftw"
    elif [[ -f /etc/fedora-release ]]; then
		pkgcmd="sudo dnf -y install gcc-c++ fftw"
    elif [[ ! -z `which pacman` ]]; then
        pkgcmd="sudo pacman -S --needed gcc fftw"
    fi
fi

echo "****************************************************"
echo "* We try to simply run './configure'"
read -p "*** press ENTER to continue ***" yn

fftw_=fftw-3.3.10

./configure $*

while [ "$?" != "0" ]; do
	echo "********************************************************"
	echo "* There was an error. Look above for details, but usually a compiler is not found or FFTW is not found."
	if [ "x$pkgcmd" != "x" ]; then
		read -p "* press ENTER to try to install FFTW and g++, by running '$pkgcmd'"

		# run proposed install command:
		$pkgcmd
		pkgcmd=''   # delete so that next time we skip to something else!

		./configure $*
	else
		echo "* If it is a compiler error, you should try install the gnu compiler gcc/g++ (recommended)"
		echo "* or whatever c/c++ compiler you wish, and then retry."
		echo "* If it is FFTW that is missing, you can either use intel MKL instead (if you have it on your system)"
		echo "* or let me download and/or configure FFTW for you." 
		echo "* What do you wish to do?"
		read -p "* press ENTER to install FFTW, 'm' to try to use MKL, or 'q' to quit and install the missing piece yourself: " fmq
		if [ "x$fmq" == "xm" ]; then
			echo "let's try with MKL, by running './configure --enable-mkl'";
			read -p "press ENTER to continue" yn
			./configure $* --enable-mkl
		elif [ "x$fmq" == "xq" ]; then 
			echo "You may want to try the following command to install missing libraries/compilers, if not already done:"
			echo "  $pkgcmd"
			echo "bye!"
			exit
		else
			echo "let's go, and try to install FFTW"
			if [ ! -f "${fftw_}.tar.gz" ]; then
				echo "let's try to fetch it from the internet"
				wget http://www.fftw.org/${fftw_}.tar.gz || curl -f -O http://www.fftw.org/${fftw_}.tar.gz;
				if [ "$?" != "0" ]; then
					echo "Not able to download FFTW. Please go to http://www.fftw.org/ and do it yourself,"
						echo "put the file in this directory, and rerun $0"
					exit
				fi
			fi
			tar xvzf ${fftw_}.tar.gz
			prefix=`pwd`
			pushd $fftw_
			opts="--enable-openmp --disable-fortran --prefix=$prefix"
			## look if your cpu has AVX/AVX2
			lscpu | grep avx2 || sysctl -a | grep machdep.cpu | grep AVX2
			if [ "$?" == "0" ]; then
				echo "apparently your CPU has avx2 instructions, let's try to use them!"
				opts="$opts --enable-avx2"
			else
				lscpu | grep avx || sysctl -a | grep machdep.cpu | grep AVX
				if [ "$?" == "0" ]; then
					echo "apparently your CPU has avx instructions, let's try to use them!"
					opts="$opts --enable-avx"
				fi
			fi
			echo "*** let's configure and make FFTW with: './configure $opts && make -j8 && make install' ***"
			read -p "*** press ENTER to continue (this may take a while) ***" yn
			./configure $opts && make -j8 && make install
			res=$?
			popd
			if [ "$res" == "0" ]; then
				echo "*** Apparently, FFTW has been compiled OK! Let's go on with shtns and xshells ! ***"
				read -p "*** press ENTER to continue ***" yn
				curdir=`pwd`
				./configure $* --prefix=$prefix
			fi
		fi
	fi
done

echo
echo ./configure seems to have succeeded!
echo

##### PYTHON MODULES #####
read -p "While we are at it, would you like to install the python modules to work with simulation output? (y/n)" yn
if [ "x$yn" == "xy" ]; then
	cur_dir=`pwd`
	# try to install from pypi repository (requires internet access -- may not work on supercomputers)
	pip install pyxshells
	if [ "x$?" != "x0" ]; then
		# local install, without using pip (no internet required)
		cd shtns && python setup.py install --user && cd ../python && python setup.py install --user
	fi
	if [ "x$?" != "x0" ]; then
		echo
		echo "******************************************"
		echo Sorry, installing the python modules failed.
		echo Yon can retry by running: "'pip install pyxshells'"
		echo "'xsplot' can also be run without installing, e.g.: 'python python/xsplot.py energy.bench'"
	fi
	cd $cur_dir
fi

echo
echo "******************************************"
echo "You might want to run the tests (usually takes several minutes) with: 'make test'"
echo
echo You must now choose a problem to solve.
echo Example problems are located in the "'problems'" directory.
echo For instance, to build executables for the geodynamo benchmark and run it:
echo
echo "       cp problems/geodynamo/* ."
echo "       make xsbig"
echo "       ./xsbig"
echo
echo Before runnning, you can also adjust the parameters found in the "'xshells.par'" file, which will be read by program at startup.
echo Depending on your machine and what you want, you can also make and run other executables:
echo ./xsbig for the simple OpenMP version
echo ./xsbig_mpi for the pure MPI code
echo ./xsbig_hyb for the hybrid OpenMP-MPI executable.
echo ./xsbig_hyb2 for the radial MPI + in-shell OpenMP executable.
echo
echo Read the user manual for more details: https://nschaeff.bitbucket.io/xshells/manual.html
