#!/bin/bash

start_dir=$(pwd)
rm -rf 3rdparty

mkdir -p 3rdparty/Polymath
git clone https://git.afach.de/samerafach/Polymath 3rdparty/Polymath
if [ $? -ne 0 ]; then
  echo 'Polymath repository cloning failed.'
  exit 1
fi

read -p "Would you like to get OpenBLAS, compile it, or should I expect it to be already in your system? [y/n]: " -n 1 -r
echo "Notice that a compiled version of OpenBLAS is faster because it will be accustomed for your CPU."
if [[ $REPLY =~ ^[Nn]$ ]]
then
echo "Skipped getting OpenBLAS..."
  exit 0
fi

mkdir -p 3rdparty/OpenBLAS
git clone https://github.com/xianyi/OpenBLAS.git 3rdparty/OpenBLAS
if [ $? -ne 0 ]; then
  echo 'Cloning OpenBLAS failed.'
  exit 1
fi

cd 3rdparty/OpenBLAS
make -j $(nproc)
if [ $? -ne 0 ]; then
  echo 'Building OpenBLAS failed.'
  exit 1
fi

#replace ln -fs with cp, to avoid linking problems with ntfs file systems
#sed -i 's/ln -fs/cp/g' Makefile
#sed -i 's/ln -fs/cp/g' Makefile.install

make PREFIX=${start_dir}/spintrum/OpenBLAS_install install
if [ $? -ne 0 ]; then
  echo 'OpenBLAS installation failed.'
  exit 1
fi

cd ${start_dir}
