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

#!/bin/sh 




BINDIR=/usr/lib/hpc/gnu9/pdtoolkit//x86_64//bin
PDTDIR=/usr/lib/hpc/gnu9/pdtoolkit/
FLINTHOME=${PDTDIR}/etc
export FLINTHOME
STDINC=
VERBOSE=off
PDB=pdtfilenotspecified

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

# Creation of exclude.txt to exclude modules that haven't been compiler by gfortran 4.8
declare -a EXCLUDELIST=() #("mpi" "iso_c_binding")
EXCLUDEDIR="/tmp/pdt-${USER}"
EXCLUDEFILE=${EXCLUDEDIR}/exclude.txt
mkdir -p ${EXCLUDEDIR}
echo ${EXCLUDELIST[0]} > ${EXCLUDEFILE}
for ((i=1;i<"${#EXCLUDELIST[@]}";i++)); do
    echo ${EXCLUDELIST[$i]} >> ${EXCLUDEFILE}
done

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


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

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


# default compiler options

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

flintform="false"
preprocess="no"

for allargs in "$@" ; do
    arg=`echo "x$allargs" | sed -e 's/^x//' -e 's/"/\\\"/g' -e s,\',%@%\',g -e 's/%@%/\\\/g' -e 's/ /\\\ /g'`

    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"
      elif [[ "$arg" = -E* ]] ; then
          mod=${arg:2}
          echo ${mod,,} >> ${EXCLUDEFILE}
	    else
		SPAWNARGS="$SPAWNARGS $arg"
	    fi
	fi
	
    fi
done

# add number of mod names being excluded to front of file
wc -l < ${EXCLUDEFILE} | cat - ${EXCLUDEFILE} > ${EXCLUDEDIR}/tmp && mv ${EXCLUDEDIR}/tmp ${EXCLUDEFILE}

# 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 [ ! -r ${INF} ]; then
  echo "PDT: ${INF} not readable"
  exit 1
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"
GF_ARGS="-c -o /dev/null -fdump-fortran-original -fsyntax-only"
#
# run the gfortran parser
#

ERROR=false
export GFORTRAN_PDB_FILENAME="pdtgf$$.pdb"
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}
    if [ $? != 0 ] ; then 
        ERROR=true
    fi
    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
    if [ $? != 0 ] ; then
        ERROR=true
        rm -f $GFORTRAN_PDB_FILENAME
        eval ${GFORTRAN} ${GF_ARGS} ${ARCHOPT} ${SPAWNARGS} ${LOCOPT} ${EDGOPT}
    fi
	  mv $GFORTRAN_PDB_FILENAME ${PDB}
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. 
wordcount=`wc -c ${PDB} | awk '{print $1;}'` 
if [ $wordcount -eq 23  -o $wordcount -eq 0 ]; then
  echo "Error ${PDB}: Invoking f95parse $*"
  ${BINDIR}/f95parse $* 
fi

# EOF
