#!/bin/sh
 
#    Jul 15/92 - E. Chan 

#id  fixpgm  - changes certain specific names of intrinsic functions to the
#id            corresponding generic ones in files containing Fortran code

#    AUTHOR  - E. Chan (Jul 15/92)

#hd  PURPOSE - "fixpgm" changes the names of certain intrinsic functions to their
#hd            generic equivalent. This is necessary if the Fortran code is to
#hd            compiled in double precision.

#pr  PARAMETERS:
#pr 
#pr    POSITIONAL
#pr 
#pr      fn1 fn2 ... fnm = list of m files that require fixing
#pr    
#pr    SECONDARY
#pr
#pr      change = switch to make changes permanent or to just list out
#pr               the possible changes (='no'/'yes')

#ex  EXAMPLE
#ex  
#ex   fixpgm * change
#ex  
#ex   The above example fixes all files in the current directory. 

#  * Check for options.

for arg 
do
  case $arg in 
    change) eval 'change=yes'              ;;   
	 *) file_list="$file_list $arg"
  esac
done

#  * Loop over all files in the input list.

for file in $file_list
do

  # Ensure that only existing ascii text files are operated on.

  info=`file $file 2>/dev/null `
  infox=`expr "$info" : '.*\(data.*\).*'`
  if [ "$infox" != 'data or International Language text' ] ; then
   file_status=`expr "$info" : '.*\(text\).*'`
   file_status=${file_status:=`expr "$info" : '.*\(character\).*'`}
  fi

  if [ ! -f "$file" ] ; then
    echo "file $file does not exist: resuming execution with next file in list"
  elif [ -z "$file_status" ] ; then
    echo "file $file is not an ascii text file: file skipped"
  else
    if [ "$change" = 'yes' ] ; then
      sed '/[IC]\(ABS *(\)/s//\1/g
           /I\(SIGN *(\)/s//\1/g
           /AMAX[01]\( *(\)/s//MAX\1/g
           /AMIN[01]\( *(\)/s//MIN\1/g
           /AMOD\( *(\)/s//MOD\1/g
           /ALOG\( *(\)/s//LOG\1/g
           /ALOG10\( *(\)/s//LOG10\1/g' $file > tmpfile$$
      mv tmpfile$$ $file
    else
      sed -n '1p
	      /[IC]\(ABS *(\)/s//\1/gp
              /I\(SIGN *(\)/s//\1/gp
              /AMAX[01]\( *(\)/s//MAX\1/gp
              /AMIN[01]\( *(\)/s//MIN\1/gp
              /AMOD\( *(\)/s//MOD\1/gp
              /ALOG\( *(\)/s//LOG\1/gp
              /ALOG10\( *(\)/s//LOG10\1/gp' $file
    fi
  fi 
done

exit
