#! /bin/csh -f
#
# fpreproc -- preprocess fortran source code
#
#  $1 -- input file     $2 -- output file
# addl arguments:
#  $* -- gcc/cpp macro definitions (optional)
#  $* -- fpp options [-132|-free] (choose one only; default is for 72 wide
#        fixed form f77 source
#
# dmc 12 Oct -- NTCC/TRANSP generalization
#
# History:
#
# clf 22 Jul 2005 -- check $NTCC_ROOT,$TRANSP_LOCATION 
#                    instead of /usr/{pppl}/ntcc
#
# dmc July 2005 -- replaced perl script & fortran program with python
#   script of Rob Andre, cf source/bpython
#
# jec 18 Aug 2004 -- if FPREPROC_DEBUG, & failure, then rename the
#     output ( to <file>~ ) - since that is where the error msgs
#     end up  ( instead of rm )
#--------------------------------------------------------------------
#
# look for scripts and #include files
#  (1) under FPREPROC_ROOT (if defined)
#  (2) under TRANSP development environment
#  (3) in $NTCC_ROOT or $TRANSP_LOCATION
#  (4) in cwd or parent directory
#
  @ ier = 0
#
  if ( $?FPREPROC_DEBUG == 1 ) then
    echo " --------------------------------------- "
    echo " fpreproc:  FPREPROC_DEBUG is set:"
    echo "   cwd = $cwd "
    echo "   argv = $argv "
    echo " --------------------------------------- "
    set ioption = "-info"
  else
    set ioption = ""
  endif
#
  if ( $?FPREPROC_ROOT == 1 ) then
#user overide
    if ( -f $FPREPROC_ROOT/incl_cpp/blas_names.h ) then
      set pydir = $FPREPROC_ROOT/fpreproc
      set incldir = $FPREPROC_ROOT/incl_cpp
    else
      set pydir = $FPREPROC_ROOT/bpython
      set incldir = $FPREPROC_ROOT/include
    endif
  else if ( $?CODESYSDIR == 1 ) then
#assume TRANSP devel environment
    set pydir = $CODESYSDIR/source/bpython
    set incldir = $CODESYSDIR/source/incl_cpp
  else if ( $?NTCC_ROOT == 1 ) then
#assume root installed NTCC software available
    set pydir = $NTCC_ROOT/etc
    set incldir = $NTCC_ROOT/include
  else if ( $?TRANSP_LOCATION == 1 ) then
#assume NTCC-TRANSP software available
    set pydir = $TRANSP_LOCATION
    set incldir = $TRANSP_LOCATION/../include
  else if ( -f include/fpreproc/blas_names.h ) then
#assume locally available...
    set pydir = ./etc
    set incldir = ./include
  else if ( -f ../include/fpreproc/blas_names.h ) then
    set pydir = ../etc
    set incldir = ../include
  else if ( -f ../../include/fpreproc/blas_names.h ) then
    set pydir = ../../etc
    set incldir = ../../include

  else 
    echo " ?? fpreproc -- could not find fpreproc #include files"
    echo "    try setting FPREPROC_ROOT environment variable."
    @ ier++
  endif
#
  if ( $#argv < 2 ) then 
    echo " ?fpreproc:  fortran source preprocessor:  usage:"
    echo \
    '    fpreproc  input_source.F output_source.f ["macros"] [-free|-132]'
    echo \
    '    ...where ["macros"] (optional) gives fpp "macro" definitions, such as'
    echo \
    '    "-D__UNIX -D__F90 -D__LINUX", and, the fourth argument specifies'
    echo \
    '    source format variations (default is fixed width 72 columns).'
    @ ier++
  endif
#
  if ( $ier > 0 ) then
    exit 99
  endif
#
  set fpp_opt = ()
  set macros = ()
#
  @ j = 2
  while ( $j < $#argv ) 
    @ j++
    if ( "$argv[$j]" == "-free" ) then
      set fpp_opt = "$argv[$j]"
    else if ( "$argv[$j]" == "-132" ) then
      set fpp_opt = "$argv[$j]"
    else
      set macros = ($macros $argv[$j])
    endif
  end
#
#  any "#" lines in source file?
#
  grep '^\#' $1 >& /dev/null
#
#  yes:  preprocess...
#
  if ( $status == 0 ) then

      python3 $pydir/fppfile.py $1 $2 -I$incldir $macros $fpp_opt $ioption
      set cstatus = $status

  else
      cp $1 $2
      set cstatus = 0
  endif

  if ( $cstatus != 0 ) then
    if ( -f $2 ) then
      echo " ?fpreproc: -- c preprocessing error, status = $cstatus "
      if ( $?FPREPROC_DEBUG == 1 ) then
         mv -v $2 $2~
      else
#         echo "  removing: $2"
          rm -v $2
      endif
    endif
  endif

  exit $cstatus
