#!/bin/sh
usage()
{
    cmd=`basename $0`
    echo ""
    echo " $cmd - Generates a wrapper library that can intercept "
    echo " at link time or at runtime UPC routines specified in a header file"
    echo " Usage: $cmd <headerfile> <library>  [-w (default) | -d | -r] -gasnet=<dir> -gasnetconduit=<conduit> -upc=<dir> "
    echo " -w (default): generates wrappers for re-linking the application"
    echo " -d : generates wrappers by redefining routines during compilation in header files"
    echo " -r : generates wrappers that may be pre-loaded using tau_exec at runtime"
    echo " -gasnet=<dir> : specifies the location of the gasnet directory"
    echo " -gasnetconduit=<conduit> : specifies the conduit (smp, mpi, etc.) for gasnet"
    echo " -upc=<dir> : specifies the upc directory"
    echo "NOTE: $cmd reads the TAU_MAKEFILE  environment variable to get PDT settings"
    echo " Example: "
    echo " % $cmd tau_upcr.h -lm -gasnet=/usr/local/packages/gasnet-1.18.0-mpi-smp -gasnetconduit=mpi-conduit -upc=/usr/local/packages/bupc-2.14.2/opt_inst"
    echo " generates a wrapper library that may be linked in using TAU_OPTIONS -optTauWrapFile=<wrapperdir>/link_options.tau"
    echo "" 
    exit 1
}

upcdir=/usr/local/packages/bupc-2.14.2/opt_inst
gasnetdir=/usr/local/packages/gasnet-1.18.0-mpi-smp
gasnetconduit=mpi-conduit
runtime_specified=0;
header_redirection_specified=0;
headerfile=""
library=""
modarg=""
if [ $# = 0 ] ; then
    usage
fi

if [ $# -gt 1 ]; then
  headerfile=$1
  library=$2
  shift;
  shift;
fi

for arg in "$@"; do
  case $arg in 
    -r)
      runtime_specified=1; 
      shift; 
      ;;
    -d)
      header_redirection_specified=1; 
      shift; 
      ;;
    -gasnet=*)
      gasnetdir=`echo $arg | sed -e 's/-gasnet=//g'`
      shift;
      ;;
    -gasnetconduit=*)
      gasnetconduit=`echo $arg | sed -e 's/-gasnetconduit=//g'`
      shift;
      ;;
    -upc=*)
      upcdir=`echo $arg | sed -e 's/-upc=//g'`
      shift;
      ;;
  esac
done

if [ "x$TAU_MAKEFILE" = "x" ] ;then
  echo "ERROR: Please set the TAU_MAKEFILE environment variable."
  exit 1
fi

if [ ! -r $TAU_MAKEFILE ] ;then
  echo "ERROR: environment variable TAU_MAKEFILE is set but the file is not readable"
  exit 1
fi


pdtdir=`grep PDTDIR= $TAU_MAKEFILE | sed -e 's/PDTDIR=//g' `
tauarch=`tau-config | grep TAUARCH | sed -e 's@TAUARCH=@@g' `
basedir=`tau-config | grep BASEDIR | sed -e 's@BASEDIR=@@g' `

parser=$pdtdir/$tauarch/bin/upcparse
if [ ! -x $parser ]; then
  echo "ERROR: Can't execute $parser: Using PDT = $pdtdir, ARCH = $tauarch"
  exit 1
fi
baseheader=`basename $headerfile .h`
echo $parser $headerfile -DGASNET_SEQ -I$upcdir/include -DUPCRI_BUILDING_LIBUPCR=1 -I$gasnetdir/include -I$gasnetdir/include/$gasnetconduit -DGASNETT_USE_GCC_ATTRIBUTE_MAYALIAS
$parser $headerfile -DGASNET_SEQ -I$upcdir/include -DUPCRI_BUILDING_LIBUPCR=1 -I$gasnetdir/include -I$gasnetdir/include/$gasnetconduit -DGASNETT_USE_GCC_ATTRIBUTE_MAYALIAS

mv $baseheader.pdb $headerfile.pdb 

if [ $runtime_specified = 1 ] ; then
  runtimeopt='-r'
else
  if [ $header_redirection_specified = 1 ]; then
    runtimeopt=''
  else 
    runtimeopt='-w'
  fi
fi

#echo "tau_wrap $headerfile.pdb $headerfile -o wr.c $runtimeopt $library $*"
# Generate select.tau

rm -f select.tau
echo "BEGIN_INCLUDE_LIST" > select.tau
nm -A $upcdir/lib/*.a | grep " _upcr" | awk '{ print $3;}' | sed -e 's/_upcr/"# _upcr/g' -e 's/$/(#"/g' >> select.tau
nm -A $upcdir/lib/*.a | grep " UPCRL" | awk '{ print $3;}' | sed -e 's/UPCRL/"# UPCRL/g' -e 's/$/(#"/g' >> select.tau
nm -A $upcdir/lib/*.a | grep " upcr" | awk '{ print $3;}' | sed -e 's/upcr/"# upcr/g' -e 's/$/(#"/g' >> select.tau
nm -A $upcdir/lib/*.a | grep " _bupc_" | awk '{ print $3;}' | sed -e 's/_bupc_/"# _bupc_/g' -e 's/$/(#"/g' >> select.tau
#nm -A $upcdir/lib/*.a | grep " bupc_" | awk '{ print $3;}' | sed -e 's/bupc_/"# bupc_/g' -e 's/$/(#"/g' >> select.tau
echo "END_INCLUDE_LIST" >> select.tau
$basedir/bin/tau_wrap $headerfile.pdb $headerfile -o wr.c $runtimeopt $library $* -f select.tau
dirname=`basename ${headerfile} .h`_wrapper

# replace bupc_smemvec_t [] a with bupc_smemvec_t * a
sed -e 's/\[\]/\*/g' ${dirname}/wr.c > ${dirname}/wr.c~
mv ${dirname}/wr.c~ ${dirname}/wr.c 

# Fix the Makefile with the correct smp args.
sed -e "s@\$(TAU_MPI_INCLUDE)@\$(TAU_MPI_INCLUDE) -DGASNET_SEQ -I$upcdir/include -DUPCRI_BUILDING_LIBUPCR=1 -I$gasnetdir/include -I$gasnetdir/include/$gasnetconduit -DGASNETT_USE_GCC_ATTRIBUTE_MAYALIAS @g" ${dirname}/Makefile > ${dirname}/Makefile~
mv ${dirname}/Makefile~ ${dirname}/Makefile

sed -e 's/-Wl,-wrap/-Wl,-Wl,-wrap/g' -e 's@-L.*$@-ltau_upcr_wrap@g'  ${dirname}/link_options.tau > ${dirname}/link_options.tau~

mv ${dirname}/link_options.tau~  ${dirname}/link_options.tau
cd ${dirname}
make
cd ..
rm -f ${headerfile}.pdb 
mkdir -p $basedir/lib/wrappers/upc
mkdir -p $basedir/lib/wrappers/upc/bupc
echo cp ${dirname}/link_options.tau $basedir/lib/wrappers/upc/bupc
cp ${dirname}/link_options.tau $basedir/lib/wrappers/upc/bupc
echo cp ${dirname}/libtau_upcr_wrap.a $basedir/lib/
cp ${dirname}/libtau_upcr_wrap.a $basedir/lib/
