#!/bin/sh 

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

# 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=//'`
    shift
    ;;

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

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

  -opari=*)
    opari=yes
    oparidir=`echo $arg | sed -e 's/-opari=//'`
    openmp=yes
    shift
    ;;

  -epilog=*)
    epilog=yes
    epilogdir=`echo $arg | sed -e 's/-epilog=//'`
    epilogopt=-epilog=$epilogdir
    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=//'`
    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
    ;;

  -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 "-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 "-mpi"
    echo "-tag=<unique name> "
    echo "-nocomm"
    echo "-opari=<oparidir>  "
    echo "-epilog=<epilogdir>  "
    echo "-prefix=<dir>  "
    echo "-exec-prefix=<dir>  "
    shift
    exit
    ;;

  *)
    echo "ERROR: Command line switch \`$arg' not recognized" 1>&2
    exit 1
    ;;
  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
if [ $tauprefix != unknown ]
then
  tagopt="$tagopt -prefix=$tauprefix"
fi

# Add exec-prefix to tagopt
if [ $tauexecprefix != unknown ]
then
  tagopt="$tagopt -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 $ccompiler $archopt $pdtopt $mpiopts $commopt"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $archopt $pdtopt $mpiopts $commopt <wishver
    make clean; make install
    if [ "x$commopt" != "x" ]
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts -nocomm"
      ./configure $fortrancompiler $ccompiler $useropts $tagopt $archopt $pdtopt $mpiopts -nocomm <wishver
      make clean; make install
    fi

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

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

# Add TRACE
    if [ $epilog = yes ]
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $slogopt $epilogopt"
      ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE  $vtfopt  $slogopt $epilogopt<wishver
      make clean; make install
    fi
    echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $slogopt"
    ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE  $vtfopt  $slogopt <wishver
    make clean; make install
  
    if [ $papi = yes ]
    then
# Add PAPI
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $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 $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE $vtfopt $slogopt -papi=$papidir -MULTIPLECOUNTERS"
    ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -TRACE  $vtfopt $slogopt -papi=$papidir -MULTIPLECOUNTERS <wishver 
      make clean; make install
    fi

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

    if [ $opari = yes -a $papi = yes ] 
    then
      echo "Configuring with ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -opari=$oparidir -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt $mpiopts $commopt -opari=$oparidir -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 $ccompiler $archopt $pdtopt "
    ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt <wishver
    make clean; make install
  fi

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

    if [ $papi = yes ]
    then 
      echo "Configuring with $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt -opari=$oparidir -openmp -papi=$papidir -MULTIPLECOUNTERS "
      ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt -opari=$oparidir -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 $ccompiler $archopt $pdtopt -pthread "
    ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt -pthread <wishver
    make clean; make install
  fi
  
# Just Sequential
  echo "Configuring with $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt "
  ./configure $fortrancompiler $useropts $tagopt $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 $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 $ccompiler $archopt $pdtopt -papi=$papidir -MULTIPLECOUNTERS"
      ./configure $fortrancompiler $useropts $tagopt $ccompiler $archopt $pdtopt  -papi=$papidir -MULTIPLECOUNTERS <wishver
      make clean; make install
    fi
  fi
  if [ $dyninst = yes ] 
  then
    echo "Configuring with $fortrancompiler $useropts $tagopt $ccompiler $archopt -dyninst=$dyninstdir "
    ./configure $fortrancompiler $useropts $tagopt $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 $ccompiler $archopt $pdtopt $mpiopts $commopt -PROFILEPARAM"
    ./configure $fortrancompiler $ccompiler $useropts $tagopt $archopt $pdtopt $mpiopts $commopt <wishver
    make clean; make install
  fi

# Set file permissions
  chmod -R go+rX *

  

