#!/bin/sh 
#
# DART software - Copyright UCAR. This open source software is provided
# by UCAR, "as is", without charge, subject to all terms of use at
# http://www.image.ucar.edu/DAReS/DART/DART_download
#
# $Id$

# usage: fixsystem [ your_fortran_command_name | -help  ]
#         (e.g. ifort, pgf90, gfortran, g95, xlf90, etc)
#
# this script updates the mpi source files for any compiler-dependent
# changes needed before building.
# 
# at the moment the only compiler-dependent code is the declaration
# of the "system()" function in a fortran interface block.
# the code uses this function to run a shell script or command and 
# wait for the command exit code, e.g.:  rc = system("/bin/date")
#
# for all compilers, except gfortran, an interface block is required
# to define the integer return from the system function.  however
# the gfortran compiler gives an error if this block is defined.
# this script enables and disables this interface block by
# looking for a pair of specially formatted comment lines and
# commenting in (or out) all the lines between those comment 
# delimiter lines.
#
# the original usage of this script was without any arguments. 
# the backwards compatibility remains for now, but is deprecated.
# usage now is to give it a single argument - the name that the
# fortran compiler is invoked with, e.g. ifort, xlf90, pgf90, etc.
# it will ensure that any required changes to the source will be
# made here before compilation.


for f in mpi_utilities_mod.f90 null_mpi_utilities_mod.f90
do

  # figure out what state the source file is in before we start
  export bline1="`fgrep SYSTEM_BLOCK_EDIT ${f} | grep START | head -n 1`"
  if [ "`echo $bline1 | grep COMMENTED_OUT`" != ""  ]; then
    export before1=out
  elif [ "`echo $bline1 | grep COMMENTED_IN`" != ""  ]; then
    export before1=in
  else
    echo ${f} not found, or does not have the right comment string to
    echo automatically change the system interface block via script.
    echo Please restore original file from the subversion repository
    echo and try again.
    exit 1
  fi
  
  # NAG sections have both in and out - but NAG_BLOCK_EDIT is key
  export bline2="`fgrep NAG_BLOCK_EDIT ${f} | grep START | head -n 1`"
  if [ "`echo $bline2 | grep COMMENTED_OUT`" != ""  ]; then
    export before2=out
  elif [ "`echo $bline2 | grep COMMENTED_IN`" != ""  ]; then
    export before2=in
  else
    echo ${f} not found, or does not have the right comment string to
    echo automatically change the system interface block via script.
    echo Please restore original file from the subversion repository
    echo and try again.
    exit 1
  fi

  # no args given - error.  required now.
  if [ $# = 0 ]; then
      echo invalid usage, 1 argument required by $0
      echo "usage: $0 [ your_fortran_command_name | -help  ]"
      echo "  e.g. $0 gfortran"
      echo "  or   $0 ifort "
      echo "  or   $0 pgf90 "
      echo "  etc."
      exit 1
  elif [ $# = 1 ]; then
    # single arg: the name of your fortran compiler command
    if ([ "$1" = help ] || [ "$1" = -help ] || [ "$1" = --help ]); then
      echo "usage: $0 [ your_fortran_command_name | -help  ]"
      echo "  e.g. $0 gfortran"
      echo "  or   $0 ifort "
      echo "  or   $0 pgf90 "
      echo "  etc."
      exit 1
    elif ([ "$1" = gfortran ] || [ "$1" = nagfor ]); then
      export todo1=out
      export todo2=out
    elif [ "$1" = nagfor ]; then
      export todo1=out
      export todo2=in
    else
      export todo1=in
      export todo2=out
    fi
    export compiler=$1

  else
    # too many arguments, give an error message and exit
    echo invalid usage, more than 1 argument given to $0
    echo "usage: $0 [ your_fortran_command_name | -help  ]"
    echo "  e.g. $0 gfortran"
    echo "  or   $0 ifort "
    echo "  or   $0 pgf90 "
    echo "  etc."
    exit 1
  fi
  
  # if we are already in the right state, loop to next file
  if ([ $before1 = $todo1 ] && [ $before2 = $todo2 ]); then continue; fi
 
  # save original copy for backup if one does not already exist.
  if [ ! -f ${f}.orig ]; then
    cp -p ${f} ${f}.orig
  fi
  
  # say what compiler we are doing this for, and move the existing
  # code into a temporary file so the sed command does not overwrite it.
  echo Setting for $compiler compiler in ${f}
  mv ${f} tempfile

  # removing comment chars, enabling interface block code
  if [ $todo1 = in ]; then
   sed -e '/SYSTEM_BLOCK_EDIT START COMMENTED_OUT/,/SYSTEM_BLOCK_EDIT END COMMENTED_OUT/s/^!//' \
       -e '/\(SYSTEM_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_OUT/s//\1 COMMENTED_IN/' tempfile > ${f}
   mv ${f} tempfile
  fi
  
  # adding comment chars, disabling interface block code
  if [ $todo1 = out ]; then
   sed -e '/SYSTEM_BLOCK_EDIT START COMMENTED_IN/,/SYSTEM_BLOCK_EDIT END COMMENTED_IN/s/^/!/' \
       -e '/\(SYSTEM_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_IN/s//\1 COMMENTED_OUT/' tempfile > ${f}
   mv ${f} tempfile
  fi

  # changing comment chars, enabling NAG specific block code
  # non-nag section headers cannot match nag headers.
  if [ $todo2 = in ]; then
   sed -e '/NAG_BLOCK_EDIT START COMMENTED_OUT/,/NAG_BLOCK_EDIT END COMMENTED_OUT/s/^!//' \
       -e '/\(NAG_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_OUT/s//\1 COMMENTED_IN/' \
       -e '/OTHER_BLOCK_EDIT START COMMENTED_IN/,/OTHER_BLOCK_EDIT END COMMENTED_IN/s/^/!/' \
       -e '/\(OTHER_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_IN/s//\1 COMMENTED_OUT/' tempfile > ${f}
  fi
  
  # changing comment chars, disabling NAG specific block code
  if [ $todo2 = out ]; then
   sed -e '/NAG_BLOCK_EDIT START COMMENTED_IN/,/NAG_BLOCK_EDIT END COMMENTED_IN/s/^/!/' \
       -e '/\(NAG_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_IN/s//\1 COMMENTED_OUT/' \
       -e '/OTHER_BLOCK_EDIT START COMMENTED_OUT/,/OTHER_BLOCK_EDIT END COMMENTED_OUT/s/^!//' \
       -e '/\(OTHER_BLOCK_EDIT [A-Z][A-Z]*\) COMMENTED_OUT/s//\1 COMMENTED_IN/' tempfile > ${f}
  fi

  \rm -f tempfile

done

exit 0

# <next few lines under version control, do not edit>
# $URL$
# $Revision$
# $Date$
