#!/bin/sh 
COMPILER_OPTS=" -D_GNU_SOURCE "

BINDIR=/opt/ohpc/pub/libs/gnu/pdtoolkit/3.21/x86_64//bin
PDTDIR=/opt/ohpc/pub/libs/gnu/pdtoolkit/3.21
STDINC=
VERBOSE=off

if [ "x$TMPDIR" = "x" ]; then
  TMP="/tmp"
else
  TMP=${TMPDIR}
fi

# if -t is specified, template text strings are not included in pdb files
PDTOPT="-t"
ORIGARGS=$*

ROSEGEN_DIR="${PDTDIR}/contrib/rose/rose-header-gen"
ROSE_INCLUDES="${ROSEGEN_DIR}/rose_c_includes"
PREINCLUDES="--sys_include ${ROSEGEN_DIR}/include/gcc_HEADERS" 

if [ ! -r ${ROSE_INCLUDES} ]; then
  echo "${ROSE_INCLUDES} not readable!"
  exit 0;
fi

if [ ! -r /usr/include/sys/types.h ]; then
  PDT_MULTI_ARCH_OPTS="-DPDT_MULTI_ARCH=1 -DPDT_MULTI_ARCH_`uname -m`=1"
else
  PDT_MULTI_ARCH_OPTS=
fi

FIXED_RELATIVE_INCLUDES=`cat ${ROSE_INCLUDES} | grep -v '^/.*' | sed -e "s@^@${ROSEGEN_DIR}/include/@"`
ABSOLUTE_INCLUDES=`cat ${ROSE_INCLUDES} | grep '^/.*'`

NEWLINE=$'\n'
ALL_INCLUDES="${FIXED_RELATIVE_INCLUDES}${NEWLINE}${ABSOLUTE_INCLUDES}"
PDT_INCLUDE_DIRS=`echo "${ALL_INCLUDES}" | awk 'ORS=" " { print "--sys_include " $1; }' | sed -e 's@$@ \\ @g' `

ROSE_CONFIG="${ROSEGEN_DIR}/rose_config.h"

GCC_MAJOR=`grep BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER ${ROSE_CONFIG} | cut -d ' ' -f 3`
GCC_MINOR=`grep BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER ${ROSE_CONFIG} | cut -d ' ' -f 3`
GCC_PATCH=`grep BACKEND_CXX_COMPILER_PATCH_VERSION_NUMBER ${ROSE_CONFIG} | cut -d ' ' -f 3`
if [ -z "$GCC_PATCH" ]; then
  GCC_PATCH=0               
fi
GNU_VER="${GCC_MAJOR}${GCC_MINOR}${GCC_PATCH}00"

PDT_DEFINES="${PDT_MULTI_ARCH_OPTS} -DPDT_PARSER -DPDT_PARSER_EDG4101 -D__GNUG__=${GCC_MAJOR} -D__GNUC__=${GCC_MAJOR} -D__GNUC_MINOR__=${GCC_MINOR} -D__GNUC_PATCHLEVEL__=${GCC_PATCH} --preinclude rose_edg_required_macros_and_functions.h -DROSE_LANGUAGE_MODE=0 --gcc  --gnu_version ${GNU_VER} --c99"

EDGOPT="--edg_base_dir ${ROSEGEN_DIR}"

COMPOPT="${PDT_INCLUDE_DIRS} ${PDT_DEFINES} "

# local options, if any
LOCOPT=

# Remove all options other than -I and -D from the command line.
INF=$1
SPAWNARGS=
shift

# PtDB
# Added to support a "-o" option for specifiying an
# output file.
getnextarg=0
gotpdbfile=0
# End - PtDB

for arg in "$@"
do
# Allow -I -D arguments to be passed to edgcpfe
  # PtDB
  if [ $getnextarg -eq 1 ]
  then
    NEWPDB=$arg
    getnextarg=0
    gotpdbfile=1
  fi

  if [ "y$arg" = "y-o" ]
  then
    getnextarg=1
  fi
  # End - PtDB

  if [ "y$arg" = "y--c99" ] ; then
      CSTDOPT="--c99"
  elif [ "y$arg" = "y-v" ] ; then
      VERBOSE=on
  fi
  for t in -I -D ; do
      testarg=${arg#$t}
      if [ "y$testarg" != "y$arg" -a "y$arg" != "y-I/usr/include" ] ; then
# /usr/include is already taken care of. It should not be passed by the user
	  mod_arg=`echo $arg | sed -e 's/"/\\\"/g'`
# For PETSc, to take care of -DARG='"STR"' to generate -DARG=\"STR\"
	  SPAWNARGS="$SPAWNARGS \"$mod_arg\""
      fi
  done
# For mpi.h to be classified as a system header we use:
# % cxxparse foo.cpp --sys_include/usr/local/packages/mpich-1.2/include

  for t in --sys_include
  do
    testarg=${arg#$t}
    if [ "y$testarg" != "y$arg" -a "y$arg" != "y--sys_include/usr/include" ]
    then
      mod_arg=`echo $arg | sed -e 's/--sys_include/--sys_include /g'`
      SPAWNARGS="$SPAWNARGS $mod_arg"
    fi
  done

# Processed an individual argument

done
#echo "SPAWNARGS=$SPAWNARGS"


ILF=${TMP}/pdt.$USER.$$.il
case ${INF} in
*.c)	PDB=`basename ${INF} .c`.pdb
	;;
*)      PDB=${INF}.pdb
	;;
esac

# PtDB
if [ $gotpdbfile -eq 1 ]
then
  PDB=${NEWPDB}
  PDBOUTARG="-o ${PDB}"
fi
# End - PtDB


#
# run the EDG parser
#
if [ $VERBOSE = on ] ; then
    echo "${BINDIR}/edgcpfe4101 ${PREINCLUDES} ${SPAWNARGS} ${LOCOPT} ${COMPOPT} ${CSTDOPT} ${EDGOPT} ${ARCHOPT} ${ASMOPT1} ${ASMOPT2} ${ASMOPT3} -o ${ILF} ${INF} "
    echo "	edgcpfe4101 ${INF} --> ${ILF}"
fi

if [ ! -r ${INF} ]; then
  echo "PDT: ${INF} not readable"
  exit 1
fi



eval ${BINDIR}/edgcpfe4101 ${PREINCLUDES} ${SPAWNARGS} ${LOCOPT} ${COMPOPT} ${CSTDOPT} ${EDGOPT} ${ARCHOPT} ${ASMOPT1} ${ASMOPT2} ${ASMOPT3} -o ${ILF} ${INF} >/dev/null 2>/dev/null
if [ $? != 0 ] ; then
    eval ${BINDIR}/edgcpfe ${PREINCLUDES} ${SPAWNARGS} ${LOCOPT} ${COMPOPT} ${CSTDOPT} ${EDGOPT} ${ARCHOPT} ${ASMOPT1} ${ASMOPT2} ${ASMOPT3} -o ${ILF} ${INF}
    exit $?
fi

#
# convert output of parser to pdb file
#
if [ $VERBOSE = on ] 
then
  echo "	taucpdisp4101 ${PDTOPT} ${ILF} --> ${PDB}"
fi
${BINDIR}/taucpdisp4101 ${PDTOPT} ${ILF} > ${PDB}
ret=$?
rm -f ${ILF}

exit $ret
