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

#!/bin/sh 

BINDIR=/opt/ohpc/pub/libs/gnu/pdtoolkit/3.21/x86_64//bin
PDTDIR=/opt/ohpc/pub/libs/gnu/pdtoolkit/3.21
FLINTHOME=${PDTDIR}/etc
export FLINTHOME
STDINC=
VERBOSE=off
PDB=pdtfilenotspecified

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

# place it at the start of the path so that cc1/gfortran/f951 from PDT are used
GFORTRAN="PATH=${BINDIR}/pdt_gfortran:$PATH gfortran"


PDTOPT=""
if [ $# = 0 ] ; then
  echo "Usage: `basename $0` [-v] [-o<file>] [files]" 1>&2
  exit 1
fi

if [ ! -f ${BINDIR}/pdt_gfortran/gfortran ] ; then
  echo "gfparse invoking f95parse ..."
  ${BINDIR}/f95parse $* -v
  exit
fi


# default compiler options

# ---------------------------------------------
# local options, if any
LOCOPT=" " 

flintform="false"
preprocess="no"

for arg in "$@" ; do
    if [ "y$arg" = "y-v" ] ; then
	VERBOSE=on
    else

	if [ "$flintform" = "true" ] ; then
	    if [ "y$arg" = "yfree" ] ; then
		SPAWNARGS="$SPAWNARGS -ffree-form"
	    fi
	    if [ "y$arg" = "yfixed" ] ; then
		SPAWNARGS="$SPAWNARGS -ffixed-form"
	    fi
	    flintform="false"
	else
        
            # CHECK for -o<file> option
	    testarg=${arg#-o}
	    if [ "y$testarg" != "y$arg" ] ; then
		PDB=$testarg
	    fi

	    if [ "y$arg" = "y-R" ] ; then
		flintform="true"
	    elif [ "y$testarg" = "y-p" ] ; then
		preprocess="yes"
#		SPAWNARGS="$SPAWNARGS -fwe-did-preprocessing"
	    else
		SPAWNARGS="$SPAWNARGS $arg"
	    fi
	fi
	
    fi
done

# What is the name of the first file on the commandline? 
for arg in "$@" ; do
  case $arg in 
    *.? | *.?? | *.??? ) INF=$arg
         break
    ;;
  esac

done

#
# if the -o<file> option is not specified, create the pdb file based on the
# the name of the first source file
#

if [ $PDB = pdtfilenotspecified ] ; then
  case ${INF} in
    *.f)	PDB=`basename ${INF} .f`.pdb
	;;
    *.F)	PDB=`basename ${INF} .F`.pdb
	;;
    *.F90)	PDB=`basename ${INF} .F90`.pdb
	;;
    *.f90)	PDB=`basename ${INF} .f90`.pdb
	;;
    *.f95)	PDB=`basename ${INF} .f95`.pdb
	;;
    *)	PDB=${INF}.pdb
	;;
  esac
fi

if [ ${PDB} = ".pdb" ] ; then
  echo "ERROR: Source file not specified."
  echo "PDT accepts upto three letter suffixes in file names (e.g. foo.f90)"
  exit 1
fi


GF_ARGS="-c -o /dev/null -fdump-parse-tree"
#
# run the gfortran parser
#

export GFORTRAN_PDB_FILENAME="pdtgf$$.pdb"
mkdir -p /tmp/pdt-${USER}
if [ $VERBOSE = on ] ; then
    # print the output of the pdtflint command
    echo "	gfortran ${SPAWNARGS} --> ${PDB}"
    echo "Executing ${GFORTRAN} ${GF_ARGS} ${ARCHOPT} ${SPAWNARGS} ${LOCOPT} ${EDGOPT}"
    rm -f $GFORTRAN_PDB_FILENAME
    eval ${GFORTRAN} ${GF_ARGS} ${ARCHOPT} ${SPAWNARGS} ${LOCOPT} ${EDGOPT}
    mv $GFORTRAN_PDB_FILENAME ${PDB}
else
    # disable printing of stdout/stderr unless there is an error
    rm -f $GFORTRAN_PDB_FILENAME
    eval ${GFORTRAN} ${GF_ARGS} ${ARCHOPT} ${SPAWNARGS} ${LOCOPT} ${EDGOPT} 1> /dev/null 2>&1
    mv $GFORTRAN_PDB_FILENAME ${PDB}
    if [ $? != 0 ] ; then
	rm -f $GFORTRAN_PDB_FILENAME
	eval ${GFORTRAN} ${GF_ARGS} ${ARCHOPT} ${SPAWNARGS} ${LOCOPT} ${EDGOPT}
	mv $GFORTRAN_PDB_FILENAME ${PDB}
    fi
fi


if [ ! -f ${PDB} -a -x ${BINDIR}/gfparse48 -a -x ${BINDIR}/pdt_gfortran48/gfortran ]
then
  echo "PDT: Error: Couldn't create ${PDB}: Invoking the alternate PDT parser based on gfortran 4.8.5"
  echo ${BINDIR}/gfparse48 $*
  ${BINDIR}/gfparse48 $*
  ret=$?
fi



# If gfparse can't produce a valid PDB file, invoke f95parse. This happens when
# it produces a file with two lines <PDB 3.0> and language fortran. 
if [ `wc -c ${PDB} | awk '{print $1;}'` -eq 23 ]; then
  echo "Error ${PDB}: Invoking f95parse $*"
  ${BINDIR}/f95parse $* 
fi


# EOF
