#!/bin/sh

## script to compile a single Fortran routine, via f2c
## NOT intended as a general Fortran script, but for use
## in makefiles for S chapters, on machines lacking a native
## f77, or where you don't like the f77
## Usage: Sfort [opts] thing.f
## where [opts] are options to f2c (NOT to the C compiler)

opts=
target=
file=

while true
do
  case $# in
  0) break;;
  *)
	case $1 in
	-o) target=$2; shift; shift;;
	-c) file=$2; shift; shift;;
	-*) opts="$opts $1"; shift;;
	*.f) file=$1; shift;;
	*.r) file=$1; shift;;
	*) echo "Usage: Sfort [opts] thing.f"; exit 1;;
	esac
  ;;
  esac


case $file in
*.f) 
	name=`basename $file '.f'`;
	if [ "x$target" = "x" ]
	then target="$name.o"
	fi
	f2c $opts ${name}.f && make $target && rm ${name}.c*
	file=""
	target=""
	;;

*.r) 
	name=`basename $file '.r'`;
	ftarget="$name.f"
	ratfor ${name}.r > $ftarget 
	if [ "x$target" = "x" ]
	then target="$name.o"
	fi
	f2c $opts ${name}.f && make $target && rm ${name}.c*
	file=""
	target=""
	ftarget=""
	;;
?*) echo "$file: need a <thing>.f file for the target file"; exit 1;;
esac


done
