#!/bin/bash

# Defaults:
MIRROR=http://fs.paratools.com/mpc/contrib/apps/pastix
CONC=12
cc=mpc_cc
FRT=mpc_gfortran
FRTLIB=-lgfortran
MCXX=mpc_cxx

for arg in "$@"; do
  case $arg in
    --help | -help)
      echo "$0 utility"
      echo "******************"
      echo "Usage: $0 [--prefix=<dir>] [-j=<concurrency>] [--mirror=[1|2] [--help] [--cc=mpc_cc(default)|mpc_icc|mpicc|mpiicc]"
      echo "--prefix=<dir> specifies the target installation directory."
      echo "if unspecified, installmpc will install the components in the current working directory"
      echo "--mirror=<mirror_id> specifies the location to download the software."
      echo "--mirror=1 (default): http://fs.paratools.com/mpc/contrib/apps/pastix"
      echo "--mirror=2 uses http://static.paratools.com/mpc/contrib/apps/pastix"
      echo "--mirror=3 uses ftp://fs.paratools.com/mpc/contrib/apps/pastix"
      echo "--mirror=4 uses ftp://paratools08.rrp.net/mpc/contrib/apps/pastix"
      echo "-j=<concurrency> specifies the level of concurrency with which the make -j <concurrency> command is invoked"
      echo "--mpc_configure_args='<args>' specifies additional MPC parameters passed to configure"
      echo "For any questions, please e-mail info@paratools.com"
      echo "or refer to the MPC webpage: http://paratools.fr/mpc"
      echo "******************"
      exit 0
      ;;

     --prefix=*) 
       PKG=`echo $arg | sed -e 's/--prefix=//'`
       echo "NOTE: $0: Installing software in $PKG"
       shift
       ;;

     -prefix=*) 
       PKG=`echo $arg | sed -e 's/-prefix=//'`
       echo "NOTE: $0: Installing software in $PKG"
       shift
       ;;

     -j=*)
       CONC=`echo $arg | sed -e 's/-j=//'`
       shift
       ;;

     --j=*)
       CONC=`echo $arg | sed -e 's/--j=//'`
       shift
       ;;

     --cc=*) 
       cc=`echo $arg | sed -e 's/--cc=//'`
       case $cc in
	 mpc_cc) 
		echo "Using MPC GNU mpc_cc as the C compiler."
		FRT=mpc_gfortran
		MCXX=mpc_cxx
		FRTLIB=-lgfortran
		;;
	 mpc_icc) 
		echo "Using MPC Intel mpc_icc as the C compiler."
		FRT=mpc_ifort
		MCXX=mpc_icpc
		FRTLIB=-lifcore
		;;
	 mpicc) 
		echo "Using MPI mpicc as the C compiler."
		FRT=mpif90
		MCXX=mpicxx
		FRTLIB=-lgfortran
		;;
	 mpiicc) 
		echo "Using MPI Intel mpiicc as the C compiler."
		FRT=mpiifort
		MCXX=mpiicpc
		FRTLIB=-lifcore
		;;
         *)
		echo "Using MPC GNU mpc_cc (default) as the C compiler."
		;;
         esac
         shift
         ;;
     -cc=*) 
       cc=`echo $arg | sed -e 's/-cc=//'`
       case $cc in
	 mpc_cc) 
		echo "Using MPC GNU mpc_cc as the C compiler."
		FRT=mpc_gfortran
		MCXX=mpc_cxx
		FRTLIB=-lgfortran
		;;
	 mpc_icc) 
		echo "Using MPC Intel mpc_icc as the C compiler."
		FRT=mpc_ifort
		MCXX=mpc_icpc
		FRTLIB=-lifcore
		;;
	 mpicc) 
		echo "Using MPI mpicc as the C compiler."
		FRT=mpif90
		MCXX=mpicxx
		;;
	 mpiicc) 
		echo "Using MPI Intel mpiicc as the C compiler."
		FRT=mpiifort
		MCXX=mpiicpc
		FRTLIB=-lifcore
		;;
         *)
		echo "Using MPC GNU mpc_cc (default) as the C compiler."
		;;
         esac
         shift
         ;;

         
     --mirror=*) 
       mirrorid=`echo $arg | sed -e 's/--mirror=//'`
       case $mirrorid in
         1)
           MIRROR=http://fs.paratools.com/mpc/contrib/apps/pastix
           ;;
         2)
           MIRROR=http://static.paratools.com/mpc/contrib/apps/pastix
           ;;
         3)
           MIRROR=ftp://fs.paratools.com/mpc/contrib/apps/pastix
           ;;
         4)
           MIRROR=ftp://paratools08.rrp.net/mpc/contrib/apps/pastix
           ;;
         *)
           MIRROR=ftp://paratools08.rrp.net/mpc/contrib/apps/pastix
           ;;
       esac
       shift
       ;;

     esac
done
if [ $# = 0 ] ; then 
  echo "Default arg"
  PKG=$PWD
  echo "NOTE: $0: Installing MPC software in $PKG using MIRROR=${MIRROR}"
fi 

echo "$PWD> "


if [ ! -d tar ]; then
  wget ${MIRROR}/BLAS.tgz 
  wget ${MIRROR}/pastix_release_4694.tgz 
  wget ${MIRROR}/hwloc_patched_1.9.tgz
  wget ${MIRROR}/scotch_patched_6.0.0.tgz
  #wget ${MIRROR}/automake-1.14.1.tar.gz
  mkdir -p tar
  mv *gz tar
  echo "== Downloaded tarballs =="
  ls tar
fi

if [ ! -d src ]; then 
   mkdir -p src
fi

cd src
echo "== Entering $PWD =="

#echo "== Installing autoconf =="
#if [ ! -d automake-1.14.1 ]; then 
#  tar zxf ../tar/automake-1.14.1.tar.gz
#fi
#cd automake-1.14.1
#./configure --prefix=${PKG}/automake
#make clean
#make -j ${CONC}; make install
#cd ..

echo "== Installing hwloc_patched_1.9 =="
#export PATH=${PKG}/automake/bin:$PATH
if [ ! -d hwloc_patched_1.9 ]; then 
  tar zxf ../tar/hwloc_patched_1.9.tgz
fi

cd hwloc_patched_1.9
setcc=0
if [ "$cc" = "mpc_cc" ]; then
  export CC='mpc_cc -include mpc.h' 
  setcc=1
fi

if [ "$cc" = "mpc_icc" ]; then
  export CC='mpc_icc -include mpc.h' 
  setcc=1
fi

./configure --prefix=${PKG}/hwloc 
make; make install
cd ..
if [ $setcc = 1 ] ; then
  unset CC 
fi

echo "== Installing BLAS =="
if [ ! -d BLAS ]; then 
  tar zxf ../tar/BLAS.tgz
fi

cd BLAS
make clean
mkdir -p ${PKG}/blas
mkdir -p ${PKG}/blas/lib
make FORTRAN=${FRT} PREFIX=${PKG}/blas/lib -j ${CONC}; make FORTRAN=${FRT} PREFIX=${PKG}/blas/lib install 
cd ..

echo "== Installing Scotch_patched_6.0.0 =="
if [ ! -d scotch_patched_6.0.0 ]; then 
  tar zxf ../tar/scotch_patched_6.0.0.tgz
fi
cd scotch_patched_6.0.0/src
mkdir -p ${PKG}/scotch
make clean
make MPC_C_COMP=${cc} prefix=${PKG}/scotch -j ${CONC} scotch ptscotch install
cd ../..

echo "== Installing PASTIX release 4694 =="
if [ ! -d pastix_release_4694 ]; then 
  tar zxf ../tar/pastix_release_4694.tgz
fi
cd pastix_release_4694/src
make clean
make PREFIX=${PKG}/pastix -j ${CONC}  HWLOC_HOME=${PKG}/hwloc BLAS_HOME=${PKG}/blas/lib SCOTCH_HOME=${PKG}/scotch CCPROG=${cc} MPCCPROG=${cc} CFPROG=${FRT} MCFPROG=${FRT} MPCXXPROG=${MCXX} MCXXPROG=${MCXX} PASTIX_BIN=${PKG}/pastix PASTIX_INC=${PKG}/pastix PASTIX_LIB=${PKG}/pastix FORTRANLIB=${FRTLIB}
make PREFIX=${PKG}/pastix -j ${CONC}  HWLOC_HOME=${PKG}/hwloc BLAS_HOME=${PKG}/blas/lib SCOTCH_HOME=${PKG}/scotch  CCPROG=${cc} MPCCPROG=${cc} CFPROG=${FRT} MCFPROG=${FRT} MPCXXPROG=${MCXX} MCXXPROG=${MCXX} PASTIX_BIN=${PKG}/pastix PASTIX_INC=${PKG}/pastix PASTIX_LIB=${PKG}/pastix  FORTRANLIB=${FRTLIB} examples
cd example/src
make PREFIX=${PKG}/pastix HWLOC_HOME=${PKG}/hwloc BLAS_HOME=${PKG}/blas/lib SCOTCH_HOME=${PKG}/scotch CCPROG=${cc} MPCCPROG=${cc} CFPROG=${FRT} MCFPROG=${FRT} PASTIX_BIN=${PKG}/pastix PASTIX_INC=${PKG}/pastix PASTIX_LIB=${PKG}/pastix MPCXXPROG=${MCXX} MCXXPROG=${MCXX} FORTRANLIB=${FRTLIB}  install
cd ${PKG}/pastix/bin

if [ -x ./ssimple ]; then
  echo "== Trying to run ssimple example =="
  if [ $cc = mpc_cc  -o $cc = mpc_icc ]; then
    echo "mpcrun -n=4 -p=1 ./ssimple -lap 20000"
    mpcrun -n=4 -p=1 -net=tcp ./ssimple -lap 20000
    echo "mpcrun -n=4 -p=4 ./ssimple -lap 20000"
    mpcrun -n=4 -p=4  -net=tcp ./ssimple -lap 20000
  fi
  if [ $cc = mpicc  -o $cc = mpiicc ]; then
    mpirun -np 4 ./ssimple -lap 20000
  fi
fi
echo "== Done! =="
