#!/bin/sh 

#Variables
pdt=no
pdtcomp=no
dyninst=no
arch=no
archdir=none
cc=no
cxx=no
fortran=no
mpiopts=
mpi=no
perf=no
perfincdir=
perflibdir=
perflibrary=
mpiinc=no
mpilib=no
tag=no
tagopt=
pdtcxxopt=
pdtcxxarg=
vtf=no
otf=no
vampirtrace=no
vptopt=
vtfopt=
slogopt=
useropt=no
papi=no
opari=no
epilog=no
epilogopt=
mpilibrary=no
tauprefix=unknown
tauexecprefix=unknown
extraopts=

###############################################################
# This method will escape spaces so that you can use ./configure `cat .last_config` when you've used them
for arg in "$@"; do
    # Thanks to Bernd Mohr for the following that handles quotes and spaces
    # -e 's/^x//'          removes the x at the start
    # -e 's/"/\\\"/g'      replaces " with \"
    # -e s,\',%@%\',g      replaces ' with %@%'
    # -e 's/%@%/\\\/g'     replaces %@% \
    # -e 's/ /\\\ /g'      replaces space with \space
    modarg=`echo "x$arg" | sed -e 's/^x//' -e 's/"/\\\"/g' -e s,\',%@%\',g -e 's/%@%/\\\/g' -e 's/ /\\\ /g'`
    modargs="$modargs $modarg"
done
echo "$modargs" >> .installflags

###############################################################
# PARSE COMMAND LINE SWITCHES
###############################################################
for arg in "$@";
do
  case $arg in

  -arch=*)
    arch=yes
    archdir=`echo $arg | sed -e 's/-arch=//'`
    shift
    ;;

  -fortran=*)
    fortran=yes
    fortranspec=`echo $arg | sed -e 's/-fortran=//'`
    shift
    ;;

  -cc=*)
    cc=yes
    ccspec=`echo $arg | sed -e 's/-cc=//'`
    shift
    ;;

  -c++=*)
    cxx=yes
    cxxspec=`echo $arg | sed -e 's/-c++=//'`
    shift
    ;;

  -useropt=*)
    useropt=yes
    useroptspec=`echo $arg | sed -e 's/-useropt=//' -e 's/ /#/g'`
    shift
    ;;

  -papi=*)
    papi=yes
    papidir=`echo $arg | sed -e 's/-papi=//'`
    shift
    ;;

  -perfinc=*)
      perf=yes
      perfincdir=`echo $arg | sed -e 's/-perfinc=//'`
      if [ ! -d $perfincdir ]
      then
        echo "Error: Cannot access PERF include directory $perfincdir"
        exit 1
      fi
      perfopts="$perfopts $arg"
    shift
    ;;

  -perflib=*)
      perf=yes
      perflibdir=`echo $arg | sed -e 's/-perflib=//'`
      if [ ! -d $perflibdir ]
      then
        echo "Error: Cannot access PERF lib directory $perflibdir"
        exit 1
      fi
      perfopts="$perfopts $arg"
    shift
    ;;

  -perflibrary=*)
      perf=yes
      perflibrary=`echo $arg | sed -e 's/-perflibrary=//' -e 's/ /#/g'`
      perfopts="$perfopts -perflibrary=$perflibrary"
    shift
    ;;


  -dyninst=*)
    dyninst=yes
    dyninstdir=`echo $arg | sed -e 's/-dyninst=//'`
    shift
    ;;

  -opari*)
    opari=yes
    openmp=yes
    shift
    ;;

  -epilog*)
    epilog=yes
    epilogopt="$epilogopt $arg"
    shift
    ;;

  -mpiinc=*)
    mpiinc=yes
    mpiincdir=`echo $arg | sed -e 's/-mpiinc=//' -e 's/ /#/g'`
    shift
    ;;

  -mpilib=*)
    mpilib=yes
    mpilibdir=`echo $arg | sed -e 's/-mpilib=//'`
    shift
    ;;

  -mpilibrary=*)
    mpilibrary=yes
    mpilibraryspec=`echo $arg | sed -e 's/-mpilibrary=//' -e 's/ /#/g'`
    mpiopt=-mpilibrary="$mpilibraryspec"
    shift
    ;;

  -mpi)
    mpi=yes
    shift
    ;;

  -nocomm)
    commopt=-nocomm
    shift
    ;;

  -slog2)
    slogopt=-slog2
    shift
    ;;

  -slog2=*)
    slogdir=`echo $arg | sed -e 's/-slog2=//'`
    slogopt=-slog2=$slogdir
    shift
    ;;

  -pdt=*)
    pdt=yes
    pdtdir=`echo $arg | sed -e 's/-pdt=//'`
    shift
    ;;

  -pdt_c++=*)
    pdtcxx=yes
    pdtcxxarg=`echo $arg | sed -e 's/-pdt_c++=//'`
    pdtcxxopt=-pdt_c++=$pdtcxxarg
    shift
    ;;

  -tag=*)
    tag=yes
    tautag=`echo $arg | sed -e 's/-tag=//'`
    tagopt=-tag=$tautag
    shift
    ;;

  -pdtcompdir=*)
    pdtcomp=yes
    pdtcompdir=`echo $arg | sed -e 's/-pdtcompdir=//'`
    shift
    ;;

  -prefix=*)
    tauprefix=`echo $arg | sed -e 's/-prefix=//' -e 's/ /_/g'`
    shift
    ;;

  -exec-prefix=*)
    tauexecprefix=`echo $arg | sed -e 's/-exec-prefix=//' -e 's/ /_/g'`
    shift
    ;;

  -vtf=*)
    vtf=yes
    vtfdir=`echo $arg | sed -e 's/-vtf=//'`
    vtfopt=-vtf=$vtfdir
    shift
    ;;

  -vampirtrace*)
    vampirtrace=yes
    vptopt="$vptopt $arg"
    shift
    ;;

  -otf=*)
    otf=yes
    otfdir=`echo $arg | sed -e 's/-otf=//'`
    otfopt=-otf=$otfdir
    shift
    ;;

  -help)
    echo "TAU Configuration Utility "
    echo "***********************************************************************"
    echo "Usage: installtau [OPTIONS]"
    echo "  where [OPTIONS] are:"
    echo "-arch=<arch>  "
    echo "-fortran=<compiler>  "
    echo "-cc=<compiler>   "
    echo "-c++=<compiler>   "
    echo "-useropt=<options>  "
    echo "-pdt=<pdtdir>  "
    echo "-pdtcompdir=<compdir>  "
    echo "-pdt_c++=<C++ Compiler>  "
    echo "-papi=<papidir>  "
    echo "-vtf=<vtfdir>  "
    echo "-otf=<otfdir>  "
    echo "-slog2=<dir> (for external slog2 dir)"
    echo "-slog2 (for using slog2 bundled with TAU)"
    echo "-dyninst=<dyninstdir>  "
    echo "-mpiinc=<mpiincdir>  "
    echo "-mpilib=<mpilibdir>  "
    echo "-mpilibrary=<mpilibrary>  "
    echo "-perfinc=<dir> "
    echo "-perflib=<dir> "
    echo "-perflibrary=<library> "
    echo "-mpi"
    echo "-tag=<unique name> "
    echo "-nocomm"
    echo "-opari  "
    echo "-epilog=<epilogdir>  "
    echo "-epiloginc=<absolute path to epilog include dir> (<epilog>/include default) "
    echo "-epilogbin=<absolute path to epilog bin dir> (<epilog>/bin default)  "
    echo "-epiloglib=<absolute path to epilog lib dir> (<epilog>/lib default)  "
    echo "-prefix=<dir>  "
    echo "-exec-prefix=<dir>  "
    shift
    exit
    ;;

  *)
    echo "WARNING: Command line switch \`$arg' not recognized, passing to configure" 1>&2
    extraopts="$extraopts $arg"
    ;;
  esac
done

# Take care of MPI options
if [ $mpi = yes ]
then
  mpiopts="$mpiopts -mpi"
fi

if [ $mpiinc = yes ]
then
  mpiopts="$mpiopts -mpiinc=$mpiincdir"
fi

if [ $mpilib = yes ]
then
  mpiopts="$mpiopts -mpilib=$mpilibdir"
fi

if [ $mpilibrary = yes ]
then
  mpiopts="$mpiopts -mpilibrary=$mpilibraryspec"
fi


# Add tauprefix to tagopt $extraopts
if [ $tauprefix != unknown ]
then
  tagopt="$tagopt $extraopts -prefix=$tauprefix"
fi

# Add exec-prefix to tagopt $extraopts
if [ $tauexecprefix != unknown ]
then
  tagopt="$tagopt $extraopts -exec-prefix=$tauexecprefix"
fi

# Take care of wish version prompt
echo " " > wishver

# Now we compose the arguments
  if [ $pdt = yes ]
  then
    pdtopt="-pdt=$pdtdir $pdtcxxopt"
    if [ $pdtcomp = yes ]
    then
      pdtopt="-pdt=$pdtdir $pdtcxxopt -pdtcompdir=$pdtcompdir"
    fi
  fi

  if [ $arch = yes ]
  then
    archopt="-arch=$archdir"
  fi

  if [ $fortran = yes ]
  then
    fortrancompiler="-fortran=$fortranspec"
  fi

  if [ $cc = yes ]
  then
    ccompiler="-cc=$ccspec"
  fi

  if [ $cxx = yes ]
  then
    ccompiler="$ccompiler -c++=$cxxspec"
  fi

  if [ $useropt = yes ]
  then
    useropts="-useropt=$useroptspec"
  fi

# Configure TAU
  if [ "x$mpiopts" != "x" ]
  then
# Configure all versions with MPI
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts $commopt <wishver
    make clean; make install
    if [ "x$commopt" != "x" ]
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts -nocomm"
      ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts -nocomm <wishver
      make clean; make install
    fi

# configure with perflib  using perfopts
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt $perfopts"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts $commopt  $perfopts <wishver
    make clean; make install

# Add callpath profiling 
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts -PROFILECALLPATH"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts -PROFILECALLPATH <wishver
    make clean; make install

# Add callpath profiling with compensate
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts -PROFILECALLPATH -COMPENSATE"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts -PROFILECALLPATH -COMPENSATE <wishver
    make clean; make install
  
# Add depth limit
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts -DEPTHLIMIT"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts -DEPTHLIMIT <wishver
    make clean; make install

# Add COMPENSATE
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts -COMPENSATE"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts -COMPENSATE <wishver
    make clean; make install

# Add TRACE
    if [ $epilog = yes -a $papi = yes ]
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vptopt $vtfopt $otfopt $slogopt $epilogopt -papi=$papidir"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE  $vptopt $vtfopt $otfopt $slogopt $epilogopt -papi=$papidir<wishver
      make clean; make install
    fi
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vptopt $vtfopt $otfopt $slogopt"
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE  $vptopt $vtfopt $otfopt  $slogopt <wishver
    make clean; make install
  
    if [ $papi = yes ]
    then
# Add PAPI
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS <wishver
      make clean; make install
# Add -TRACE -papi multiplecounters to it
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $otfopt $slogopt -papi=$papidir -MULTIPLECOUNTERS"
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $otfopt $slogopt -papi=$papidir -MULTIPLECOUNTERS <wishver 
      make clean; make install
      if [ $vampirtrace = yes ]
      then 
# No need to have -MULTIPLECOUNTERS with Vampirtrace
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $otfopt $slogopt -papi=$papidir"
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt $vptopt $vtfopt $otfopt $slogopt -papi=$papidir <wishver 
      fi
# Add -PROFILECALLPATH -papi multiplecounters to it
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS -PROFILEPHASE"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS -PROFILEPHASE <wishver
      make clean; make install

# Add -COMPENSATE -PROFILECALLPATH -papi multiplecounters to it
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS -PROFILEPHASE -COMPENSATE"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS -PROFILEPHASE -COMPENSATE <wishver
      make clean; make install

    fi

    if [ $opari = yes ]
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -opari"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -opari -openmp <wishver
      make clean; make install
    fi 

    if [ $opari = yes -a $papi = yes ] 
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -opari -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -opari -openmp -papi=$papidir -MULTIPLECOUNTERS <wishver
      make clean; make install
    fi 
  fi
# MPI options are over

# Serial library with pdt
  if [ $pdt = yes ]
  then
    echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt "
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt <wishver
    make clean; make install
  fi

  if [ $opari = yes ]
  then
    echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -opari -openmp "
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -opari -openmp <wishver
    make clean; make install

    if [ $papi = yes ]
    then 
      echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -opari -openmp -papi=$papidir -MULTIPLECOUNTERS "
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -opari -openmp -papi=$papidir -MULTIPLECOUNTERS <wishver
      make clean; make install
    fi
  fi 

  if [ $arch = no -o $archdir != bgl -a $archdir != xt3 ]
  then
# Just Pthread
    echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -pthread "
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -pthread <wishver
    make clean; make install
  fi
  
# Just Sequential
  echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt "
  ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt <wishver
  make clean; make install

  if [ $papi = yes ]
  then 
# PAPI with PTHREAD 
    if [ $arch = no -o $archdir != bgl -a $archdir != xt3 ]
    then
      echo "Configuring with -pthread -papi=$papidir -MULTIPLECOUNTERS "
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -pthread -papi=$papidir -MULTIPLECOUNTERS <wishver
      make clean; make install
    fi

# PAPI with -pdt 
    if [ $pdt = yes ]
    then
      echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt  -papi=$papidir -MULTIPLECOUNTERS -iowrapper <wishver
      make clean; make install
    fi
  fi
  if [ $dyninst = yes ] 
  then
    echo "Configuring with $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt -dyninst=$dyninstdir "
    ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt -dyninst=$dyninstdir < wishver
    make clean; make install
  fi

# One final library to build with -PROFILEPARAM and MPI!
  if [ "x$mpiopts" != "x" ]
  then
# Configuring with -PROFILEPARAM
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $extraopts $ccompiler $archopt $pdtopt $mpiopts $commopt -PROFILEPARAM"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $extraopts $archopt $pdtopt $mpiopts $commopt <wishver
    make clean; make install
  fi


# Set file permissions
  chmod -R go+rX *


  

