#!/bin/sh

set -e

wgetcmd="wget"
wgetcount=`which wget 2>/dev/null | wc -l`
if test ! $wgetcount = 1; then
  echo "Utility wget not found in your PATH."
  if test `uname` = Darwin; then
    wgetcmd="curl -L -k -O"
    echo "Using ftp command instead."
  elif test `uname` = FreeBSD; then
    wgetcmd=fetch
    echo "Using fetch command instead."
  else
    exit -1
  fi
fi

coinblas=blas-20130815.tgz

echo " "
echo "Running script for downloading the source code for BLAS"
echo " "

rm -f blas.tgz

echo "Downloading the source code from projects.coin-or.org..."
if $wgetcmd http://www.coin-or.org/BuildTools/Blas/$coinblas ;
then
  mv $coinblas blas.tgz
else
  echo
  echo "Failed, try downloading the source code from www.netlib.org..."
  if ! $wgetcmd http://www.netlib.org/blas/blas.tgz ;
  then
    echo
    echo "Failed, try downloading the source code from netlib.sandia.gov..."
    if ! $wgetcmd http://netlib.sandia.gov/blas/blas.tgz ;
    then
      exit 1;
    fi
  fi
fi

echo "Uncompressing the tarball..."
gunzip -f blas.tgz

echo "Unpacking the source code..."
tar xf blas.tar

echo "Deleting the tar file..."
rm blas.tar

echo "Moving the source files from BLAS subdirectory"
mv BLAS/*.f .
rm -rf BLAS

echo " "
echo "Done downloading the source code for BLAS."
echo " "

